We often require removing the unwanted, obsolete, highly confidential pages from the documents while sharing or finalizing the drafts. In this article, we will learn how to programmatically remove such pages from the PDF document using C#. Requirements may differ at times, so we will discuss different ways to remove the different sets of pages within the PDF document.

The following topics are discussed below:

.NET API to Remove Pages from PDF

GroupDocs.Merger showcases the .NET API that allows programmatically deleting pages from the PDF document. Additionally, it allows .NET applications to change pages orientation, move pages, split documents, extract, and rotate document pages. We will use this GroupDocs.Merger for .NET to delete selective pages of PDF files using C#. For the details and other features of the API, you can visit the documentation.

You can download the DLLs or MSI installer from the downloads section or install the API in your .NET application via NuGet.

PM> Install-Package GroupDocs.Merger

Remove Selected Pages from PDF using C#

Just provide the list of pages from the loaded PDF document to remove. The below steps allow removing the provided list of selective pages from a PDF document using C#.

  • Initialize RemoveOptions class with the list of page numbers to remove.
  • Instantiate Merger object with source document path or stream.
  • Call RemovePages() method to delete the listed pages.
  • Call the appropriate Save() method to save the resultant document.

The following C# code example removes the selected 3rd and 5th pages from the PDF document.

Remove Pages Range from PDF using C#

Likewise, you can remove any range of pages within the PDF document. The following steps allow removing a sequence of pages within the provided range using C#.

  • Initialize RemoveOptions.
  • Provide the page range by setting starting and ending page number.
  • Instantiate Merger object with source document path or stream.
  • Call RemovePages() method with the range.
  • Call the appropriate Save() method to save the resultant document.

The following C# sample code removes all the pages from the PDF document within the provided range i.e. 2 to 4.

Remove Even or Odd Pages from PDF using C#

Similarly, you can remove all the even or odd pages of the document. The following steps show how to remove even or odd pages of the PDF file within the given range using C#.

  • Initialize RemoveOptions class with the page range.
  • Set the mode to even or odd.
  • Instantiate Merger object with source document path or stream.
  • Call RemovePages() method with the removal options.
  • Call the appropriate Save() method to save the resultant document.

The following C# code sample removes all the even pages from the PDF document within the provided range i.e. 1-6.

The following C# code snippet removes all the odd pages from the whole PDF document.

Get a Free API License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

To sum up, we just learned how to delete pages from a PDF document using C# within .NET applications. Specifically, we have seen how to delete pages by providing page numbers and page ranges. Finally, we saw how to remove even or odd pages from any PDF document. You can try building your own application to eliminate any variation of selected pages from the PDF files.

For more details about the API, visit the documentation. For queries, contact us via the forum.

See Also