如何在pdf文件中添加页脚和页眉。
这是我需要添加到页脚的数据
我尝试过的示例代码:(仍处于早期阶段)
class Hello extends PdfPageEventHelper {
public void onStartPage(PdfWriter pdfWriter, Document document) {
System.out.println("onStartPage() method > Writing header in file");
Rectangle rect = pdfWriter.getBoxSize("rectangle");
// TOP LEFT
ColumnText.showTextAligned(pdfWriter.getDirectContent(),
Element.ALIGN_CENTER, new Phrase("TOP LEFT"), rect.getLeft(),
rect.getTop(), 0);
// TOP MEDIUM
ColumnText.showTextAligned(pdfWriter.getDirectContent(),
Element.ALIGN_CENTER, new Phrase("TOP MEDIUM"),
rect.getRight() / 2, rect.getTop(), 0);
// TOP RIGHT
ColumnText.showTextAligned(pdfWriter.getDirectContent(),
Element.ALIGN_CENTER, new Phrase("TOP RIGHT"), rect.getRight(),
rect.getTop(), 0);
}
public void onEndPage(PdfWriter pdfWriter, Document document) {
System.out.println("onEndPage() method > Writing footer in file");
Rectangle rect = pdfWriter.getBoxSize("rectangle");
// BOTTOM LEFT
/* ColumnText.showTextAligned(pdfWriter.getDirectContent(),
Element.ALIGN_CENTER, new Phrase("BOTTOM LEFT"),
rect.getLeft()+15, rect.getBottom(), 0);*/
// BOTTOM MEDIUM
ColumnText.showTextAligned(pdfWriter.getDirectContent(),
Element.ALIGN_CENTER, new Phrase("Apollo Munich Health Insurance Co. Ltd."),
rect.getRight() / 2, rect.getBottom(), 0);
/* // BOTTOM RIGHT
ColumnText.showTextAligned(pdfWriter.getDirectContent(),
Element.ALIGN_CENTER, new Phrase("BOTTOM RIGHT"),
rect.getRight()-10, rect.getBottom(), 0);*/
}
}
public class TestPdf extends
PdfPageEventHelper {
public static void main(String[] args) throws DocumentException, IOException {
String pdfFilePath = "D:/Header.pdf";
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
Document document = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(document, fos);
Rectangle rectangle = new Rectangle(30, 30, 550, 800);
pdfWriter.setBoxSize("rectangle", rectangle);
Hello headerAndFooter =
new Hello();
pdfWriter.setPageEvent(headerAndFooter);
document.open();
document.add(new Paragraph("This is Header and Footer in Pdf Using Itext Example"));
document.close();
fos.close();
System.out.println("PDF created in >> " + pdfFilePath);
}
}
我尝试将文本添加到页脚。但是我做不到。在需要与图像一起放置的页眉和页脚文本下方
标题: