我在我的项目中使用itextpdf生成PDF文件。目前我正面临页脚的问题,而文本并没有包装仅部分文本显示在页脚中。请注意,我正在使用“ itextpdf-5.5.6.jar”作为我的库。找到我尝试过的以下代码
class HeaderAndFooterPdfPageEventHelper extends PdfPageEventHelper {
public void onEndPage(PdfWriter pdfWriter, Document document) {
String leftfooter = "This message contains information that may be privileged or confidential and is the property of XXX. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message. Please note that any views or opinions presented in this e-mail are solely those of the author and do not necessarily represent those of XXX. We recommend that you check this e-mail and any attachments against viruses. XXX accepts no liability for any damage caused by any malicious code or virus transmitted by this e-mail";
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(leftfooter),
rect.getLeft()+0, rect.getBottom(), 0);
}
}
public class HowToSet_HeaderFooter_InPdfUsingItextExample extends
PdfPageEventHelper {
public static void main(String[] args) throws DocumentException, IOException {
String pdfFilePath = "E:/Set Header and Footer in Pdf Using Itext Example.pdf";
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
Document document = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(document, fos);
Rectangle rectangle = new Rectangle(1000, 30, 500, 1000);
pdfWriter.setBoxSize("rectangle", rectangle);
HeaderAndFooterPdfPageEventHelper headerAndFooter =
new HeaderAndFooterPdfPageEventHelper();
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);
}
}