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 is useful for merging PDFs, extracting specific pages, or modifying existing documents. We’ll focus on adding new pages to an existing PDF and deleting unwanted pages.

Why Add or Delete Pages in PDF?

Before we dive into the code, let’s discuss why you might need to add or delete pages in a PDF document:

Adding Pages

Insertion of New Content: Insert additional content or information, such as new chapters in a book or extra sections in a report.

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

Template Creation: Add blank pages as templates for users to fill out or for signatures and annotations.

Deleting Pages

Content Removal: Remove unnecessary or confidential pages before sharing or publishing.

Error Correction: Delete pages with errors or outdated information to keep the document accurate.

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

Now that we understand the reasons, 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 with the NuGet command below:

PM> NuGet\Install-Package Conholdate.Total

Add Pages in PDF Document using C#

Add pages to a PDF document by following these steps:

  • Create a Document object to load the source PDF.
  • Add an empty page at the end of the document with the Add method.
  • Save the output PDF 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#

Insert a page into a PDF document at any index by following these steps:

  • Create a Document object to load the input PDF.
  • Insert a page at the desired index with the Insert method.
  • Export the updated PDF by calling the Save method.

The code snippet below demonstrates how to insert a page at a specific index using C#:

Delete Page from a PDF Document in C#

Delete a page from a PDF document by following these steps:

  • Initialize a Document object to read the input PDF.
  • Delete the page by its zero‑based index using the Delete method.
  • Write the output PDF with the Save method.

The sample code below shows how to remove a 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 learned how to add or delete pages in a PDF using C#. Use this guide to automate PDF editing tasks in various applications. Experiment with different page manipulation techniques to fit your needs. If you have questions, please write to us at the forum.

See Also