)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to replace pages in a PDF document?

Platform: WinForms |
Control: PDF |
Published Date: January 20, 2016 |
Last Revised Date: January 24, 2020
Tags: pdf, page

The pages can be replaced in a PDF document using the PdfTemplate. Please find the code snippet and sample below for the same.

C#:

//Existing pdf with file path.
 string file1 = "../../Data/imageDoc.pdf";

//Loading File1
 PdfLoadedDocument ldDoc1 = new PdfLoadedDocument(file1);
 
 //Gets the page collection of loaded document
 PdfLoadedPageCollection pageCollection = ldDoc1.Pages;
//Removes the page at index zero
 pageCollection.RemoveAt(0);
 //Initializes font.
 PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
 //Creates a new Pdf document
 PdfDocument doc = new PdfDocument();
 //Adds a new page
 PdfPage page = doc.Pages.Add();
 PdfGraphics g = page.Graphics;
 //Draws string in the new page.
 g.DrawString("Hello", font, PdfBrushes.Black, new PointF(0, 0));
 //Creates template of replacing page.
 PdfTemplate template = page.CreateTemplate();
 
 //Inserts new page at index zero in loaded document
 PdfPage newLoadedPage = pageCollection.Insert(0) as PdfPage;
  PdfGraphics lgraphics = newLoadedPage.Graphics;
  //Draw the template of the replacing page in the newly inserted loaded document page graphics.
 lgraphics.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());
 
 //Save the document and dispose it
 ldDoc1.Save("sample.pdf");
 ldDoc1.Close();

 

Sample Link:

http://www.syncfusion.com/downloads/support/directtrac/139783/ze/Template-650157397

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon