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

ぃ、小莉子 提交于 2020-01-21 08:31:52

问题


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..

Here is my Itext code in java...

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..


回答1:


You need a Chunk to do that:

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 ChunkBackground example and the resulting PDF document.

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



来源:https://stackoverflow.com/questions/19976343/how-to-set-the-paragraph-of-itext-pdf-file-as-rectangle-with-background-color-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!