To merge multiple PDFs, you essentially want to use a PdfCopy to generate the
merged Pdf file. The output stream can be directed to a physical file or to
a BLOB for storage in the database.
You will want a FOR-LOOP for each PDF you wish to merge. Use a PdfReader to
read each PDF you wish to merge. For the dynamic one, you will use the same
principle, but keep in-mind the order you copy them is the order they will
appear in the merged document.
The book iText In-Action does cover this topic and there are examples on the
iText web site. In short, your code will be similar to the following:
// for first PDF...
PdfReader reader1 = new PdfReader(filename);
Document document1 = new Document();
PdfCopy mergedPDF = new PdfCopy(document1, new
FileOutputStream(path+filename));
document1.open();
for (int x = 0; x < reader1.getNumberOfPages(); ) {
x++;
mergedPDF.addPage(mergedPDF.getImportedPage(reader1, x));
}// end for loop
document1.close();
// for second PDF, keep original PdfCopy open until all files are merged...
PdfReader reader2 = new PdfReader(filename);
Document document2 = new Document();
PdfCopy mergedPDF = new PdfCopy(document2, new
FileOutputStream(path+filename));
document2.open();
for (int x = 0; x < reader2.getNumberOfPages(); ) {
x++;
mergedPDF.addPage(mergedPDF.getImportedPage(reader2, x));
}// end for loop
document2.close();
// continue until last PDF...
// for last PDF...
PdfReader readerLast = new PdfReader(filename);
Document documentLast = new Document();
PdfCopy mergedPDF = new PdfCopy(documentLast, new
FileOutputStream(path+filename));
document.open();
for (int x = 0; x < readerLast.getNumberOfPages(); ) {
x++;
mergedPDF.addPage(mergedPDF.getImportedPage(readerLast, x));
}// end for loop
documentLast.close();
mergedPDF.close();
Hope this helps...
--
View this message in context:
http://itext-general.2136553.n4.nabble.com/Merging-multiple-pdfs-tp2967673p2968328.html
Sent from the iText - General mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1, ECMAScript5, and DOM L2 & L3.
Spend less time writing and rewriting code and more time creating great
experiences on the web. Be a part of the beta today.
http://p.sf.net/sfu/beautyoftheweb
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php