'Cannot get footer table to be entire width of PDF in iText Java
I am using iText PDF version 5 in Java. I have a table at the bottom of my page that I need to be the entire width of the page up to the margins of the page. Here is my document code with my margins:
Document document = new Document(PageSize.LETTER, 40, 40, 36, 40);
I have other tables on the top and the middle of the page:
PdfPTable headerTable = new PdfPTable(2);
headerTable.setWidthPercentage(100);
headerTable.addCell("Welcome");
headerTable.addCell("Contact Us");
document.add(headerTable);
PdfPTable itemTable = new PdfPTable(3);
itemTable.setWidthPercentage(100);
itemTable.addCell("item name: coding shirt");
itemTable.addCell("item size: medium");
itemTable.addCell("item cost: $10.00");
document.add(itemTable);
I have a table that I am displaying at the bottom of the page:
PdfPTable grandFooterTable = new PdfPTable(1);
grandFooterTable.addCell("Some information here");
grandFooterTable.addCell("Some more information as well");
grandFooterTable.setTotalWidth(document.right(document.rightMargin()) - document.left(document.leftMargin()));
grandFooterTable.setWidthPercentage(100);
grandFooterTable.writeSelectedRows(
0,
-1,
document.left(document.leftMargin()),
grandFooterTable.getTotalHeight() + document.bottom(document.bottomMargin()),
writer.getDirectContent()
);
For some reason this table is not the entire width of the page up to the margins. It is not as wide as the other tables above it.
I've tried:
- removing/commenting out
grandFooterTable.setWidthPercentage(100);
- changing
setTotalWidth()
tograndFooterTable.setTotalWidth(100);
(I know that is not a percentage parameter but I tried anyway) - moving
grandFooterTable.setWidthPercentage(100);
to different places: beforesetTotalWidth
, afterwriteSelectedRows
None of the above works. How can I get the footer table to be as wide as the other tables on my page, so that the width hits the margins?
Solution 1:[1]
You really should try iText7.
The width is set via setWidth(UnitValue.createPercentValue(100)). So, the table will take up the entire possible width. And using setFixedPosition(), it can be placed at the bottom
PdfDocument pdfDocument = new PdfDocument(new PdfWriter("file.pdf"));
Document document = new Document(pdfDocument, PageSize.LETTER);
document.setMargins(40, 40, 36, 40);
Table headerTable = new Table(2);
headerTable.setWidth(UnitValue.createPercentValue(100));
headerTable.addCell("Welcome");
headerTable.addCell("Contact Us");
document.add(headerTable);
Table itemTable = new Table(3);
itemTable.setWidth(UnitValue.createPercentValue(100));
itemTable.addCell("item name: coding shirt");
itemTable.addCell("item size: medium");
itemTable.addCell("item cost: $10.00");
document.add(itemTable);
Table grandFooterTable = new Table(1);
grandFooterTable.addCell("Some information here");
grandFooterTable.addCell("Some more information as well");
grandFooterTable.setWidth(UnitValue.createPercentValue(100));
PageSize ps = pdfDocument.getDefaultPageSize();
grandFooterTable.setFixedPosition(document.getLeftMargin(), document.getBottomMargin(),
ps.getWidth() - document.getLeftMargin() - document.getRightMargin());
document.add(grandFooterTable);
document.close();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Zhenya Prudnikov |