This is official iText solution for creating a table on the last page at the bottom in pdf document. This solution puts my table at the bottom of the last pdf page. Great.
Unfortunately, it causes getting my table more narrow too. And this is what I don't want. I have tried several hours to get that table wider again, but without success. I cannot resolve it. How to put table at the bottom in original size before moving? What is the best solution of this problem?
Picture of the problem
Before moving, width of my table was created only based on table.setWidthPercentage(100); Then it started to report exception that width of the table must be greater than zero.
table.setWidths(number of columns in my table);
I tried table.setTotalWidth() set on different value than zero and then overwrite it with that official code from iText. But without success. I am looking for some elegant solution of this.
The code:
public static void main(String[] args)
{
Document document = new Document(PageSize.A4);
PdfWriter writer = null;
try {
writer = PdfWriter.getInstance(document,
new FileOutputStream("C:/radek-folder/calendar.pdf"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
document.open();
PdfPTable datatable = createHeaderTable();
document.add(datatable);
datatable = createFooterTable();
drawTableAtTheEndOfPage(document, writer, datatable);
document.close();
System.out.println("done");
}
private static void drawTableAtTheEndOfPage(Document document, PdfWriter writer, PdfPTable datatable) {
datatable.setTotalWidth(document.right(document.rightMargin()) - document.left(document.leftMargin()));
datatable.writeSelectedRows(0, -1, document.left(document.leftMargin()),
datatable.getTotalHeight() + document.bottom(document.bottomMargin()),
writer.getDirectContent());
}
private static PdfPTable createFooterTable() throws DocumentException {
int[] columnWidths = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 };
PdfPTable datatable = new PdfPTable(columnWidths.length);
datatable.setKeepTogether(true);
datatable.setWidthPercentage(100);
datatable.setWidths(columnWidths);
datatable.getDefaultCell().setPadding(5);
datatable.getDefaultCell().setHorizontalAlignment(horizontalAlignment);
datatable.getDefaultCell().setVerticalAlignment(verticalAlignment);
for (int i = 0; i < 100; i++) {
addCellToTable(datatable, horizontalAlignmentLeft,
verticalAlignmentMiddle, "Přehledová tabulka", columnWidths.length, 1,
fontTypeBold, fontSizeRegular, cellLayout_Bottom);
}
return datatable;
}