Add Delete Pages in PDF C#

In this tutorial, we’ll explore how to add or delete pages in a PDF file using C#. Manipulating PDF files programmatically can be useful in various scenarios, such as merging multiple PDFs, extracting specific pages, or modifying existing PDFs. We’ll focus specifically on adding new pages to an existing PDF and deleting unwanted pages.

Why Add or Delete Pages in PDF?

Before we learn the technical details, let’s discuss why you might need to add or delete pages in a PDF document:

Adding Pages

Insertion of New Content: When you need to insert additional content or information into an existing PDF, such as new chapters in a book or additional sections in a report.

Document Assembly: Combining multiple PDF files into a single document by adding pages from different sources.

Template Creation: Adding blank pages as templates for users to fill out or for specific purposes like signatures or annotations.

Deleting Pages

Content Removal: Removing unnecessary or confidential pages from a PDF before sharing or publishing it.

Error Correction: Deleting pages with errors or outdated information to maintain document accuracy.

Customization: Tailoring PDF content for different audiences by removing irrelevant sections.

Now that we understand the reasons for adding or deleting pages in a PDF, let’s proceed with the technical implementation using C#.

PDF Pages Manipulation - C# API Installation

You can manipulate pages in a PDF document by installing Conholdate.Total for .NET in your environment by using the NuGet installation command below:

PM> NuGet\Install-Package Conholdate.Total

Add Pages in PDF Document using C#

You can add pages into a PDF document by following the steps below:

  • Create an object of Document class to load the source PDF document.
  • Add an empty page at the end of the document with the Add method.
  • Save the output PDF file using the Save method.

The sample code below shows how to add pages in a PDF document using C#:

Insert Pages to PDF File at Specific Index using C#

You can insert a page into a PDF document at any index by mentioning the number, as explained in the following steps:

  • Initiate a Document class object to load the input PDF file.
  • Insert a page at any index of the document with the Insert method.
  • Export the generated PDF document by calling the Save method.

The following code snippet demonstrates how to insert a page at any index of the PDF document using C#:

Delete Page from a PDF Document in C#

For deleting any page from a PDF document, you need to follow the steps below:

  • Initialize an instance of the Document class to read the input PDF document.
  • Delete any page by mentioning its zero-based index in the Delete method.
  • Write the output PDF file with the Save method.

The code sample elaborates on how to remove any page in a PDF document using C#:

Free Evaluation License

You may get a free temporary license for testing the APIs to their full capacity.

Summing Up

You’ve now learned how to add or delete pages in a PDF using C#. This tutorial can be followed to automate PDF editing tasks in various applications and scenarios. You can experiment with different page manipulation techniques to suit your needs. If you have any concerns or questions, please write to us at forum.

See Also