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.

 

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

Let’s create a Table instance in iText 7:

Table table = new Table(1);
Cell cell;
for (int i = 0; i 

To place it at the bottom of the page you’ll need the setFixedPosition(float x, float y, float width) method:

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
PageSize ps = pdfDoc.getDefaultPageSize();
Document doc = new Document(pdfDoc);
table.setFixedPosition(doc.getLeftMargin(), doc.getBottomMargin(), ps.getWidth() - doc.getLeftMargin() - doc.getRightMargin());
doc.add(table);
doc.close();

Here it is! The x and y coordinates define the position of the bottom-left table’s corner.

You may also want to know the total height of the table, in this case you would need to manipulate with table renderer a bit:

IRenderer tableRenderer = table.createRendererSubTree().setParent(doc.getRenderer());
LayoutResult tableLayoutResult =
        tableRenderer.layout(new LayoutContext(new LayoutArea(0, new Rectangle(ps.getWidth(), 1000))));
float totalHeight = tableLayoutResult.getOccupiedArea().getBBox().getHeight();

Click this link if you want to see how to answer this question in iText 5.



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