Extract pages

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Merger MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-merger -- dnx GroupDocs.Merger.Mcp --yes
Folders, licensing and more →

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

Warning
Specify at least one page number. Calling ExtractPages without page numbers (for example, with new ExtractOptions()) throws GroupDocsMergerException (“There are no page numbers to extract.”).

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 stream;
  • 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:

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

ExtractOptions extractOptions = new ExtractOptions(new int[] { 1, 4 }); // Resultant document will contain pages 1 and 4

using (Merger merger = new Merger(filePath))
{
    merger.ExtractPages(extractOptions);
    merger.Save(filePathOut);
}

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

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

ExtractOptions extractOptions = new ExtractOptions(1, 3, RangeMode.EvenPages); // Resultant document will contain page 2
using (Merger merger = new Merger(filePath))
{
	merger.ExtractPages(extractOptions);
    merger.Save(filePathOut);
}