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:

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

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

Merger merger = new Merger(filePath);
merger.extractPages(extractOptions);
merger.save(filePathOut);

More resources

Advanced Usage Topics 

To learn more about document merging features, please refer the advanced usage section.

GitHub Examples 

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App 

Along with full featured Java library we provide simple, but powerful free Apps. You are welcome to merge your documents with our free online GroupDocs Merger App.