The Page Builder feature transforms document merging by allowing users to seamlessly combine pages from different documents with precision and ease. It enables merging pages in any sequence, giving complete control over the structure of the final document. Whether merging a few key pages or creating a comprehensive document, this tool ensures flexibility and efficiency. By reducing manual effort, it simplifies the process of crafting tailored documents, saving valuable time and boosting overall productivity in both personal and professional workflows.

Page Builder - how to merge pages from documents

The following topics are discussed here:

If you are interested in Merging documents online, visit the Online Merging Tool that is powered by GroupDocs.

Understanding Page Builder

The Page Builder feature introduces a streamlined way to create custom documents by combining pages from multiple source documents. This feature allows developers to build a new document step-by-step, adding pages in any sequence from two or more documents. Here’s how the process works:

  • Begin by loading the source Word document using the Merger class and pass to the constructor one of the source documents.
  • Join the documents one by one to merge using the Join method.
  • Initialize the Page Builder. Over the method CreatePageBuilder from the Merger instance This serves as the starting point for constructing your new document.
  • The AddPage method of the Page Builder instance allows to add into the destination document any particular page of joined documents previously
  • for each source document, choose the specific pages you want to include over the index;
  • add these pages to the new document in the desired sequence. You can mix and match pages from different documents to meet your needs;
  • repeat this process for additional source documents, ensuring that you have the flexibility to combine pages in a way that best suits your requirements;
  • Using ApplyPageBuilder method to apply changes from the Page Builder instance
  • Save the merged document to any destination. Once all the necessary pages have been added, save the new document. The final output will be a fully customized file containing pages from multiple sources, organized exactly how you want them.

This step-by-step approach makes it easy to create documents tailored to specific use cases, such as compiling reports, creating personalized presentations, or assembling legal documents. With Page Builder, developers have complete control over the page order and content, eliminating the need for time-consuming manual edits and ensuring precision in the final output.

Sample case and code implementation

Let’s demonstrate how to use the Page Builder feature in GroupDocs.Merger with a practical example. To simplify the case assume we have two documents:

  • Document A with 4 pages.
  • Document B with 4 pages.

We aim to create a new document by merging pages from these two documents in different sequences based on specific scenarios.

Page Builder - Sample A and B document structure with 4 pages each

Here we marked each document page with document letter A, B and page number. From documents A and B we need to receive the tailored document C with the following structure

Page Builder - Sample A and B document structure with 4 pages each

The following source code snippet shows how to merge particular pages from the several documents in the resultant document.

string documentA = @"documentA.pdf";
string documentB = @"documentB.pdf";
string documentC = @"documentC.pdf";

using (Merger merger = new Merger(documentA))
{
    merger.Join(documentB);

    PageBuilder pageBuilder = merger.CreatePageBuilder();
    // get documents A and B references
    var docA = pageBuilder.Documents[0];
    var docB = pageBuilder.Documents[1];
    
    pageBuilder.AddPage(docA.Pages[0]); // document A, page 1 or Page A-1
    pageBuilder.AddPage(docB.Pages[0]); // document B, page 1 or Page B-1
    pageBuilder.AddPage(docB.Pages[1]); // document B, page 2 or Page B-2
    pageBuilder.AddPage(docB.Pages[2]); // document B, page 3 or Page B-3
    pageBuilder.AddPage(docA.Pages[1]); // document A, page 2 or Page A-2
    pageBuilder.AddPage(docA.Pages[3]); // document A, page 4 or Page A-4
    
    // Apply the page order
    merger.ApplyPageBuilder(pageBuilder);

    merger.Save(documentC);
}

Specifying the range of the required pages

When working with document manipulation, adding multiple pages efficiently is crucial for handling complex workflows. Instead of adding pages individually, you can use the AddPageRange method, which accepts an array of IPageInfo objects, each representing a specific page. This approach simplifies the process, reduces repetitive code, and improves performance by allowing batch operations. Below is a code example demonstrating how to utilize this method to add multiple pages from different documents in a single, streamlined operation.

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 CreatePageBuilder method.
  • Call the AddPageRange method and pass an array of IPageInfo objects describing the appropriate pages. Please note that IPageInfo objects use zero-based notation for source documents and page numbers.
  • 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 documentA = @"documentA.pdf";
string documentB = @"documentB.pdf";
string documentC = @"documentC.pdf";

using (Merger merger = new Merger(documentA))
{
    merger.Join(documentB);

    PageBuilder pageBuilder = merger.CreatePageBuilder();
     // get documents A and B references
    var docA = pageBuilder.Documents[0];
    var docB = pageBuilder.Documents[1];
    // Specify a range of pages
    IPageInfo[] range = new IPageInfo[] {
        docA.Pages[0], // Page A-1 from the document A
        docB.Pages[0], // Page B-1 from the document B
        docB.Pages[1], // Page B-2 from the document B
        docB.Pages[2], // Page B-3 from the document B
        docA.Pages[1], // Page A-2 from the document A
        docA.Pages[3], // Page A-4 from the document A                    
    };
    // Append a page of pages
    pageBuilder.AddPageRange(range);

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

    merger.Save(documentC);
}

Conclusion

These examples illustrate the versatility of the Page Builder feature. Developers can easily combine pages from multiple documents in various sequences to create tailored outputs. This feature significantly reduces manual work and ensures precision in document generation, making it an essential tool for diverse use cases.

Get a Free API License

You can get a free temporary license to use the API without the evaluation limitations.

Build Your Own Merge Page Builder Application

Here are some important links that will help you in building your own document comparison application.

Documentation | API Reference | Supported File Formats

Running Examples – GitHub | Free Support Forum | Release Notes

See Also

For more information and additional resources, you may find the following links useful: