要实现页眉和页脚,您需要实现一个扩展
iText API 的 PdfPageEventHelper 类的 HeaderFooter 类。然后覆盖onEndPage()
设置页眉和页脚。在此示例中,我name
在页眉中设置页眉和页脚中的“页数”。
在 pdf 创建端代码中,您需要使用HeaderAndFooter
如下类:
Document document = new Document(PageSize.LETTER);
PdfWriter writer = PdfWriter.getInstance(document, "C:\sample.pdf");
//set page event to PdfWriter instance that you use to prepare pdf
writer.setPageEvent(new HeaderAndFooter(name));
.... //Add your content to documne here and close the document at last
/*
* HeaderAndFooter class
*/
public class HeaderAndFooter extends PdfPageEventHelper {
private String name = "";
protected Phrase footer;
protected Phrase header;
/*
* Font for header and footer part.
*/
private static Font headerFont = new Font(Font.COURIER, 9,
Font.NORMAL,Color.blue);
private static Font footerFont = new Font(Font.TIMES_ROMAN, 9,
Font.BOLD,Color.blue);
/*
* constructor
*/
public HeaderAndFooter(String name) {
super();
this.name = name;
header = new Phrase("***** Header *****");
footer = new Phrase("**** Footer ****");
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
//header content
String headerContent = "Name: " +name;
//header content
String footerContent = headerContent;
/*
* Header
*/
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(headerContent,headerFont),
document.leftMargin() - 1, document.top() + 30, 0);
/*
* Foooter
*/
ColumnText.showTextAligned(cb, Element.ALIGN_RIGHT, new Phrase(String.format(" %d ",
writer.getPageNumber()),footerFont),
document.right() - 2 , document.bottom() - 20, 0);
}
}
希望能帮助到你。我曾在其中一个联名中使用过这个。