💡 Full working example available on GitHub: build-word-document-from-selected-pages-dotnet

Building a Word document from selected pages is a GroupDocs.Merger workflow for .NET that takes chosen page ranges from several DOCX files and writes them into one new document.

The Old Way Was Painful

A page is the natural unit for anyone who assembles a document from other documents. You say “take pages 2 and 3 of the catalogue” because that is how a person reads it. A DOCX file does not store pages, though. It stores paragraphs, tables and section settings, and the page boundaries appear only when a layout engine fills pages with them.

That gap showed whenever a table ran across pages. The 26.11 release notes describe how earlier versions handled it: “a table spanning several pages came back whole as one page, and the remaining pages were copies of the first page or empty documents.” A page range over such a table therefore did not return the pages you asked for.

The notes name two separate failures. The table arrived as a single page, and the pages after it were filled with copies of the first page or with empty documents.

There’s a Better Way

Since 26.11, page operations on Word documents, Join with page ranges included, follow the real Word page layout, tables that span pages included. The sample project puts that to the test with a table-heavy file. parts-catalogue.docx has 15 pages: a title, an intro and three sections of 140, 120 and 160 rows, 420 in total, with a page break before the second and third section and a header row marked to repeat. Beside it sit cover.docx, a single page reading “SAMPLE DOCUMENT 2” and “Page 1”, and the 10-page handbook.docx from the GroupDocs.Merger examples.

You need the .NET 8.0 SDK and the NuGet package. Without a licence the library runs in evaluation mode and evaluation limits apply, so take a temporary licence before you judge any output.

dotnet add package GroupDocs.Merger --version 26.11.0

Does Join return the table rows of the pages I name?

Yes. Since 26.11, Join with PageJoinOptions reads pages from the real Word layout, so a range over a table that spans pages returns the pages you name, with the table rows that belong to them. In the sample run, the rows of catalogue pages 2-3 matched what a page-by-page split writes, and each page began with the repeated table header row.

The New Way: Ask for Pages

The pack is a cover, then catalogue pages 2-3, then handbook pages 2-3. The code opens a Merger on the cover, queues two joins with a PageJoinOptions each, saves, and reopens the result to read its page count.

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 pack = new Merger("output/briefing.docx");
Console.WriteLine(pack.GetDocumentInfo().PageCount);

The saved file reports 5 pages: the cover and two pages from each source. What matters more is what the two catalogue pages contain. Pages 2-3 brought the rows PV-027 to PV-088, which are the rows that splitting the catalogue page by page writes for page 2 (PV-027 to PV-057) and page 3 (PV-058 to PV-088). Each of those pages starts with the repeated table header row. A range now returns pages, and pages come with their table rows.

The other four methods in the sample change one thing each.

A first document that is only partly wanted. PageJoinOptions apply to joined files, so they cannot trim the file the Merger is opened on. The sample opens the catalogue, calls ExtractPages with ExtractOptions(1, 2), and then joins handbook pages 2-3 to what is left. The digest has 4 pages.

Alternate pages. The third argument of PageJoinOptions is a RangeMode. Passing RangeMode.OddPages with the handbook range 1-3 added pages 1 and 3 behind the cover, and that output has 3 pages.

Fewer seams. WordJoinOptions takes the same page range and adds a Mode. With WordJoinMode.DisableSectionBreaks the cover plus catalogue pages 2-3 became a single section. In the rendered result, the table header row and PV-027 start right under the cover text. The output has 3 pages, which is also what the default join of that range gave, with 3 sections, each joined page its own. The mode removes breaks; it does not save pages.

A range that does not exist. Join only records the operation, and Save checks the range. Requesting handbook pages 12-13 of a 10-page document made Save throw GroupDocs.Merger.Exceptions.GroupDocsMergerException with the message Specified argument was out of the range of valid values. (Parameter 'startPageNumber'), and no output file was written. For ranges typed by users, read the page count with GetDocumentInfo first.

Before vs. After

The left column is the behaviour the release notes describe for earlier versions. The right column is what the 26.11 sample run produced.

Before 26.11 With 26.11
Table spanning pages Came back whole as one page Rows of the pages you name
Other pages of the range Copies of the first page or empty documents The pages themselves
Catalogue pages 2-3 Not measured; the notes describe spanning tables in general PV-027 to PV-088, equal to the page-by-page split

The page numbers passed to PageJoinOptions are one-based and inclusive, and each range is read against the layout of its own source file: catalogue page 2 means page 2 of the catalogue, not page 2 of the output.

A Pack You Can Check

The sample is small enough to review in a few minutes. Run the project, open output/briefing.docx, and look at pages 2 and 3: both begin with the table header row, followed by the catalogue rows of the pages you named. Open briefing-one-section.docx and confirm that the table follows the cover text directly. The output folder ends up with briefing.docx, digest.docx, odd-pages.docx and briefing-one-section.docx; out-of-range.docx is never written. The sample’s Main runs all five methods, prints a page count after each and exits with a non-zero code when one of them does not behave as expected (Program.cs, lines 21-62).

Headers, footers and page numbering across the joined parts were not examined in the sample, so a document that depends on them needs its own check. The same goes for any source whose layout differs from the three files used here.

What Else Can You Do with GroupDocs.Merger for .NET?

  • Page-level extraction on its own: the extract pages documentation covers keeping a subset of one document without any join.
  • Joins across several source documents: the merge pages from various documents page shows the same idea from the documentation side.
  • Word-specific join options: the merge Word documents page lists what WordJoinOptions adds.

Conclusion

Before 26.11, as the release notes put it, a table spanning several pages came back whole as one page. With the 26.11 layout-based pages, the sample’s four joins produced 5, 4, 3 and 3 pages from three files, and the fifth failed cleanly at Save. The catalogue rows that came with the pages matched a page-by-page split.

Ready to try it?

Additional Resources