使用itext在pdf footer中创建包含2行的表 [英] Creating table with 2 rows in pdf footer using itext

查看:207
本文介绍了使用itext在pdf footer中创建包含2行的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想添加2行的页脚。第一行将具有背景颜色的文档名称。第二行将有副本权利说明。我尝试使用ColumnText创建。但我无法设置行的背景颜色(只有文本获取背景颜色)。有没有可以实现这一目标。我整晚都在寻找解决方案但却无法做到。

Hi I want to add footer with 2 rows. 1st row will have document name with background color. 2nd row will have copy rights notes. I tried to create using ColumnText. but I am not able to set the background color for the row(only text getting background color). Is there any ay to achieve this. i spend my whole night to find a solution but not able to do.

推荐答案

您可以通过阅读文档为自己省去一个不眠之夜。您已经发现可以使用 setBackgroundColor()方法设置单元格的背景,并且可以使用<$ c $在绝对位置添加表格c> writeSelectedRows()方法。

You could have saved yourself a sleepless night by reading the documentation. You'd have discovered that you can set the background of a cell using the setBackgroundColor() method and that you can add a table at an absolute position using the writeSelectedRows() method.

看看 TableFooter 示例:

PdfPTable table = new PdfPTable(1);
table.setTotalWidth(523);
PdfPCell cell = new PdfPCell(new Phrase("This is a test document"));
cell.setBackgroundColor(BaseColor.ORANGE);
table.addCell(cell);
cell = new PdfPCell(new Phrase("This is a copyright notice"));
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell);

如果连续有多个单元格,则需要为所有单元格设置背景。请注意,我正在为表格定义总宽度(523是页面的宽度减去边距)。总宽度是必需的,因为我们将使用 writeSelectedRows()添加表格:

If you have more than one cell in a row, you need to set the background for all cells. Note that I'm defining a total width for the table (523 is the width of the page minus the margins). The total width is needed because we'll add the table using writeSelectedRows():

footer.writeSelectedRows(0, -1, 36, 64, writer.getDirectContent());

生成的PDF看起来像这个。确保以页脚内容不与页面内容重叠的方式定义页面边距。

The resulting PDF looks like this. Make sure you define the margins of your page in such a way that the footer table doesn't overlap with the page content.

这篇关于使用itext在pdf footer中创建包含2行的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆