Merge pages in the arbitrary order

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 in detail in the Merge pages from various documents section.

  • To merge arbitrary pages from all the source documents, you can use the PageBuilder object, which is explained on this page.

Using the PageBuilder object, you can define the specific order in which the pages from the source documents will be combined. For instance, you can merge the 3rd page from Document A with the 5th page from Document B and then with the 2nd page from Document A.

To start using the PageBuilder class, you need to create an instance of it. You can then use the AddPage method to append a single page, or the AddPageRange method to append a range of pages. Once you have added all the pages in the required order, you can apply them to the documents by calling the Merger.ApplyPageBuilder method.

Note
PageBuilder works with paged formats such as PDF, Word processing documents and multi-page TIFF images. It is not available after a join with ImageJoinOptions, and while a spreadsheet joined with SpreadsheetJoinMode.Rows is pending: in both cases CreatePageBuilder or ApplyPageBuilder throws GroupDocsMergerException.

Specifying the required pages by their number

The AddPage method allows several ways to define the page to be added. The easiest way is to pass the IPageInfo object that describes the page’s source document and its number.

Here are the steps to combine several pages from various documents specifying the pages by their numbers:

  • Create a Merger object and provide the path or stream of the source file.
  • Use the Join to add another source document. Repeat this step for each document you want to merge.
  • Create a PageBuilder object by calling the Merger.CreatePageBuilder method.
  • Call the AddPage method and pass the IPageInfo object describing the appropriate page. Note that the Documents and Pages collections are zero-based: Documents[0].Pages[0] is page 1 of the first document.  Repeat this step for every page you want to add.
  • Use the ApplyPageBuilder method to apply the specified order of pages to the merged document.
  • Save the resulting document by calling the Save method and providing a file path.
string filePath = @"c:\sample1.docx";
string filePath2 = @"c:\sample2.docx";
string filePathOut = @"c:\output\result.docx";

using (Merger merger = new Merger(filePath))
{
    merger.Join(filePath2);

    PageBuilder pageBuilder = merger.CreatePageBuilder();
    // Add page 1 from the first document
    pageBuilder.AddPage(pageBuilder.Documents[0].Pages[0]);
    // Add page 3 from the second document
    pageBuilder.AddPage(pageBuilder.Documents[1].Pages[2]);
    // Add page 4 from the first document 
    pageBuilder.AddPage(pageBuilder.Documents[0].Pages[3]);
    // Add page 2 from the second document
    pageBuilder.AddPage(pageBuilder.Documents[1].Pages[1]);
    // Add page 1 from the second document
    pageBuilder.AddPage(pageBuilder.Documents[1].Pages[0]);
    // Add page 6 from the first document 
    pageBuilder.AddPage(pageBuilder.Documents[0].Pages[5]);

    // Apply the page order
    merger.ApplyPageBuilder(pageBuilder);

    merger.Save(filePathOut);
}

Specifying the required pages by document index and page number

When adding many pages, taking them from the Documents and Pages collections is not optimal, since that requires loading document information. To improve performance, you can skip loading the document information and pass the document index and the page number directly.

To combine several pages from various documents specifying the document index and the page number:

  • Create a Merger object and provide the path or stream of the source file.
  • Use the Join to add another source document. Repeat this step for each document you want to merge.
  • Create a PageBuilderOptions object and set the LoadDocumentInfo property to false.
  • Create a PageBuilder object by calling the Merger.CreatePageBuilder method and pass the PageBuilderOptions to it.
  • Call the AddPage method and pass the source document index (starting from 0) and the number of the needed page (starting from 1). Repeat this step for every page you want to add.
  • Call the ApplyPageBuilder method to apply the specified order of pages to the merged document.
  • Save the resulting document by calling the Save method and providing a file path.
string filePath = @"c:\sample1.docx";
string filePath2 = @"c:\sample2.docx";
string filePathOut = @"c:\output\result.docx";

using (Merger merger = new Merger(filePath))
{
    merger.Join(filePath2);

    PageBuilderOptions pageBuilderOptions = new PageBuilderOptions();
    pageBuilderOptions.LoadDocumentInfo = false;

    PageBuilder pageBuilder = merger.CreatePageBuilder(pageBuilderOptions);
    // Add page 1 from the first document
    pageBuilder.AddPage(pageBuilder.Documents[0].Index, 1);
    // Add page 3 from the second document
    pageBuilder.AddPage(pageBuilder.Documents[1].Index, 3);
    // Add page 4 from the first document 
    pageBuilder.AddPage(pageBuilder.Documents[0].Index, 4);
    // Add page 2 from the second document
    pageBuilder.AddPage(pageBuilder.Documents[1].Index, 2);
    // Add page 1 from the second document
    pageBuilder.AddPage(pageBuilder.Documents[1].Index, 1);
    // Add page 6 from the first document 
    pageBuilder.AddPage(pageBuilder.Documents[0].Index, 6);
    
    // Apply the page order
    merger.ApplyPageBuilder(pageBuilder);

    merger.Save(filePathOut);
}

Specifying the range of the required pages

When you need to add multiple pages at once, you can use the AddPageRange method. It accepts an array of IPageInfo objects, each describing the appropriate page.

Here are the steps to combine several pages from various documents specifying them as a range:

  • Create a Merger object and provide the path or stream of the source file.
  • Use the Join to add another source document. Repeat this step for each document you want to merge.
  • Create a PageBuilder object by calling the Merger.CreatePageBuilder method.
  • Call the AddPageRange method and pass an array of IPageInfo objects describing the appropriate pages. Please note that the Documents and Pages collections are zero-based: Documents[0].Pages[0] is page 1 of the first document.
  • Use the ApplyPageBuilder method to apply the specified order of pages to the merged document.
  • Save the resulting document by calling the Save method and providing a file path.
string filePath = @"c:\sample1.docx";
string filePath2 = @"c:\sample2.docx";
string filePathOut = @"c:\output\result.docx";

using (Merger merger = new Merger(filePath))
{
    merger.Join(filePath2);

    PageBuilder pageBuilder = merger.CreatePageBuilder();
    // Specify a range of pages
    IPageInfo[] range = new IPageInfo[] {
                    pageBuilder.Documents[0].Pages[0], // Page 1 from the first document
                    pageBuilder.Documents[1].Pages[2], // Page 3 from the second document
                    pageBuilder.Documents[0].Pages[3], // Page 4 from the first document
                    pageBuilder.Documents[1].Pages[1], // Page 2 from the second document
                    pageBuilder.Documents[1].Pages[0], // Page 1 from the second document
                    pageBuilder.Documents[0].Pages[5], // Page 6 from the first document                    
                };
    // Append a range of pages
    pageBuilder.AddPageRange(range);

    // Apply the page order
    merger.ApplyPageBuilder(pageBuilder);

    merger.Save(filePathOut);
}
Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.