Convert XFA to AcroForm and Merge PDF files using Java with Aspose.PDF

Aspose-Pdf-9.3.0 for Java

I am attempting to merge the contents of 2 PDF files. These PDFs were created using Adobe Lifecycle designer and contain dynamic forms, and so have been saved as a Dynamic PDFs.

Using the following code taken from your example:

com.aspose.pdf.Document pdfDocument1 = new com.aspose.pdf.Document(myDir + "someDoc1.pdf");
com.aspose.pdf.Document pdfDocument2 = new com.aspose.pdf.Document(myDir + "someDoc2.pdf");
com.aspose.pdf.Document pdfDocument3 = new com.aspose.pdf.Document();

pdfDocument3.getPages().add(pdfDocument1.getPages());
pdfDocument3.getPages().add(pdfDocument2.getPages());
pdfDocument3.save(myDir + "Merged.pdf");
pdfDocument3.dispose();

I can successfully merge the contents of two Static PDFs containing Forms, but this does not work as soon as you try to use a PDF that includes a Dynamic Form. If using a Dynamic form and trying to view the merged PDF in Adobe Reader, you will only ever see this message: "If this message is not eventually replaced by the proper contents of the document, your PDF viewer may not be able to display this type of document."

Is this just a limitation of the toolkit? Should I be / is there a way to convert the Dynamic PDFs to a Static representation before I attempt this merge? I am attaching a sample Dynamic PDF for reference. Thank You.

Hi Brian,


Thanks for using our API’s.

In order to merge/concatenate Dynamic XFA forms, you need to first convert it to Standard Acro Form and then try merging the documents. Please take a look over following code snippet to first convert Dynamic XFA form to Standard Acro Form and then try merging the documents. For your reference, I have also attached the resultant PDF generated over my end.

Please note that I have used Aspose.Pdf for Java 9.5.2 for testing purposes.

[Java]

com.aspose.pdf.Document
pdfDocument1 =
new com.aspose.pdf.Document(“c:/pdftest/Dynamic.pdf”);<o:p></o:p>

// convert XFA form to Standard Acro Form

pdfDocument1.getForm().setType(com.aspose.pdf.FormType.Standard);

com.aspose.pdf.Document pdfDocument2 = new com.aspose.pdf.Document("c:/pdftest/Dynamic - Copy.pdf");

pdfDocument2.getForm().setType(com.aspose.pdf.FormType.Standard);

com.aspose.pdf.Document pdfDocument3 = new com.aspose.pdf.Document();

pdfDocument3.getPages().add(pdfDocument1.getPages());

pdfDocument3.getPages().add(pdfDocument2.getPages());

pdfDocument3.save("c:/pdftest/Dynamic_Merged.pdf");

pdfDocument3.dispose();

Thank you for the feedback. This code does indeed allow us to merge these dynamic documents. I do have one follow up question though. If any of the dynamic documents had any fields filled out within their forms, I noticed that once I merge them, the information in the fields that were populated is lost, and does not appear in the merged document.


I am assuming this might have to do with the fact the field values are stored within the XFA data, and is lost once we convert the forms to Static/Standard. Is there any way to preserve the field values when down-converting from Dynamic to Static?

Thanks again.

Hi Brian,


Thanks for sharing the feedback.

I have tried entering the information in one of the XFA form so that I can use it for testing purposes, but when I have tried saving the filled form, I am getting a message that data type into this form cannot be saved. Adobe Reader can only save a blank copy of this form. It appears that Extended Rights Features are not enabled over this form. Can you please share the resource files which you are using (filled forms) so that we can test the scenario in our environment.

Sorry about that, I am attaching a document that contains Dynamic XFA forms with some fields filled out and some radio buttons selected. Thank you.

Hi Brian,


Thanks for sharing the resource file.

I have tested the scenario and I am able to
notice that
the value of Salary TextField is lost and one of the value is selected for Date Status and License Type radio buttons. For the sake of correction, I have logged this problem
as PDFNEWJAVA-34551 in our issue tracking system. We will further
look into the details of this problem and will keep you updated on the status
of correction. Please be patient and spare us little time. We are sorry for
this inconvenience.

Thanks for the feedback. For your information, it would appear I can sucessfully down-convert the document by setting the form type to Static:


setType(com.aspose.pdf.FormType.Static)

And all the fields appear to be set correctly.

using Standard:

setType(com.aspose.pdf.FormType.Standard)

Some of the fields are lost, and some values are incorrectly populated as you noted in your testing.

It would appear we can accomplish the merging of Dynamic PDFs by first down-converting by setting the forms to Static instead of Standard, then merging the resultant documents by adding their pages to a new PDF. Thanks for the help.

Hi Brian,


We are glad to hear that your problem can be resolved by setting form type as Static. Please continue using our API and in the event of any further query, please feel free to contact.

The issues you have found earlier (filed as PDFJAVA-34551) have been fixed in Aspose.Pdf for Java 17.3.0 Release Notes.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.