When fields created with Aspose PDF call to Field.flatten() causes NullPointerException. Sample code is attached
Hi John,
Thanks for your inquiry. I have tested your shared code and noticed the reported exception. For further investigation, I have logged an issue in our issue tracking system as PDFJAVA-36096 and also linked your request to it. We will keep you updated via this thread regarding the issue status.
We are sorry for the inconvenience caused.
Hi John,
Hi John,
Thanks for your patience.
We have further investigated earlier reported PDFJAVA-36096 and as per our observations, fields are actually created in the process of saving document so it needs to be saved and to reload document before flattening form fields. Please try using following code snippet.
[Java]
Document pdf = new Document();
pdf.getPages().insert(1);
TextBoxField field1 = new TextBoxField(pdf, new Rectangle(0, 0, 100, 100));
field1.setPartialName("FIELD1");
field1.setValue("FIELD1 VALUE");
pdf.getForm().add(field1, 1);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
pdf.save(stream);
Document pdf2 = new Document(new ByteArrayInputStream(stream.toByteArray()));
for (Field field : pdf2.getForm().getFields()) {
field.flatten();
}
java.io.OutputStream out = new FileOutputStream(myDir + "output.pdf");
pdf2.save(out);
Anyways, there is small sense to create form field and immediately transfer it to text as much easier to create the desired text directly by using the code snippet:
[Java]
Document docNew = new Document();
Page page = docNew.getPages().add();
FloatingBox box = new FloatingBox(100, 100);
box.setLeft(0);
box.setTop(page.getPageInfo().getHeight() - 100);
box.getParagraphs().add(new TextFragment(“FIELD1 VALUE”));
page.getParagraphs().add(box);
docNew.save(myDir+“output2.pdf”);