Merge pages from various documents

GroupDocs.Merger allows to merge the source document with specific document pages from joined document into one resultant document by specifying desired page numbers or page ranges. Joined documents should be of the same format.

Here are the steps to join several document parts:

  • Instantiate Merger object with source document path or InputStream;
  • Instantiate JoinOptions object and specify desired page numbers or page range to join;
  • Call join method and pass joined document file path or InputStream to it specifying JoinOptions object as parameter. Repeat this step for every joined document part.
  • Call save method specifying file path to save resultant document.

The following code sample demonstrates how to join document parts:

String filePath = "c:\sample.docx";
String filePath2 = "c:\sample2.docx";
String filePathOut = "c:\output\result.docx";

JoinOptions joinOptions = new JoinOptions(1, 2);

Merger merger = new Merger(filePath);
merger.join(filePath2 , joinOptions);
merger.save(filePathOut);