/* * This class is part of the book "iText in Action - 2nd Edition" * written by Bruno Lowagie (ISBN: 9781935182610) * For more info, go to: http://itextpdf.com/examples/ * This example only works with the AGPL version of iText. */ package part2.chapter06; import java.io.FileOutputStream; import java.io.IOException; import java.sql.SQLException; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfCopy; import com.itextpdf.text.pdf.PdfReader; import part1.chapter02.MovieHistory; import part1.chapter02.MovieLinks1; public class Concatenate { /** The resulting PDF file. */ public static final String RESULT = "results/part2/chapter06/concatenated.pdf"; /** * Main method. * @param args no arguments needed * @throws DocumentException * @throws IOException * @throws SQLException */ public static void main(String[] args) throws IOException, DocumentException, SQLException { // using previous examples to create PDFs MovieLinks1.main(args); MovieHistory.main(args); String[] files = { MovieLinks1.RESULT, MovieHistory.RESULT }; // step 1 Document document = new Document(); // step 2 PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT)); // step 3 document.open(); // step 4 PdfReader reader; int n; // loop over the documents you want to concatenate for (int i = 0; i < files.length; i++) { reader = new PdfReader(files[i]); // loop over the pages in that document n = reader.getNumberOfPages(); for (int page = 0; page < n; ) { copy.addPage(copy.getImportedPage(reader, ++page)); } copy.freeReader(reader); reader.close(); } // step 5 document.close(); } }