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

Category: 
Tags: tablesposition tableiText 5

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.

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