1. Home
  2. /
  3. GroupDocs.Merger.LowCode
  4. /
  5. Developer Guide
  6. /
  7. Remove PDF Pages (RemovePagesPdf)

Using RemovePagesPdf to Remove Pages from PDF Documents

This topic shows how to use the RemovePagesPdf plugin to remove pages from a PDF document. The pages to remove are described by RemoveOptions, and Save writes the remaining pages to a new document.

Prerequisites

Refer to the Developer Guide to learn how to set up your environment and run the code examples in this section.

The Loading Source Documents and Saving Output Documents topics explain how to specify input and output documents.

Remove Specific Pages

Pass the numbers of the pages to remove. Page numbers start at 1. The following example removes pages 2 and 4 from an 18-page document.

using System;
using GroupDocs.Merger.Domain.Options;
using GroupDocs.Merger.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply the license
License.Set(publicKey, privateKey);

// Remove pages 2 and 4
var remover = new RemovePagesPdf("business-plan.pdf", new RemoveOptions(new[] { 2, 4 }));

// Save the remaining pages
remover.Save("output/trimmed.pdf");
Imports System
Imports GroupDocs.Merger.Domain.Options
Imports GroupDocs.Merger.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply the license
        License.Set(publicKey, privateKey)

        ' Remove pages 2 and 4
        Dim remover As New RemovePagesPdf("business-plan.pdf", New RemoveOptions({2, 4}))

        ' Save the remaining pages
        remover.Save("output/trimmed.pdf")
    End Sub
End Module
open System
open GroupDocs.Merger.Domain.Options
open GroupDocs.Merger.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply the license
    License.Set(publicKey, privateKey)

    // Remove pages 2 and 4
    let remover = RemovePagesPdf("business-plan.pdf", RemoveOptions([| 2; 4 |]))

    // Save the remaining pages
    remover.Save("output/trimmed.pdf")
    0

Expected output: Download trimmed.pdf (16 pages)

Remove a Range of Pages

Pass the first and the last page of a range. The following example removes the appendix: pages 15 to 18.

using System;
using GroupDocs.Merger.Domain.Options;
using GroupDocs.Merger.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply the license
License.Set(publicKey, privateKey);

// Remove pages 15 to 18
var remover = new RemovePagesPdf("business-plan.pdf", new RemoveOptions(15, 18));

// Save the remaining pages
remover.Save("output/without-appendix.pdf");
Imports System
Imports GroupDocs.Merger.Domain.Options
Imports GroupDocs.Merger.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply the license
        License.Set(publicKey, privateKey)

        ' Remove pages 15 to 18
        Dim remover As New RemovePagesPdf("business-plan.pdf", New RemoveOptions(15, 18))

        ' Save the remaining pages
        remover.Save("output/without-appendix.pdf")
    End Sub
End Module
open System
open GroupDocs.Merger.Domain.Options
open GroupDocs.Merger.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply the license
    License.Set(publicKey, privateKey)

    // Remove pages 15 to 18
    let remover = RemovePagesPdf("business-plan.pdf", RemoveOptions(15, 18))

    // Save the remaining pages
    remover.Save("output/without-appendix.pdf")
    0

Expected output: Download without-appendix.pdf (14 pages)

Remove Odd or Even Pages of a Range

Add a RangeMode to narrow the range to its odd or even pages. The following example removes the even pages from 1 to 6: pages 2, 4, and 6.

using System;
using GroupDocs.Merger.Domain.Options;
using GroupDocs.Merger.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply the license
License.Set(publicKey, privateKey);

// Remove the even pages from 1 to 6
var remover = new RemovePagesPdf("business-plan.pdf", new RemoveOptions(1, 6, RangeMode.EvenPages));

// Save the remaining pages
remover.Save("output/without-even-pages.pdf");
Imports System
Imports GroupDocs.Merger.Domain.Options
Imports GroupDocs.Merger.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply the license
        License.Set(publicKey, privateKey)

        ' Remove the even pages from 1 to 6
        Dim remover As New RemovePagesPdf("business-plan.pdf", New RemoveOptions(1, 6, RangeMode.EvenPages))

        ' Save the remaining pages
        remover.Save("output/without-even-pages.pdf")
    End Sub
End Module
open System
open GroupDocs.Merger.Domain.Options
open GroupDocs.Merger.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply the license
    License.Set(publicKey, privateKey)

    // Remove the even pages from 1 to 6
    let remover = RemovePagesPdf("business-plan.pdf", RemoveOptions(1, 6, RangeMode.EvenPages))

    // Save the remaining pages
    remover.Save("output/without-even-pages.pdf")
    0

Expected output: Download without-even-pages.pdf (15 pages)

Remove Pages from a Stream

The source document can also be a Stream. It is read from the beginning, and it stays open after Save.

using System;
using System.IO;
using GroupDocs.Merger.Domain.Options;
using GroupDocs.Merger.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply the license
License.Set(publicKey, privateKey);

// Open the source document as a stream
using var stream = File.OpenRead("business-plan.pdf");

// Remove pages 2 and 4
var remover = new RemovePagesPdf(stream, new RemoveOptions(new[] { 2, 4 }));

// Save the remaining pages
remover.Save("output/trimmed.pdf");
Imports System
Imports System.IO
Imports GroupDocs.Merger.Domain.Options
Imports GroupDocs.Merger.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply the license
        License.Set(publicKey, privateKey)

        ' Open the source document as a stream
        Using stream = File.OpenRead("business-plan.pdf")
            ' Remove pages 2 and 4
            Dim remover As New RemovePagesPdf(stream, New RemoveOptions({2, 4}))

            ' Save the remaining pages
            remover.Save("output/trimmed.pdf")
        End Using
    End Sub
End Module
open System
open System.IO
open GroupDocs.Merger.Domain.Options
open GroupDocs.Merger.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply the license
    License.Set(publicKey, privateKey)

    // Open the source document as a stream
    use stream = File.OpenRead("business-plan.pdf")

    // Remove pages 2 and 4
    let remover = RemovePagesPdf(stream, RemoveOptions([| 2; 4 |]))

    // Save the remaining pages
    remover.Save("output/trimmed.pdf")
    0

Expected output: Download trimmed.pdf (16 pages)

Note
At least one page number is required, and every page number must exist in the document. Otherwise, Save throws GroupDocsMergerException. See Handling Errors.