Remove pages

GroupDocs.Merger for Python via .NET lets you remove one or more pages from a source document and save the result as a new document. The pages to delete are specified through RemoveOptions.

Here are the steps to remove document pages:

  • Instantiate Merger with the path to the source document.
  • Create a RemoveOptions object with the list of 1-based page numbers to remove.
  • Call merger.remove_pages() passing the RemoveOptions object.
  • Call merger.save() to write the resulting document.
from groupdocs.merger import Merger
from groupdocs.merger.domain.options import RemoveOptions

def remove_document_pages():
    # Load the source PDF document
    with Merger("./input.pdf") as merger:
        # Remove page 2 from the document
        merger.remove_pages(RemoveOptions([2]))
        # Save the document with the specified page removed
        merger.save("./output.pdf")

if __name__ == "__main__":
    remove_document_pages()

input.pdf is a sample file used in this example. Click here to download it.

Binary file (PDF, 335 KB)

Download full output

Explanation

  • Load source document: Merger("./input.pdf") opens the document as a context manager, ensuring resources are released on exit.
  • RemoveOptions([2]): accepts a list of 1-based page numbers to remove. To remove multiple pages, include them all in the list — for example, RemoveOptions([2, 4, 6]).
  • merger.remove_pages(): applies the removal in memory. The original file is not modified.
  • merger.save("./output.pdf"): writes the resulting document (with the specified pages omitted) to the given path.
Tip
To delete pages in a range (for example, all even pages from 1 to 10), use the RemoveOptions overload that accepts start_number, end_number, and a RangeMode value.

API reference

See also