DEV Community

Cover image for Remove pages from a PDF programmatically
Atir Tahir
Atir Tahir

Posted on

Remove pages from a PDF programmatically

How to remove a single or a list of pages from a PDF?
You can do this using Adobe Acrobat or any such software/tool. But what if the tool is not available? And what if you always get such PDF files in bulk and you have to remove all those unnecessary pages. This is just a one random use-case. There could be many others.
GroupDocs.Merger for .NET provides an ability to remove single page or a collection of specific page numbers from the source document.
Have a look at the following code:

string filePath = @"c:\sample.pdf";
string filePathOut = @"c:\output\result.pdf";

RemoveOptions removeOptions = new RemoveOptions(new int[] { 3, 5 });

using (Merger merger = new Merger(filePath))
{
    merger.RemovePages(removeOptions);
    merger.Save(filePathOut);
} 
Enter fullscreen mode Exit fullscreen mode

The above code will remove page 3 and 5 from the source file.
About the API
GroupDocs.Merger is a back-end, UI-agnostic API That could be implemented in any .NET (new or existing) application without any third party tool or software dependency.
All you have to do is to add the DLL reference or install NuGet in the application.

Top comments (0)