您报告的问题无法重现。我以您的示例为例,并使用此事件创建了TextFooter示例:
class MyFooter extends PdfPageEventHelper { Font ffont = new Font(Font.FontFamily.UNDEFINED, 5, Font.ITALIC); public void onEndPage(PdfWriter writer, document document) { PdfContentByte cb = writer.getDirectContent(); Phrase header = new Phrase("this is a header", ffont); Phrase footer = new Phrase("this is a footer", ffont); ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, header, (document.right() - document.left()) / 2 + document.leftMargin(), document.top() + 10, 0); ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, footer, (document.right() - document.left()) / 2 + document.leftMargin(), document.bottom() - 10, 0); }}请注意,我只创建了一次
Font和
Paragraph实例,从而提高了性能。我还介绍了页脚和页眉。您声称要添加页脚,但实际上您添加了页眉。
该
top()方法使您位于页面顶部,因此也许您打算计算
y相对于页面顶部的位置
bottom()。
您的
footer()方法中还有一个错误:
private Phrase footer() { Font ffont = new Font(Font.FontFamily.UNDEFINED, 5, Font.ITALIC); Phrase p = new Phrase("this is a footer"); return p;}您定义了一个
Fontnamed
ffont,但是您不使用它。我想你打算写:
private Phrase footer() { Font ffont = new Font(Font.FontFamily.UNDEFINED, 5, Font.ITALIC); Phrase p = new Phrase("this is a footer", ffont); return p;}现在,当我们查看生成的PDF时,我们清楚地看到了作为每个页面的页眉和页脚添加的文本。



