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 <= 10; i++) {
     cell = new Cell().add(new Paragraph(Integer.toString(i)));
    table.addCell(cell);
}

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.