java 使用 ITEXT 在 PDF 中显示页码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5717981/
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
Show page numbers in PDF using ITEXT
提问by ashishjmeshram
We are using Itext for generating the PDF's in our web application. The PDF's are getting generated properly.
我们正在使用 Itext 在我们的 Web 应用程序中生成 PDF。PDF 正在正确生成。
I need to show some copyright information in the footer along with page numbers. I need to show the copyright information on the left hand side at the bottom while the page numbers on the right hand side at the bottom.
我需要在页脚中显示一些版权信息以及页码。我需要在底部左侧显示版权信息,而在底部右侧显示页码。
I have gone through google and some articles, using them I could add footer at the bottom. But this footer displays only copyright at the right hand side in the bottom.
我浏览了谷歌和一些文章,使用它们我可以在底部添加页脚。但是此页脚仅在底部右侧显示版权。
How can I add page numbers to this using Itext?
如何使用 Itext 为其添加页码?
Below is the code I am using to generate the copyright information on the right hand side at the footer.
下面是我用来在页脚右侧生成版权信息的代码。
static class HeaderFooter extends PdfPageEventHelper {
public void onEndPage(PdfWriter writer, Document document) {
Rectangle rect = writer.getBoxSize("footer");
BaseFont bf_times;
try {
bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252",
false);
Font font = new Font(bf_times, 9);
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT, new Phrase("Copyright 2011",
font), (rect.getLeft() + rect.getRight()) / 2,
rect.getBottom() - 18, 0);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
As well below is the code which is used to show the page numbers. I am unable to combine both of these to show the page numbers and the copyright information at the same time.
下面是用于显示页码的代码。我无法将这两者结合起来同时显示页码和版权信息。
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_RIGHT,
new Phrase(String.format("%d", writer.getPageNumber()),
font), (rect.getLeft() + rect.getRight()) / 2,
rect.getBottom() - 18, 0);
回答by tschaible
The showTextAligned function takes the following parameters.
showTextAligned 函数采用以下参数。
public static void showTextAligned(PdfContentByte canvas,
int alignment,
Phrase phrase,
float x,
float y,
float rotation)
In your example, you are setting the x-values for both items to the same value, so you may be writing one piece of text over the other. Try adjusting your x-values to see if that helps.
在您的示例中,您将两个项目的 x 值设置为相同的值,因此您可能将一段文本写在另一段文本上。尝试调整您的 x 值,看看是否有帮助。
Specifically, try setting the page number block of code so that is aligned with the right side of your footer Rectangle.
具体来说,尝试设置页码代码块,使其与页脚矩形的右侧对齐。
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_RIGHT,
new Phrase(String.format("%d", writer.getPageNumber()),
font), rect.getRight(),
rect.getBottom() - 18, 0);
Additionally, you can use the PDFStamper to add headers and footers after the fact to a PDF document. Sometimes this can be a simpler approach. The iText in Action book has several examples of this. Here's the code sample for one of them.
此外,您可以使用 PDFStamper 在事后向 PDF 文档添加页眉和页脚。有时,这可能是一种更简单的方法。iText in Action 一书中有几个这样的例子。 这是其中之一的代码示例。