We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Field.flatten for newly created fields

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.

<span style=“font-size:10.0pt;line-height:115%;font-family:“Arial”,“sans-serif”;
mso-fareast-language:EN-US;mso-bidi-language:AR-SA”>Best Regards,

Hi John,

In addition to above reply, as a workaround you may save your form to stream, reopen using Document() object and flatten the form fields. It will help you to accomplish the task until above logged issue is resolved. We will notify you as soon as it is resolved.

Best Regards,

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.


Document pdf = new Document();<o:p></o:p>


TextBoxField field1 = new TextBoxField(pdf, new Rectangle(0, 0, 100, 100));


field1.setValue("FIELD1 VALUE");

pdf.getForm().add(field1, 1);

ByteArrayOutputStream stream = new ByteArrayOutputStream();


Document pdf2 = new Document(new ByteArrayInputStream(stream.toByteArray()));

for (Field field : pdf2.getForm().getFields()) {



java.io.OutputStream out = new FileOutputStream(myDir+"output.pdf");


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:

Document docNew = new Document();
Page page = docNew.getPages().add();

FloatingBox box = new FloatingBox(100, 100);
box.setTop(page.getPageInfo().getHeight() - 100);

box.getParagraphs().add(new TextFragment(“FIELD1 VALUE”));