Merge pages from various documents

With GroupDocs.Merger you can combine different pages from multiple source documents into a single document.

  • To merge the entire first document along with specific pages from the second and subsequent documents, you can make use of the PageJoinOptions object, which is explained on this page.

  • To merge arbitrary pages from all the source documents, you can use the PageBuilder object, which is explained in detail in the Merge pages in the arbitrary order section.

Using the PageJoinOptions object, you can merge the source document with specific document pages from the joined documents 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:

  • Create a Merger object and provide the path or stream of the source file.
  • Create a PageJoinOptions object and specify desired page numbers or page ranges to join.
  • Use the Join to add another source document. Provide the path or stream of the source file and the PageJoinOptions object as parameters. Repeat this step for every joined document part.
  • Save the resulting document by calling the Save method and providing a file path.

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";

PageJoinOptions joinOptions = new PageJoinOptions(1, 4, RangeMode.OddPages);

using (Merger merger = new Merger(filePath, loadOptions))
{
    merger.Join(filePath2, joinOptions);
    merger.Save(filePathOut);
}