Insert a pdf page in a PDF document

Hi all,

I am using PDF kit for .NET for PDF merger.

I have a query.

I have a PDF doc with 2 pages(ABC.pdf) and a single page PDF. Now i wanted to insert this single pdf page in between the two pages of ABC.pdf.

i am using following code.

FileStream inStream1 = new FileStream(inFile1, FileMode.Open);

FileStream inStream2 = new FileStream(inFile2, FileMode.Open);

FileStream outputStream = new FileStream(outFile, FileMode.Create);

PdfFileEditor editor = new PdfFileEditor();

int location = 1; // what should be the location ??

int[] pages = new int[] { 1,2,3 }; //How to have these page numbers ??

editor.Insert(inStream1, location, inStream2, pages, outputStream);

outputStream.Close();

Any help would be higly appreciated..

Thanks,

flexuser

Hello Prashant,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for considering Aspose.

Please use the following code snippet to insert Pages from single page Pdf (SinglePage.pdf) into 2 pages (ABC.Pdf) file. As per your request following code will inset 1 page from SinglePage.Pdf in-between the 2 pages of ABC.Pdf.

string inFile1 = "SinglePage.pdf"; //Single Page Pdf, from where to select page.
string inFile2 = "ABC.pdf"; //ABC.pdf in which page from SinglePage.Pdf will be inserted.
string outFile = "OutPutfile.pdf"; //Resultant Output file.

FileStream SinglePageStream = new FileStream(inFile1, FileMode.Open);
FileStream ABCStream = new FileStream(inFile2, FileMode.Open);
FileStream outputStream = new FileStream(outFile, FileMode.Create);

PdfFileEditor editor = new PdfFileEditor();
int location = 1; //Location in ABC.Pdf, where to insert the page from SinglePage.pdf
int[] pages = new int[] { 1 }; // Number of Pages from SinglePage.Pdf
editor.Insert(ABCStream, 1, SinglePageStream, pages, outputStream); //1 Page from SiglePage.Pdf will be inserted after 1st page of ABC.pdf

outputStream.Close();

Hi,

Thanks a lot for the detailed reply.

From the above description it is clear that we can take few pages from one PDF (say "SinglePage.pdf") and insert them into another PDF(say "ABC.pdf") at a perticular location.

I have one query here: Say i have First PDF with 4 pages and second PDF with 5 pages.Now i want to take 1,3 page numbers from First PDF and 2,3 pages from Second PDF and merge them ? If possible how can we arrange the page number sequence in the merged document.

Thanks a lot for your support so far.

Prashant

Hello Prashant,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

There are two approaches to accomplish your requirement.

1st - Split both the pdf files into single page documents, and create the resultant pdf, in a sequence of pages according to your requirement.

2nd - Extract specific pages from both the pdf files, and save them as separate files, and than merge them through Insert method of PdfFileEditor class. In this method you can only specify the location where you can insert the pages & the number of pages to be inserted. Following code snippet is based over this scenario

int[] pages1 = new int[] { 1, 3}; // Pages to be extracted from First file (pdf with 4 pages)
PdfFileEditor pdfEditor = new PdfFileEditor();
pdfEditor.Extract(@"C:\Temp\4Page.pdf", pages1, @"C:\Temp\Extract\First_Third_page.pdf"); //Extract page 1 & 3 from 1st pdf, and save as a separate pdf file.

int[] pages2 = new int[] { 2, 3}; // Pages to be extracted from Second file (pdf with 5 pages)
pdfEditor.Extract(@"C:\Temp\5Page.pdf", pages2, @"C:\Temp\Extract\Second_Third_page.pdf"); // Extract page 2 & 3 from 1st pdf, and save as a separate pdf file.

// Now we need to merge both the pdf files. We will insert 2 pages from 2nd file into 1st pdf at location 1.

int[] pages = new int[] { 1, 2 }; // Number of Pages to be inserted from 2nd pdf
pdfEditor.Insert(@"C:\Temp\Extract\First_Third_page.pdf", 1, @"C:\Temp\Extract\Second_Third_page.pdf", pages, @"c:\Temp\extract\FinalConcatenated.pdf"); // The Final concatenated pdf will have 4 pages with sequence like, Page 1 from 1st pdf, Page2 & Page 3 from 2nd Pdf, and Page 3 from 1st Pdf

How to work with password protected PDF? Where to put password when open PDf in the stream.

@prakash.handsome

You can use password in second argument of Document constructor as follows:

Document doc = new Document(inputStream, “pass”);