Add header to PDF and RTF with iText
I am using iText to generate both PDF and RTFs. I know RTF generation with iText is not popular, but I still need to make the code work for both. Technically, I can have different pieces of code to handle each type, but it needs to be able to be contained within the same file/class.
I can work with code like this:
String outputData = getFieldValue(myRecord, myFieldName);
if (exportToPDF) {
iTextPdfPCell = new PdfPCell(pdfPTable.getDefaultCell());
iTextPdfPCell.setP开发者_如何学运维hrase(outputData);
pdfPTable.addCell(iTextPdfPCell);
}
if (exportToRTF) {
iTextCell = new RtfCell(outputData);
iTextTable.addCell(iTextCell);
}
I can add a header using HeaderFooter that will appear on both PDF and RTFs
Phrase headerPhrase = new Phrase ("This is a page header.");
HeaderFooter header = new HeaderFooter(headerPhrase, false);
iTextDoc.setHeader(header);
The trouble is when the header gets complex. I would like to add a header that contains a table and images (images may be within the table). I saw some code on a different forum that suggested doing this:
Phrase headerPhrase = new Phrase();
headerPhrase.add(iTextTable);
HeaderFooter header = new HeaderFooter(headerPhrase, false);
iTextDoc.setHeader(header);
While my initial testing shows that this does not cause any compile errors and technically "works", it starts the header table in the same position as the body text of the file, so they overlap. When I set the page margins wider to allow for space for the header, it just moves the header down with the body.
Most of the information I read online (and in the manual) seems to be pointing to using page events, but that means my class has to extend PdfPageEventHelper. If I do that,
1. Is there anything similar for RTF? 2. If there is, can the same class extend both the 'RtfPageEventHelper' (if it exists) and PdfPageEventHelper? I cannot write separate classes for each output type 3. If not, is there a way that I can use tables/images within the HeaderFooter class, which already works for both PDF and RTF? 4. (I don't even want to ask this) Since iText seems to have left RTF generating behind, should I be using something else to generate RTFs (preferably works with PDF as well)?I did it in this format. Is for RTF, but i think is same with PDF. Only change RtfHeader for PdfHeader
document.open();
Paragraph head=new Paragraph("Head");
Paragraph foot=new Paragraph("Foot");
HeaderFoot header=new RtfHeaderFooter(head);
HeaderFoot footer=new RtfHeaderFooter(foot);
document.setHeader(header);
document.setFooter(footer);
document.close();
I wait be help.
A header or footer with HeaderFooter is displayed on every page. If you want simply a document header just add a standard element(paragraph/etc) to the page before your content.
For instance I'm doing this with my document:
Paragraph Header = new Paragraph("Document Header", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 0)));
document.open();
document.add(Header);
document.add(otherContent);
document.close();
精彩评论