Articles in this section
Category / Section

How to replace pages in a PDF document?

2 mins read

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;
// Draws 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:

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

 

Conclusion

I hope you enjoyed learning about how to replace pages in a PDF document.

You can refer to our WinForms PDF’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms PDF documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms PDF and other WinForms components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied