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.
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 have
Use
Pages produced in the sample
A cover plus ranges of two files
PageJoinOptions per Join
5
A first file you want only partly
ExtractPages, then Join
4
Every other page of a range
RangeMode.OddPages
3
Parts that must flow on from the cover
WordJoinOptions with DisableSectionBreaks
3
Page numbers typed by a user
a GetDocumentInfo check before Save
rejected 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.
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.
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.
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.
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.
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
Putting a range on the first file. The options apply to joined files only, so the opening document arrives whole. Use ExtractPages instead.
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.