C# tutorial: insert pages to an existing PDF document


Insert pages to an existing PDF document

In the previous page, you learnt how to add new content under and over the original content of an existing PDF document. Now I am going to show how to insert pages of a PDF document to another PDF document. In this tutorial, I have two PDF files, htmlpdf.pdf and pdfcontentadded.pdf. The first page of the htmlpdf.pdf file will be inserted into the second page of the pdfcontentadded.pdf file. To accomplish the task, you need two PdfReader objects. One PdfReader object reads one page from the first PDF file and another PdfReader object reads all pages from the second PDF file. The output file will contain all pages from the second file and one page from the first file. So, you will pass the second PdfReader object to the constructor of the PdfStamper class. To import one page from the first file, the GetImportedPage method of the PdfStamper class is used. After that, you will use the InsertPage() method of the PdfStamper class to specify the page number to place the new PDF page. This method accepts two arguments. The first argument is the page number and the second argument is the size of the page. To insert the page to the specified location, you must first get the PdfContentByte object from the PdfStamper object. Then use the AddTemplate method of the PdfContentByte class to insert the page.

Below is the C# code to do thing as I mentioned above.

//variables
String first_source = "D:/myprogram/pdf/htmlpdf.pdf";
String second_source = "d:/pdfcontentadded.pdf";
String pathout = "d:/pdfout.pdf";
//create a document object
//var doc = new Document(PageSize.A4);
//create PdfReader objects to read pages from the source files
PdfReader reader = new PdfReader(first_source);
PdfReader reader1 = new PdfReader(second_source);
//create PdfStamper object to write to the pages read from readers
PdfStamper stamper=new PdfStamper(reader1, new FileStream(pathout, FileMode.Create));
//get one page from htmlpdf.pdf
PdfImportedPage page = stamper.GetImportedPage(reader, 1);
//the page gotten from htmlpdf.pdf will be inserted at the second page in the first source doc
stamper.InsertPage(2, reader1.GetPageSize(1));
//insert the page
PdfContentByte pb = stamper.GetUnderContent(2);
pb.AddTemplate(page, 0, 0);
//close the stamper
stamper.Close();

insert pages to existing pdf file





Comments

c# add a page to a pdf documen comment

 c# add a page to a pdf documen

c# add a page to a pdf document
http://www.rasteredge.com/how-to/csharp-imaging/pdf-insert/


2016-04-12



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.