0

I have two Adobe Life cycle designed forms "F1_0.pdf" and "F1_1.pdf". Both form has some form data.

I am merging them using below code(itextsharp v5.1.2). It merged them into resultant file "Merged_Final.pdf" in well manner. My issue is: Merged_Final.pdf file only have the data of 2nd pdf i.e. of F1_1.pdf. F1_0.pdf file's data are gone. Here is the pdf file attachment. So, how can I over come from this issue.

public void CombineMultiplePDFs(string[] fileNames, string outFile)
    {
        // step 1: creation of a document-object
        Document document = new Document();

        // step 2: we create a writer that listens to the document
        PdfCopy writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));
        if (writer == null)
        {
            return;
        }

        // step 3: we open the document
        document.Open();

        foreach (string fileName in fileNames)
        {
            // we create a reader for a certain document
            PdfReader reader = new PdfReader(fileName);
            reader.ConsolidateNamedDestinations();

            // step 4: we add content
            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                PdfImportedPage page = writer.GetImportedPage(reader, i);
                writer.AddPage(page);
            }

            PRAcroForm form = reader.AcroForm;
            if (form != null)
            {
                writer.CopyAcroForm(reader);
            }

            reader.Close();
        }

        // step 5: we close the document and writer
        writer.Close();
        document.Close();
    }
Avijit
  • 1,219
  • 2
  • 15
  • 28

1 Answers1

1

Maybe you are trying to do something that is impossible:

If your forms are pure XFA forms (as in: dynamic, not hybrid, consisting of nothing but XML), then you are trying to do something that is impossible: both forms probably have a different data description (a different .xsd) and no software program (not even Adobe LiveCycle Designer) can merge two incompatible types of XML.

If your forms are pure XFA forms, your only options are:

  • to fill them first, flatten them (= remove all interactivity) and then merge them.
  • to create a new XSD that combines the data descriptions of both forms and to manually create a new form in Adobe LiveCycle Designer using the new XSD as data source.

Maybe you are trying to do something that is possible:

If your forms are hybrid forms (as in: they also contain a description of the form based on AcroForm technology), you should remove the XFA part and merge the PDFs as pure AcroForm documents. How to do this, is explained in my answer to the following question: Whats the alternative to copyAcroForm?

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • At first glance the OP's sample files look like hybrid-form PDFs. Thus, dropping the XFA part seems an option worth investigating. – mkl Dec 08 '14 at 09:00
  • In that case, the question can probably be marked as duplicate ;-) – Bruno Lowagie Dec 08 '14 at 09:05
  • @BrunoLowagie: So, what I did is: took my above two pdfs and use the code from your link to merge. But after merging I haven't find any data for both the forms. Is XFA removal part is covered in that link or am I missing some thing? Im new to iText/iTextSharp. – Avijit Dec 08 '14 at 19:33
  • @BrunoLowagie: I have one more question at [link](http://stackoverflow.com/questions/27325577/itextsharp-need-to-repeat-table-rows-with-textfield-dynamically). It would be very helpful for me if you have any view. – Avijit Dec 08 '14 at 20:29
  • 1
    In your other question, you seem to have a dynamic XFA form without an AcroForm counter-part (or at least you use code that assumes this). If this is true, you are trying to do something that is impossible. Your question has been unclear from the start. I would take a step back and study the problem before starting to code if I were you. Before you spend any more of your employer's time, you should assess what it is that you're trying to do. Is it possible? Or is it impossible? It's hard to tell from a distance. – Bruno Lowagie Dec 09 '14 at 07:15