I am using iText7 to create a document which has multiple sections. Some of the sections are to be formatted normally, but some are to be formatted into columns. I can get it to format properly into columns by using the ColumnDocumentRenderer object, but when I do so, the entire document is set to use columns. Is there any way to have iText swap which renderer to use on the fly?
When I try to swap out renderers on the fly, I get a null pointer exception (com.itextpdf.kernel.pdf.PdfDictionary.get(PdfDictionary.java:482)).
PdfDocument pdf = new PdfDocument(new PdfWriter(targetFile));
Document document = new Document(pdf);
DocumentRenderer defRender = new DocumentRenderer(document);
document.setRenderer(defRender);
ColumnDocumentRenderer dictRender = getColumnRender();
while (<CONDITION>) {
document.setRenderer(dictRender);
document.add(new Paragraph("THIS IS NORMAL TEXT"));
document.add(new Paragraph("THIS IS NORMAL TEXT"));
document.add(new Paragraph("THIS IS NORMAL TEXT"));
<...>
document.setRenderer(defRender);
document.add(new Paragraph("THIS IS COLUMN TEXT"));
document.add(new Paragraph("THIS IS COLUMN TEXT"));
document.add(new Paragraph("THIS IS COLUMN TEXT"));
<...>
}
After I set the renderer to dictRender, the first document.add() statement throws a null pointer error at com.itextpdf.kernel.pdf.PdfDictionary.get(PdfDictionary.java:482).
I don't want to have to create multiple different PDF files, but am thinking that might be what I end up having to do. Thanks for any help here.