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.setPhrase(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)?