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:
- dynamic XFA forms; forms created with Adobe LiveCycle Designer
- How do you flatten a dynamic XFA form?
- iText Demo: Dynamic XFA forms in PDF
Demo: XFA to PDF (Bruno Lowagie's Online Gazette)
- XfaMovie Java example
- 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