Form values not visible after flattenAnnotations(false)

Hi,

when filling out a PDF form using WebViewer 3.1 and flatten the entire document using PDFNet Java (libPDFNetC.so.6.7.1), the values of the fields are not visible any more.

I use this code:

PDFDoc doc = new PDFDoc(inputStream); FDFDoc fdoc = FDFDoc.createFromXFDF(xfdfString); // xfdfString contains all form data doc.fdfMerge(fdoc); doc.refreshFieldAppearances(); doc.flattenAnnotations(false); // I want to flatten the entire document, not only form fields but also other annotations ByteArrayOutputStream merged = new ByteArrayOutputStream(); doc.save(merged, SDFDoc.e_linearized, null); doc.close(); merged.flush(); byte[] b = merged.toByteArray(); return new ByteArrayInputStream(b);

You can try it with the demo document attached, but it’s not only this document, no PDF form is flattened correctly.

Best regards,

Christian Gruber

OoPdfFormExample.pdf (64.2 KB)

Unfortunately, we could not reproduce the issue on our end. Using the PDF that you provided and the code that you used. We tried exporting the PDF provided into an XFDF and merged that file back to the document before flattening the annotations and exporting. The file created had the fields merged in as expected.

Is it possible for you to provide us with the corresponding XFDF as well? Additionally, could you please try using the latest version of our SDK (you can download it from the website).

Unfortunately, we cannot use the latest version of the SDK.

PDFNetException: Your AMS expired on 20170804. To upgrade to this version of PDFNet SDK, please renew your AMS subscription.

We currently use version libPDFNetC.so.6.7.1 with PDFNet.jar on Linux.

If you can confirm that the problem exists on your side when using version 6.7.1 but not with the latest version (6.8.2), we certainly are going to renew our AMS subscription.

Best regards,

Christian Gruber

This is the XFDF string:

`

<?xml version="1.0" encoding="UTF-8"?> 150 Off Red Off Yes Off Off Off Man Test Test Test Test Test 150 Off Red Off Yes Off Off Off Man Test

`

What do you mean by “We tried exporting the PDF provided into an XFDF and merged that file back to the document before flattening the annotations and exporting.” Did you use exactly the same code as I did or not?

Greets

Thank you for the XFDF String. We tried the following code on our end, but was unable to reproduce the issue:

`
try
{
PDFDoc doc = new PDFDoc(“OoPdfFormExample.pdf”);
FDFDoc fdoc = FDFDoc.createFromXFDF(“annot.xfdf”); // this xfdf is the string is what you forwarded to us
doc.fdfMerge(fdoc);
doc.refreshFieldAppearances();
doc.flattenAnnotations(false); // I want to flatten the entire document, not only form fields but also other annotations
ByteArrayOutputStream merged = new ByteArrayOutputStream();
doc.save(merged, SDFDoc.e_linearized, null);
merged.flush();
byte[] b = merged.toByteArray();
try (FileOutputStream fos = new FileOutputStream(“out.pdf”)) {
fos.write(b);
fos.close(); //There is no more need for this line since you had created the instance of “fos” inside the try. And this will automatically close the OutputStream
}

//doc.save("/vagrant/merged.pdf", SDFDoc.e_linearized, null);
doc.close();
}
catch(PDFNetException e)
{
e.printStackTrace();
System.out.println(e);
}
catch(IOException e)
{
e.printStackTrace();
System.out.println(e);
}
`

The “out.pdf” file generated contains the flattened fields as expected. Could you please let us know if we need to do anything differently? We are using 6.7.1 on our end.

Thanks.

Hi,

I finally found the root of the problem.

When we import a new document like OoPdfFormExample.pdf in our system, we first extract all annotations (including forms) and remove all annotations of the document afterwards. The extracted annotations are stored alongside the document as XFDF. When displaying the document in the WebViewer, everything gets displayed correctly, just like the annotations would be included in the PDF directly.

So the OoPdfFormExample.pdf attached previously actually is wrong because it still contains form fields. The correct document to reproduce the issue is the one attached, all annotations were removed. You basically can take any (empty) PDF you want.

The problem that I found now is following:

XFDF data (form fields) only gets merged/flattened into the document when there still are corresponding fields inside the PDF.

Since XFDF data contains all necessary annotation data, why are form fields and values not merged and flattened in the document?
Shouldn’t contain XFDF everything so that no corresponding form fields are needed?

Best regards,

Christian

OoPdfFormExample-ANNOTATIONS-REMOVED.pdf (52.2 KB)

Thank you for the file. Yes, the issue is due to the fact that the PDF has all the field annotations removed (therefore the values on the XFDF file will not be used since the corresponding widgets on the PDF are no longer there).

The information under the pdf-info tag is only used in WebViewer, and is not part of the XFDF standard (you will note that importing the this XFDF file on Adobe will not work as well).

May I ask why you are flattening the PDF and removing its form fields? Why not keep the fields and use FDFMerge() or FDFUpdate() when merging and flattening the document?

Ok, thanks for the information. I didn’t now that the XFDF itself doesn’t contain corresponding widget data, too.

The function you mentioned (FDFUpdate()) is exactly what we would need - it’s just not available in our current version. Is it new? Could I achieve the same result combining other functions of the SDK?

I’m using PDFNet.jar with libPDFNetC.so.6.7.1

Thanks for you help!

Christian

The FDFUpdate() function is a new API that is included in the 6.8 SDK. For more details, please take a look at the API from here:
https://www.pdftron.com/api/PDFNet/?topic=html/M_pdftron_PDF_PDFDoc_FDFUpdate.htm

Ok, thanks for your help! Topic is closed.