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-font-family:Calibri;color:#333333;mso-ansi-language:EN-US;
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.

[Java]

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

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”);