DEV Community

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

Posted on

1 1

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.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay