I used Bruno's example (https://developers.itextpdf.com/examples/form-examples/clone-create-fields-table) to add form fields to tables in a newly created document. The example itself works without any problems, but if I want to create 50 tables I get the following exception:
Exception in thread "main" java.lang.NullPointerException
at com.itextpdf.kernel.pdf.PdfDictionary.get(PdfDictionary.java:552)
at com.itextpdf.kernel.pdf.PdfDictionary.getAsArray(PdfDictionary.java:156)
at com.itextpdf.kernel.pdf.PdfPage.getAnnotations(PdfPage.java:743)
at com.itextpdf.kernel.pdf.annot.PdfAnnotation.getPage(PdfAnnotation.java:427)
at com.itextpdf.forms.fields.PdfFormField.regenerateField(PdfFormField.java:1781)
at com.itextpdf.forms.fields.PdfFormField.setValue(PdfFormField.java:1043)
at com.itextpdf.forms.fields.PdfFormField.setValue(PdfFormField.java:1004)
at com.itextpdf.forms.fields.PdfFormField.setValue(PdfFormField.java:999)
at com.itextpdf.forms.fields.PdfFormField.createText(PdfFormField.java:469)
at com.itextpdf.forms.fields.PdfFormField.createText(PdfFormField.java:410)
at com.itextpdf.forms.fields.PdfFormField.createText(PdfFormField.java:391)
at CreateFormInTable$MyCellRenderer.draw(CreateFormInTable.java:62)
at com.itextpdf.layout.renderer.TableRenderer.drawChildren(TableRenderer.java:1023)
at com.itextpdf.layout.renderer.AbstractRenderer.draw(AbstractRenderer.java:458)
at com.itextpdf.layout.renderer.TableRenderer.draw(TableRenderer.java:948)
at com.itextpdf.layout.renderer.DocumentRenderer.flushSingleRenderer(DocumentRenderer.java:138)
at com.itextpdf.layout.renderer.RootRenderer.processRenderer(RootRenderer.java:349)
at com.itextpdf.layout.renderer.RootRenderer.shrinkCurrentAreaAndProcessRenderer(RootRenderer.java:338)
at com.itextpdf.layout.renderer.RootRenderer.addChild(RootRenderer.java:236)
at com.itextpdf.layout.RootElement.add(RootElement.java:109)
at com.itextpdf.layout.Document.add(Document.java:143)
at CreateFormInTable.manipulatePdf(CreateFormInTable.java:45)
at CreateFormInTable.main(CreateFormInTable.java:25)
I use Java 1.8.0_121 on Windows 8 Enterprise.
The modified manipulatePdf
method looks like this:
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
for (int i = 0; i < 50; i++) {
System.out.println(i);
Table table = new Table(2);
Cell cell;
cell = new Cell().add("Name:");
table.addCell(cell);
cell = new Cell();
cell.setNextRenderer(new MyCellRenderer(cell, "name" + i));
table.addCell(cell);
cell = new Cell().add("Address");
table.addCell(cell);
cell = new Cell();
cell.setNextRenderer(new MyCellRenderer(cell, "address" + i));
table.addCell(cell);
doc.add(table);
}
doc.close();
}
The complete example that produces the NPE can be found here: https://github.com/trettstadtnlb/itext-table-form-fields
Has anyone experienced this kind of behaviour or can confirm that this is a bug?