How to create a table with 2 rows that can be used as a footer?

I want to add footer with 2 rows. The 1st row will have the document name with a background color. The 2nd row will have copyright notices. I tried to create this table using ColumnText, but I am n

I want to add footer with 2 rows. The 1st row will have the document name with a background color. The 2nd row will have copyright notices. I tried to create this table using ColumnText, but I am not able to set the background color for the row (only the text is getting background color).

Posted on StackOverflow on Mar 2, 2014 by Satesh S

You can set the background of a cell using the setBackgroundColor() method and 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.



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now