-1

Using the iText library, I can merge static PDF files, but not dynamic ones.

How can I accomplish this task?

Edit: (collected from comments below):
I am sorry for did not explain clearly dynamic pdf means. I am writing now.
Dynamic pdf file is created by adobe livecycle designer. It calls "XFA pdf files".
Ravinder's code works for static pdf very well. But not for XFA pdf files.
They are not readable on merge(Combine).

I used this dynamic pdf files.

  1. Pdf --> turbobit.net/9rn2r3quw5gx.html
  2. Pdf --> turbobit.net/4e6q7a1ts4jw.html

How can I merge them into a single pdf file?

Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
Gunes ismail
  • 39
  • 1
  • 2

1 Answers1

1

Updated Answer:

The PDF files you have are built using XFA (XML Forms Architecture).
iText has only partial support for XFA but full for AcroForms.

You require to flatten an XFA form and then use as you require.

You can refer to various discussions on handling XFA forms at:

  1. dynamic XFA forms; forms created with Adobe LiveCycle Designer
  2. How do you flatten a dynamic XFA form?
  3. iText Demo: Dynamic XFA forms in PDF
  4. Demo: XFA to PDF (Bruno Lowagie's Online Gazette)
  5. XfaMovie Java example
  6. XFA to PDF: articles/examples on itextpdf.com

And may be more ...

The XfaMovie example would be more helpful to solve your requirement.


Original Answer:

You can use byte[] or InputStream forms of all the dynamic pdf files to construct relevant PdfReader objects and combine them to generate a single PDF file.

Here in the example I am using FileInputStream instance but you can generate ByteArrayInputStream instance from your dynamic PDF content and use it.

Example:

import com.itextpdf.text.pdf.PdfCopyFields;
import com.itextpdf.text.pdf.PdfReader;
//import com.lowagie.text.pdf.PdfCopyFields;
//import com.lowagie.text.pdf.PdfReader;

public class CombineDynamicPdfContents
{
    // throws FileNotFoundException, IOException, DocumentException
    public static void main( String ... a ) throws Exception
    {
        String fileHome = System.getProperty( "user.home" ) + "/Desktop/";

        System.out.println( "Start combine PDF files" );
        FileInputStream fis1 = new FileInputStream( fileHome + "pdf-file-1.pdf" );
        FileInputStream fis2 = new FileInputStream( fileHome + "pdf-file-2.pdf" );

        // now create pdfreaders using inputstreams of pdf contents
        PdfReader file1 = new PdfReader( fis1 );
        PdfReader file2 = new PdfReader( fis2 );

        FileOutputStream fos = new FileOutputStream( fileHome + "Pdf-Combined.pdf" );
        PdfCopyFields copy = new PdfCopyFields( fos );

        copy.addDocument( file1 );
        copy.addDocument( file2 );
        copy.close();

        System.out.println( "Done ..." );
    } // psvm( .. )
} // class CombineDynamicPdfContents
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
  • i am sorry for did not explain clearly dynamic pdf means. i am writing now. Dynamic pdf file is created by adobe livecycle designer. it calls "XFA pdf files". Your(Ravinder) codes works for static pdf very well(Thank you). but this XFA pdf files does not merged(Combined) with them. i used this dynamic pdf files. 1.Pdf--> http://turbobit.net/9rn2r3quw5gx.html 2.Pdf--> http://turbobit.net/4e6q7a1ts4jw.html Please, would you try it for me to merge? – Gunes ismail Jul 12 '12 at 17:19
  • This is alternative link for files: http://www.dosya.tc/server18/hHYsaN/Tries.rar.html – Gunes ismail Jul 12 '12 at 17:28
  • Thanks for your update @RavinderReddy but this code also doesn't work now and also the instead of dynamic PDF we are getting "Please wait...." page. – sapatelbaps Jan 11 '17 at 11:50
  • _**@[sapatelbaps](http://stackoverflow.com/users/2224590/sapatelbaps)**_: Not sure of your issue context. Please post a new question that includes the steps you followed with code snippets. – Ravinder Reddy Jan 11 '17 at 13:28