Insert Pages into a PDF Document
We can insert selected pages from a PDF document into another PDF document at any location.
Please remember that Insertion and Appending are different from each other. In appending, the pages are simply added to a PDF file at the end where as insertion allows developers to insert pages to a PDF document at any location of the document.
PdfFileEditor class is equipped with Insert method that does this job. Insert method takes input stream (inStream1) holding the PDF file from where to select pages, an integer that stores the page number (location) of the input stream (inStream2) of PDF document as the location where the selected pages from inStream1 are to be inserted, another input stream (inStream2) to which pages are inserted, an Array of integers (pages) having the selected page numbers of the PDF document and an output stream (outputStream) that will be created finally as a new PDF document having the modified version of inStream2 with inserted pages.
We can insert selected pages from a PDF document into another PDF document at any location.
Please remember that Insertion and Appending are different from each other. In appending, the pages are simply added to a PDF file at the end where as insertion allows developers to insert pages to a PDF document at any location of the document.
PdfFileEditor class is equipped with Insert method that does this job. Insert method takes input stream (inStream1) holding the PDF file from where to select pages, an integer that stores the page number (location) of the input stream (inStream2) of PDF document as the location where the selected pages from inStream1 are to be inserted, another input stream (inStream2) to which pages are inserted, an Array of integers (pages) having the selected page numbers of the PDF document and an output stream (outputStream) that will be created finally as a new PDF document having the modified version of inStream2 with inserted pages.
//Initialize the string variables storing paths of PDF files String inFile = "example1.pdf"; String portFile = "example2.pdf"; String outFile = "kitOut.pdf"; //Setting the page number of the inStream2 as location where pages will be inserted int location = 1; //Creating an array of Page Numbers to be inserted int[] pages = new int[] { 1, 2, 3, 8 }; PdfFileEditor pdfEditor = new PdfFileEditor(); //Calling Insert method pdfEditor.insert(inFile, location, portFile, pages, outFile);
There are three more overloaded methods of Insert that can be used by develpers to perform their tasks. Please check them out by yourself.
