How to add a table to the bottom of the last page?

I have created table in a PDF document using iText. This works fine, but I don't want to add the table at the current pointer in the page, I want to add the table at the bottom.

24th October 2015
admin-marketing

 

PdfPTable datatablebottom = new PdfPTable(8);
PdfPCell cell = new PdfPCell();
// add cells here
datatablebottom.setTotalWidth(PageSize.A4.getWidth()-70);
datatablebottom.setLockedWidth(true);
document.add(datatablebottom);
Posted on StackOverflow on Mar 9, 2014 by user3434594

You need to define the absolute width of your table:

datatable.setTotalWidth(document.right(document.rightMargin())
    - document.left(document.leftMargin()));

Then you need to replace the line:

document.add(datatablebottom);

with this one:

datatable.writeSelectedRows(0, -1,
    document.left(document.leftMargin()),
    datatable.getTotalHeight() + document.bottom(document.bottomMargin()),
    writer.getDirectContent());

The writeSelectedRows() method draws the table at an absolute position. We calculate that position by asking the document for its left margin (the x value) and by adding the height of the table to the bottom margin of the document (the Y coordinate). We draw all rows (0 to -1).


Share this article

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