Merge pages in an arbitrary order

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Merger MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-merger -- dnx GroupDocs.Merger.Mcp --yes
Folders, licensing and more →

GroupDocs.Merger for Python via .NET can build a new document from chosen pages of several documents, in the order you list them: for example, page 3 of document A, then page 2 of document B, then page 1 of document A.

  • To join a whole document with chosen pages of other documents, pass PageJoinOptions to join, for example merger.join("./input2.pdf", PageJoinOptions([1, 3])).
  • To take any pages of any of the documents, in any order, use a PageBuilder, as this page shows.

A PageBuilder comes from merger.create_page_builder(). Its add_page method appends one page and its add_page_range method appends several. Once every page is listed, merger.apply_page_builder() makes them the document that save writes.

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 create_page_builder or apply_page_builder raises GroupDocsMergerException (from groupdocs.merger.exceptions).

Specifying the pages by their page information

The easiest way to name a page is to pass its page information, which says which document the page comes from and its number. The builder’s documents list holds one entry per loaded document, and each entry’s pages list holds its pages.

  1. Create a Merger with the path to the first document.
  2. Call merger.join() to add each other document.
  3. Call merger.create_page_builder() to get a PageBuilder.
  4. Call add_page with each page you want, in order. The documents and pages lists count from 0: documents[0].pages[0] is page 1 of the first document.
  5. Call merger.apply_page_builder() with the builder.
  6. Call merger.save() with the path of the result.
from groupdocs.merger import Merger

def merge_pages_in_order_by_page():
    with Merger("./input.docx") as merger:
        merger.join("./input2.docx")

        page_builder = merger.create_page_builder()
        first = page_builder.documents[0]   # the document the Merger opened
        second = page_builder.documents[1]  # the first document joined
        page_builder.add_page(first.pages[0])   # page 1 of the first document
        page_builder.add_page(second.pages[2])  # page 3 of the second document
        page_builder.add_page(first.pages[1])   # page 2 of the first document
        page_builder.add_page(second.pages[1])  # page 2 of the second document
        page_builder.add_page(second.pages[0])  # page 1 of the second document
        page_builder.add_page(first.pages[2])   # page 3 of the first document

        # Make the listed pages the document to save
        merger.apply_page_builder(page_builder)
        merger.save("./output.docx")

if __name__ == "__main__":
    merge_pages_in_order_by_page()

input.docx is a sample file used in this example. Click here to download it.

input2.docx is a sample file used in this example. Click here to download it.

Binary file (DOCX, 13 KB)

Download full output

Specifying the pages by document index and page number

Listing pages through documents and pages needs the information of every document loaded first. With many pages, it is faster to skip loading that information and name each page by the index of its document and its page number.

  1. Create a Merger with the path to the first document, and call merger.join() to add each other document.
  2. Create a PageBuilderOptions and set load_document_info to False.
  3. Call merger.create_page_builder() with the options.
  4. Call add_page with the index of the document (0 is the document the Merger opened, 1 the first one joined) and the page number (1 is the first page), for each page in order.
  5. Call merger.apply_page_builder() with the builder, then merger.save().
from groupdocs.merger import Merger
from groupdocs.merger.domain.options import PageBuilderOptions

def merge_pages_in_order_by_number():
    with Merger("./input.docx") as merger:
        merger.join("./input2.docx")

        # Do not load the documents' information: the pages are named by number
        page_builder_options = PageBuilderOptions()
        page_builder_options.load_document_info = False
        page_builder = merger.create_page_builder(page_builder_options)

        # add_page(document index, page number): document 0 is the one the Merger opened
        page_builder.add_page(0, 1)  # page 1 of the first document
        page_builder.add_page(1, 3)  # page 3 of the second document
        page_builder.add_page(0, 2)  # page 2 of the first document
        page_builder.add_page(1, 2)  # page 2 of the second document
        page_builder.add_page(1, 1)  # page 1 of the second document
        page_builder.add_page(0, 3)  # page 3 of the first document

        merger.apply_page_builder(page_builder)
        merger.save("./output.docx")

if __name__ == "__main__":
    merge_pages_in_order_by_number()

input.docx is a sample file used in this example. Click here to download it.

input2.docx is a sample file used in this example. Click here to download it.

Binary file (DOCX, 13 KB)

Download full output

Specifying the pages as a range

add_page_range appends several pages in one call. It takes a list of page information, in the order the pages should appear.

  1. Create a Merger with the path to the first document, and call merger.join() to add each other document.
  2. Call merger.create_page_builder() to get a PageBuilder.
  3. Call add_page_range with a list of the pages you want, taken from the documents and pages lists, which count from 0.
  4. Call merger.apply_page_builder() with the builder, then merger.save().
from groupdocs.merger import Merger

def merge_pages_in_order_as_range():
    with Merger("./input.docx") as merger:
        merger.join("./input2.docx")

        page_builder = merger.create_page_builder()
        first = page_builder.documents[0]
        second = page_builder.documents[1]
        # Append the pages in the order they are listed
        page_builder.add_page_range([
            first.pages[0],   # page 1 of the first document
            second.pages[2],  # page 3 of the second document
            first.pages[1],   # page 2 of the first document
            second.pages[1],  # page 2 of the second document
            second.pages[0],  # page 1 of the second document
            first.pages[2],   # page 3 of the first document
        ])

        merger.apply_page_builder(page_builder)
        merger.save("./output.docx")

if __name__ == "__main__":
    merge_pages_in_order_as_range()

input.docx is a sample file used in this example. Click here to download it.

input2.docx is a sample file used in this example. Click here to download it.

Binary file (DOCX, 13 KB)

Download full output

API reference

See also