Extract pages

Extract pages 

GroupDocs.Merger allows to extract pages from source document. The result is a new document that contains only specified pages from the source document.

Here are the steps to extract document pages:

  • Initialize ExtractOptions class with page numbers that should appear in the resultant document;
  • Instantiate Merger object with source document path or InputStream;
  • Call extractPages method and pass ExtractOptions object to it;
  • Call save method specifying file path to save resultant document.

The following code sample demonstrates how to extract document pages by specifying exact page numbers:

with gm.Merger("c:/sample.pdf") as merger:
    extract_options = gm.domain.options.ExtractOptions(1, 2)
    merger.extract_pages(extract_options)
    merger.save("c:/result.pdf")

The following code sample demonstrates how to extract document pages by specifying page numbers range:

with gm.Merger("c:/sample.pdf") as merger:
    even_pages = gm.domain.options.RangeMode.EVENPAGES
    extract_options = gm.domain.options.ExtractOptions(1, 3, even_pages)
    merger.extract_pages(extract_options)
    merger.save("c:/result.pdf")