1. GroupDocs Documentation
  2. /
  3. GroupDocs.Merger Product Family
  4. /
  5. GroupDocs.Merger for .NET
  6. /
  7. Getting Started
  8. /
  9. Merger Use Cases
  10. /
  11. Build One Word Document from Selected Pages of Other Word Files in C#

Build One Word Document from Selected Pages of Other Word Files in C#

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 →
Note
💡 Full working example available on GitHub: build-word-document-from-selected-pages-dotnet

Building one Word document from selected pages is a GroupDocs.Merger capability for .NET that joins chosen page ranges of several DOCX files into a new document, with the ranges measured against the real Word page layout.

Introduction

Which join you write depends on what you hold before you start. Maybe the first file must stay whole, or only part of it should survive. The page numbers may be fixed in your code or typed by a user. The appended content may have to start on a new page, or flow on from the text before it. GroupDocs.Merger for .NET has an answer for each case, and this guide is organised around those inputs rather than around the API.

The ranges follow the real Word page layout since 26.11, including tables that span pages; the release notes describe the previous behaviour. The examples come from a sample project with three Word files: a one-page cover, a 15-page parts catalogue and a 10-page handbook.

How do I take only some pages of one Word file into another?

Open a Merger on the document that should come first, then call Join for every other file with a PageJoinOptions(start, end) that names the pages to append; both ends are inclusive. Call Save to write the result. To keep only some pages of the first document itself, call ExtractPages with an ExtractOptions range before the first Join.

Setup

You need the .NET 8.0 SDK and the NuGet package. The figures in this guide come from a run on GroupDocs.Merger 26.11.0 with a licence applied. Without a licence the library runs in evaluation mode and evaluation limits apply, so get a temporary licence before you compare outputs.

dotnet add package GroupDocs.Merger --version 26.11.0

The project has Program.cs, WordFromSelectedPages.csproj and a resources folder with cover.docx, parts-catalogue.docx and handbook.docx. Main runs the five methods in order (Program.cs, lines 21-62).

The three source files

The cover is a single page that reads “SAMPLE DOCUMENT 2” and “Page 1”. The handbook is the 10-page Word sample from the GroupDocs.Merger examples. The parts catalogue was made for this sample and is the demanding input: 15 pages with a title, an intro and three sections of long tables, 140, 120 and 160 rows, so 420 rows in all, a page break before the second and third section, and a header row marked to repeat.

That catalogue is why the page layout matters. Page 2 of it holds rows PV-027 to PV-057 and page 3 holds PV-058 to PV-088, and joining the range 2-3 returned exactly those two pages, each starting with the header row.

Pick the join from what you have

What you haveUsePages produced in the sample
A cover plus ranges of two filesPageJoinOptions per Join5
A first file you want only partlyExtractPages, then Join4
Every other page of a rangeRangeMode.OddPages3
Parts that must flow on from the coverWordJoinOptions with DisableSectionBreaks3
Page numbers typed by a usera GetDocumentInfo check before Saverejected when outside

If the first file stays whole

Open the Merger on that file and append the rest. Here the cover comes first, then catalogue pages 2-3 and handbook pages 2-3, and both catalogue pages start with the repeated table header row.

Directory.CreateDirectory("output");

using (var merger = new Merger("resources/cover.docx"))
{
    merger.Join("resources/parts-catalogue.docx", new PageJoinOptions(2, 3));
    merger.Join("resources/handbook.docx", new PageJoinOptions(2, 3));
    merger.Save("output/briefing.docx");
}
using var result = new Merger("output/briefing.docx");
int pages = result.GetDocumentInfo().PageCount;

The second block reads the page count back from the saved file, which is how the sample verifies each output. The ranges are checked only when Save runs.

If the first file is needed only in part

PageJoinOptions describe joined files, so they cannot trim the file the Merger was opened on. Call ExtractPages first; it leaves the pages you name, and the Join that follows appends to them. The catalogue’s pages 1-2 lead this digest, followed by handbook pages 2-3.

Directory.CreateDirectory("output");

using (var merger = new Merger("resources/parts-catalogue.docx"))
{
    merger.ExtractPages(new ExtractOptions(1, 2));
    merger.Join("resources/handbook.docx", new PageJoinOptions(2, 3));
    merger.Save("output/digest.docx");
}

If the order matters to you, note that the trimming happens before the append: the trimmed file is always the first part of the output.

If you want every other page

The third argument of PageJoinOptions is a RangeMode. With OddPages the range 1-3 brings pages 1 and 3 of the handbook. RangeMode also has the values EvenPages and AllPages.

Directory.CreateDirectory("output");

using (var merger = new Merger("resources/cover.docx"))
{
    var odd = new PageJoinOptions(1, 3, RangeMode.OddPages);
    merger.Join("resources/handbook.docx", odd);
    merger.Save("output/odd-pages.docx");
}

One option object picks those pages, with no loop over page numbers in your code.

If the parts must flow on from each other

WordJoinOptions accepts the same start and end pages and adds a Mode. WordJoinMode.DisableSectionBreaks joins the pages without section breaks, and the result is one section. In the rendered sample the catalogue’s table header row and its first row, PV-027, start right under the cover text.

Directory.CreateDirectory("output");

using (var merger = new Merger("resources/cover.docx"))
{
    var options = new WordJoinOptions(2, 3)
    {
        Mode = WordJoinMode.DisableSectionBreaks
    };
    merger.Join("resources/parts-catalogue.docx", options);
    merger.Save("output/briefing-one-section.docx");
}

Do not expect a shorter document from this mode. The default join of the same range also gave 3 pages, in 3 sections, each joined page its own; the mode removes the breaks and saves no pages. Header, footer and page-numbering behaviour was not examined in the sample, so check those in your own files.

If the page numbers come from a user

Join records the operation and does not look at the range. Save runs the joins and checks it, so a request for handbook pages 12-13 from a 10-page document fails there, with no output file.

Directory.CreateDirectory("output");

try
{
    using var merger = new Merger("resources/cover.docx");
    merger.Join("resources/handbook.docx", new PageJoinOptions(12, 13));
    merger.Save("output/out-of-range.docx");
}
catch (GroupDocsMergerException exception)
{
    Console.WriteLine(exception.Message);
}

The exception is GroupDocs.Merger.Exceptions.GroupDocsMergerException, and its message is Specified argument was out of the range of valid values. (Parameter 'startPageNumber'). Read the page count with GetDocumentInfo before the options are built, and reject the request with your own message.

One habit worth copying

The sample reopens each saved file with a second Merger and reads GetDocumentInfo().PageCount. It turns every method into something you can assert on in a test: the page count of the output, and for the last method an exception and the absence of the output file. A range given to Join is not checked until Save runs.

Common pitfalls

  1. Putting a range on the first file. The options apply to joined files only, so the opening document arrives whole. Use ExtractPages instead.
  2. Waiting for Join to complain. It never does for a bad range; the exception comes from Save.

FAQ

Are page numbers one-based and the range inclusive? Yes. PageJoinOptions(2, 3) takes pages 2 and 3, and ExtractOptions(1, 2) keeps pages 1 and 2. The numbers refer to the page layout of each source file.

Do I get whole table rows or a slice of the table? You get the rows of the pages you name. Catalogue pages 2-3 carried the same rows that a page-by-page split writes for those pages, each page starting with the repeated header row.

Conclusion

Start with the plain range after a cover, and reach for the other joins only when your inputs ask for them: ExtractPages when the first file must be trimmed, RangeMode.OddPages for alternate pages, WordJoinMode.DisableSectionBreaks when the parts must read as one section, and a page-count check when a user chooses the pages.

See Also