无法在 iText Java 中将页脚表设置为 PDF 的整个宽度
我正在 Java 中使用 iText PDF 版本 5。我的页面底部有一个表格,我需要将其设置为页面的整个宽度,直到页面边缘。这是我的带有边距的文档代码:
Document document = new Document(PageSize.LETTER, 40, 40, 36, 40);
我在页面的顶部和中间还有其他表格:
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);
我有一个在页面底部显示的表格:
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()
);
由于某种原因,该表格不是页面的整个宽度翻页到页边距。它不像上面的其他桌子那么宽。
我尝试过:
- 删除/注释
grandFooterTable.setWidthPercentage(100);
- 将
setTotalWidth()
更改为grandFooterTable.setTotalWidth(100);
(我知道这不是百分比参数,但我还是尝试了) - 将
grandFooterTable.setWidthPercentage(100);
移动到不同的位置: 在setTotalWidth
之前,在writeSelectedRows
之后,
以上都不起作用。如何使页脚表格与页面上的其他表格一样宽,以便宽度达到边距?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你真的应该尝试 iText7。
宽度通过 setWidth(UnitValue.createPercentValue(100)) 设置。因此,表格将占据整个可能的宽度。并且使用setFixedPosition(),可以将其放置在底部
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