Split document

GroupDocs.Merger allows to split source document into several resultant documents. Document splitting can be performed in different ways by specifying page numbers array or start/end page numbers and setting different PageSplitOptions modes.
Here are the possible use cases:

  1. Page numbers array specified and splitting mode is set to PageSplitMode.Pages - specified page numbers indicate exact page numbers, which will be saved to the separate one-page documents.
    Ex: Array{ 3, 6, 8 } will produce 3 documents with 3rd, 6th and 8th pages.
  2. Page numbers array specified and splitting mode is set to PageSplitMode.Interval - specified page numbers indicate the boundaries of the page intervals, which will be saved to the separate multi-page documents.
    Ex: Array{ 3, 6, 8 } will produce 4 page intervals 1-2, 3-5, 6-7, 8-10.

There is also an ability to set parameter RangeMode and obtain only even or odd pages from desired pages range.

The steps to split document to multiple on-page documents are the following:

  • Initialize PageSplitOptions class with output files path format;
  • Instantiate Merger object with source document path or InputStream;
  • Call split method and pass PageSplitOptions object to itfor saving resultant documents.

Split the document to several one-page documents (by exact page numbers)

The following code sample demonstrates how to split document to three one-page documents with 3rd, 6th and 8th pages:

String filePath = "c:\sample.docx";
String filePathOut = "c:\output\document_{0}.{1}";

PageSplitOptions splitOptions = new PageSplitOptions(filePathOut, new int[] { 3, 6, 8 });
Merger merger = new Merger(filePath);
merger.split(splitOptions);

This code snippet will  produce:

Document namePage numbers
document_03
document_16
document_28

Split the document to several one-page documents (by start/end page numbers)

The following code sample demonstrates how to split document to several one-page documents starting from 3rd and ending at 7th page number:

String filePath = "c:\sample.docx";
String filePathOut = "c:\output\document_{0}.{1}";

PageSplitOptions splitOptions = new PageSplitOptions(filePathOut, 3, 7);  
Merger merger = new Merger(filePath);
merger.split(splitOptions);   

This code snippet will  produce:

Document namePage numbers
document_03
document_14
document_25
document_36
document_47

Split the document to several one-page documents (by start/end page numbers and even/odd filter)

The following code sample demonstrates how to split document to several one-page documents for odd pages starting from 3rd and ending at 7th page number:

String filePath = "c:\sample.docx";
String filePathOut = "c:\output\document_{0}.{1}";

PageSplitOptions splitOptions = new PageSplitOptions(filePathOut, 3, 7, RangeMode.OddPages);
   
Merger merger = new Merger(filePath);
merger.split(splitOptions);

This code snippet will produce:

Document namePage numbers
document_03
document_15
document_27

Split the document to several multi-page documents

The following code sample demonstrates how to split document to several multi-page documents:

String filePath = "c:\sample.docx";
String filePathOut = "c:\output\document_{0}.{1}";

PageSplitOptions splitOptions = new PageSplitOptions(filePathOut,  PageSplitMode.Interval, new int[] { 3, 6, 8 },);

Merger merger = new Merger(filePath);
merger.split(splitOptions);

This code snippet will produce:

Document namePage numbers
document_01, 2
document_13, 4, 5
document_26, 7
document_38, 9, 10

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.