Flatten XFA Forms and Merge PDF Files using Aspose.PDF for Java - resultant file is not valid

Hello,

I am following the way provided by Aspose.pdf for Java to merge two pdf files into a single pdf file. Below concatenate() is the coding.
It works fine when merging two normal pdf files (see file1.pdf, file2.pdf and merge.pdf attached).
But the merging result will be a mess if the two files are the plain pdf files flattened by Aspose.pdf for Java from the xfa forms. (see flat_file1.pdf, flat_file2.pdf and flat_merge.pdf attached). In this case, flat_merge.pdf contains the complete flat_file1.pdf, but no data for the flat_file2.pdf in it).

Is this the limitation of the Aspose.pdf for Java?
Is there any other way to do the merge correctly?

I also tried the following way to merge the two files.
Document target = new Document(tar);
Document source = new Document(src);
target.getPages().add(source.getPages());
target.save(mergedFile);
It does NOT work either.
I also found that the footprint of the merged file is larger in this case.


public static void concatenate() {
String tar = "C:/temp/pdf/utilities/concatenate/flat_file1.pdf";
String src = "C:/temp/pdf/utilities/concatenate/flat_file2.pdf";
String mergedFile = "C:/temp/pdf/utilities/concatenate/flat_merge.pdf";

// the merged file has a smaller footprint
PdfFileEditor pe = new PdfFileEditor();
System.out.println(pe.concatenate(tar, src, mergedFile));
}

I just found one thing which might be helpful to resolve the issue.

The flattened pdf file (e.g., flat_file1.pdf) by Aspose.pdf for Java does NOT have dc:format metadata defined. Its value should be application/pdf. I tried to merge flat_file1.pdf and flat_file2.pdf using iText API. It complains that these two files are invalid pdf files. I wonder if some metadata is missing when Aspose.pdf flatten the XFA forms into a plain pdf file.

By the way, I am asked by my company to evaluate Aspose.pdf for Java to see if our requirements can be met. Your help will be highly appreciated. Thanks in advance!

Hi Jeff,


We are sorry for the delayed response. We will appreciate it if you please share your source XFA forms here, we will test the scenario and will update you accordingly.

Best Regards,

Attached are the two XFA forms.


The following method is used to flatten XFA forms into plain pdf file.

public static void flattenOne(String fileIn, String fileOut) {
Form pdfForm = new Form(fileIn);
pdfForm.flattenAllFields();
pdfForm.save(fileOut);
}

This is the most critical requirement to be met in order to choose Aspose.pdf for Java as our new pdf framework.

Thanks.

Jeff


Hi Jeff,

Thanks for sharing the input files.

As we are familiar that contents inside XFA form are created during PDF file loading, so in order to merge XAF forms, we need to first convert XFA forms to standard AcroForm and then try concatenating these resultant files. However during my testing, I have observed that contents in resultant/merged file get garbled. For the sake of correction, I have logged it as PDFJAVA-36158 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.

[Java]

// Load first XFA form
com.aspose.pdf.Document doc1 = new com.aspose.pdf.Document("c:/pdftest/B6I.pdf");

// Convert XFA form to standard AcroForm
doc1.getForm().setType(com.aspose.pdf.FormType.Standard);

// Load second XFA form
com.aspose.pdf.Document doc2 = new com.aspose.pdf.Document("c:/pdftest/B6J.pdf");
doc2.getForm().setType(com.aspose.pdf.FormType.Standard);

// Add pages from the second form to the pages collection of the first document
doc1.getPages().add(doc2.getPages());

// Save the resultant concatenated file
doc1.save("c:/pdftest/ConcatenatedFile.pdf");

Hello,

Based on our requirement, we want to merge two flattened pdf files. We don’t want to merge two XFA forms. It seems that you went to a different direction. You may check the difference between a flattened pdf file and a normal pdf file, which is the reason to failed the merge.

Below are my observations based on my test.

  1. Aspose.pdf for Java can merge two normal pdf files perfectly. As to normal pdf files, I mean that they are created as a pdf file directly. Say, I create a .docx file and then save it as a pdf type.

  2. Aspose.pdf for Java can flatten an XFA form to a plain pdf file perfectly.

  3. Aspose.pdf for Java cannot correctly merge two flattened files which are the resultant files from #2 above.

Therefore, you may check the differences (say, metadata difference, etc.) between a normal plain pdf file and a flattened pdf file.
If there is a difference, you may fix it when flattening the XFA forms into a plain pdf file.

Thanks.

Jeff

Hi,

I double-checked with the development team about the requirement. They also need to merge the XFA forms to a single XFA form first and then flatten it to pdf or pdfa. So your approach to fix the issue is still valid.

Thanks.

Jeff

Nayyer,

Our immediate requirement is below.

Step 1: First flatten XFA forms to normal plain pdf files.

Step 2: Then merge these normal plain pdf files into a single normal plain pdf file.

Please consider this use case first when you start to find a solution.

As to step 1 above, the plain pdf files flattened from an XFA forms look fine when being checked by eyes. All the data is correct. I tried to merge them using iText, the error message shows that these are not valid pdf files. So you may need to check what is wrong with the flattened plain pdf file, maybe in the metadata level.

Thanks.

Jeff

jeffguo:
Nayyer,

Our immediate requirement is below.

Step 1: First flatten XFA forms to normal plain pdf files.

Step 2: Then merge these normal plain pdf files into a single normal plain pdf file.

Please consider this use case first when you start to find a solution.

As to step 1 above, the plain pdf files flattened from an XFA forms look fine when being checked by eyes. All the data is correct. I tried to merge them using iText, the error message shows that these are not valid pdf files. So you may need to check what is wrong with the flattened plain pdf file, maybe in the metadata level.

Hi Jeff,

Thanks for sharing the details.

In order to merge XFA to normal PDF file, first it needs to be converted to Standard Acro form and when XFA forms are converted to normal PDF file, the document appears fine but when these documents are merged, the contents are garbled. The product team will further investigate earlier reported issue and as soon as we have some definite updates regarding its resolution, we will let you know.

Nayyer,

I understand what you mean.
I try to clarify our two use cases here.

  1. Flatten an XFA form to a plain pdf file which is NOT an acro form. This can be successfully done by the flattenAllFields() method in the Form in Aspose.pdf for Java.

But I have a question here. What is the difference between a normal plain pdf file and a flattened plain pdf file? The reason to ask is that Aspose.pdf for Java can successfully merge two normal plain pdf files, but failed to merge two flattened plain pdf files.

  1. Merge multiple plain pdf files into a single plain pdf file. In this case, the input pdf files are the flattened pdf files created in the use case 1 above.

Basically, we do NOT have the use case to merge two XFA forms into a single Acro forms.

Thanks.

Jeff

Hi Jeff,

Thanks for your patience.

The plain PDF file is normal PDF document which do not contain and Form field or form fields appear as flattened (labels instead of fields) and when using following code snippet to first flatten PDF forms and then concatenating them into single resultant file, I have observed that contents/field data for second documents is missing. We will also consider this scenario during the resolution of earlier reported issue.

[Java]

com.aspose.pdf.facades.Form form1 = new com.aspose.pdf.facades.Form();
form1.bindPdf("c:/pdftest/B6I.pdf");
form1.flattenAllFields();
form1.save("c:/pdftest/B6I_Flattened.pdf");

com.aspose.pdf.facades.Form form2 = new com.aspose.pdf.facades.Form();
form2.bindPdf("c:/pdftest/B6J.pdf");
form2.flattenAllFields();
form2.save("c:/pdftest/B6J_Flattened.pdf");

com.aspose.pdf.facades.PdfFileEditor editor = new com.aspose.pdf.facades.PdfFileEditor();
editor.concatenate("c:/pdftest/B6I_Flattened.pdf", "c:/pdftest/B6J_Flattened.pdf", "c:/pdftest/Flattened_Merged.pdf");

Yes, as I reported earlier, the field data are missing in the second flattened file. This issue has the highest priority among all the issues I have reported so far. I hope you guys can resolve this issue first and as soon as possible. This issue will be a blocker for us to make a decision on purchasing Aspose.pdf for Java.

Thanks in advance!

Jeff

Hi Jeff,


Thanks for the acknowledgement.

The issue related to missing contents has already been associated with earlier reported problem and the product team will surely consider fixing it during the resolution of this problem. As soon as we have some definite updates regarding its resolution, we will let you know.

Hello,

Good morning!
Is it possible to resolve the merging file and the filling XFA form issues within this week? On Oct 3, I have to do the demo on our listed requirements. These two issues are critical.

Thanks in advance!

Jeff

Hi Jeff,


Thanks for your patience.

As we recently have noticed earlier reported issue, so its pending for review and is not yet resolved. However the product team will surely consider investigating/fixing it as per development schedule and as soon as we have some definite updates regarding its resolution, we will let you know. Please be patient and spare us little time. We are sorry for this delay and inconvenience.

for what it’s worth, i’ve also been asked to evaluate aspose pdf and ran into the same issue. i have 1 filled and flatted xfa form and appending a page to the document doesn’t work.



shinkley:
for what it’s worth, i’ve also been asked to evaluate aspose pdf and ran into the same issue. i have 1 filled and flatted xfa form and appending a page to the document doesn’t work.
Hi Sean,

Thanks for contacting support.

Aspose.Pdf is successfully being used for file concatenation by many customers. However for certain documents, the API may encounter issues. Therefore, we request you to please share the input files and the code snippet which you are using, so that we can test the scenario in our environment. We are really sorry for this inconvenience.

Hi Nayyer, I can see about creating a simple form to highlight the problem for you. However, it is the same issue as described by Jeff above. Do you have any status on resolution for his issue?


I have spent some time doing a demo with iText, and this does not have the same limitation. I am able to form fill an XFA form, flatten the form to PDF, then merge additional PDFs as needed. Because of this, I do not think it is a limitation of my source XFA form or my additional PDF documents.


Hi Sean,

Do you have a sample code to merge two XFA forms into a single XFA form using the iText Core API (Not XFA Worker API)? If so, could you share? So I can use the iText core API to merge two XFA forms and then use Aspose.pdf to flatten the merged XFA form. Thanks in advance!

Jeff