java 在 iText 中将 PDFPTable 添加到页面底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12796525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a PDFPTable to bottom of page in iText
提问by Batman
I'm trying to add a table as a footer containing all the copyright text, page number etc. But I can't find any supporting method that'll accept a PdfPTable
我正在尝试添加一个表格作为包含所有版权文本、页码等的页脚。但我找不到任何支持方法 PdfPTable
For a phrase there is code like:
对于一个短语,有如下代码:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_CENTER, new Phrase(
String.format("%d", document.getPageNumber())),
(document.getPageSize().getLeft() + document.getPageSize().getRight())/2,
document.getPageSize().getBottom() + 18, 0);
回答by Bruno Lowagie
The PdfPTable
class has a method writeSelectedRows()
that can be used to add (a selection of columns and) rows at an absolute position.
的PdfPTable
类有一个方法writeSelectedRows()
可以被用于添加在绝对位置(选择列和的)行。
Examples:
例子:
- http://itextpdf.com/examples/iia.php?id=89adds rows at an absolute position.
- http://itextpdf.com/examples/iia.php?id=90adds a selection of columns/rows at an absolute position.
- http://itextpdf.com/examples/iia.php?id=91an alternative solution where you wrap a table in a
ColumnText
object.
- http://itextpdf.com/examples/iia.php?id=89在绝对位置添加行。
- http://itextpdf.com/examples/iia.php?id=90在绝对位置添加了一系列列/行。
- http://itextpdf.com/examples/iia.php?id=91一种替代解决方案,您可以将表格包装在一个
ColumnText
对象中。
回答by user1361991
The examples posted by Bruno are a good pointer, here's an example without magic numbers:
Bruno 发布的示例是一个很好的指针,这是一个没有魔术数字的示例:
private void writeFooterTable(PdfWriter writer, Document document, PdfPTable table) {
final int FIRST_ROW = 0;
final int LAST_ROW = -1;
//Table must have absolute width set.
if(table.getTotalWidth()==0)
table.setTotalWidth((document.right()-document.left())*table.getWidthPercentage()/100f);
table.writeSelectedRows(FIRST_ROW, LAST_ROW, document.left(), document.bottom()+table.getTotalHeight(),writer.getDirectContent());
}
This will write the PdfPTable within the document margins at the bottom overlapping any text you have at the bottom. If you wish to write the table in the margin, use: document.bottom()
instead of document.bottom()+table.getTotalHeight()
.
这将在底部的文档边距内写入 PdfPTable,与底部的任何文本重叠。如果您想在页边空白处写表格,请使用:document.bottom()
而不是document.bottom()+table.getTotalHeight()
。
As a relevant note if you're following the example on this link, the "art" box does not appear to be required and the magic numbers 36, 54, 559, 788 correspond to:
作为相关说明,如果您遵循此链接上的示例,则“艺术”框似乎不是必需的,幻数 36、54、559、788 对应于:
document.left(), document.bottom(), document.right(), document.top()
回答by VahidN
To implement a custom footer you need to implement the PdfPageEventHelper.
要实现自定义页脚,您需要实现PdfPageEventHelper。