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 (JAVA Version) class is equipped with Insert (JAVA Version) method that does this job. Insert (JAVA Version) 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.
Code Snippet
[C#]
//Initialize the string variables storing paths of PDF files
string inFile1 = ".\\example1.pdf";
string inFile2 = ".\\example2.pdf";
string outFile = ".\\InsertStream.pdf";
//Creating stream objects holding the PDF files in Open Mode
FileStream inStream1 = new FileStream(inFile1,FileMode.Open);
FileStream inStream2 = new FileStream(inFile2,FileMode.Open);
//Creating output stream object that will store the extracted pages as a PDF file
FileStream outputStream = new FileStream(outFile,FileMode.Create);
//Instantiating PdfFileEditor object
PdfFileEditor editor = new PdfFileEditor();
//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};
//Calling Insert method
editor.Insert(inStream1,location,inStream2,pages,outputStream);
//Closing output stream
outputStream.Close();
[VB.NET]
'Initialize the string variables storing paths of PDF files
Dim inFile1 As String = ".\\example1.pdf"
Dim inFile2 As String = ".\\example2.pdf"
Dim outFile As String = ".\\InsertStream.pdf"
'Creating stream objects holding the PDF files in Open Mode
Dim inStream1 As FileStream = New FileStream(inFile1,FileMode.Open)
Dim inStream2 As FileStream = New FileStream(inFile2,FileMode.Open)
'Creating output stream object that will store the extracted pages as a PDF file
Dim outputStream As FileStream = New FileStream(outFile,FileMode.Create)
'Instantiating PdfFileEditor object
Dim editor As PdfFileEditor = New PdfFileEditor()
'Setting the page number of the inStream2 as location where pages will be inserted
Dim location As Integer = 1
'Creating an array of Page Numbers to be inserted
Dim pages() As Integer = New Integer() {1,2,3,8}
'Calling Insert method
editor.Insert(inStream1,location,inStream2,pages,outputStream)
'Closing output stream
outputStream.Close()
[JAVA]
//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 (JAVA Version ) that can be used by develpers to perform their tasks. Please check them out by yourself.