如何在Java中将itext pdf文件的段落设置为带有背景色的矩形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19976343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:00:50  来源:igfitidea点击:

How to set the paragraph of itext pdf file as rectangle with background color in Java

javapdffontsitext

提问by Divyam

I am designing a pdf report using itext library.I have implemented a paragraph in it.Now as per my requirement i have to set this paragraph inside rectangular box with background color but i am not able to do it..

我正在使用 itext 库设计 pdf 报告。我已经在其中实现了一个段落。现在根据我的要求,我必须将此段落设置在具有背景颜色的矩形框中,但我无法做到。

Here is my Itext code in java...

这是我在java中的Itext代码......

Font f = new Font(FontFamily.TIMES_ROMAN, 25.0f, Font.BOLD, BaseColor.CYAN);
Paragraph p = new Paragraph("Total Cost:" + dbsumcallcost, f);
document.add(p);

Please guys help me. Thanks in advance..

请大家帮帮我。提前致谢..

采纳答案by Bruno Lowagie

You need a Chunkto do that:

你需Chunk要这样做:

Font f = new Font(FontFamily.TIMES_ROMAN, 25.0f, Font.BOLD, BaseColor.WHITE);
Chunk c = new Chunk("Total Cost:" + dbsumcallcost, f);
c.setBackground(BaseColor.RED);
Paragraph p = new Paragraph(c);
document.add(p);

See the ChunkBackgroundexample and the resulting PDF document.

请参阅ChunkBackground示例和生成的 PDF 文档

You can fine-tune the rectangle by using a slightly different setBackground()method: http://api.itextpdf.com/itext/com/itextpdf/text/Chunk.html#setBackground%28com.itextpdf.text.BaseColor,%20float,%20float,%20float,%20float%29

您可以使用稍微不同的setBackground()方法微调矩形:http: //api.itextpdf.com/itext/com/itextpdf/text/Chunk.html#setBackground%28com.itextpdf.text.BaseColor,%20float,% 20float,%20float,%20float%29