Creating table with 2 rows in pdf footer using itext

让人想犯罪 __ 提交于 2019-12-20 06:36:32

问题


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.


回答1:


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.

Take a look at the TableFooter example:

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);

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());

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.



来源:https://stackoverflow.com/questions/22122340/creating-table-with-2-rows-in-pdf-footer-using-itext

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