šŸ’” Full working example available on GitHub: split-documents-into-page-chunks-for-rag-dotnet

Page chunking for RAG ingestion is a GroupDocs.Merger workflow for .NET that cuts Word and PowerPoint files into page-level files, so each piece fits the page or upload limit of a retrieval-augmented generation (RAG) pipeline.

The RAG Intake Challenge: When a Document Is Longer Than the Limit

Suppose your ingestion service, AI tool or upload form accepts only a few pages per file, and the source is a 15-page spare-parts catalogue, a 10-page handbook or a deck of 6 slides. The files are Word and PowerPoint, and keeping the pieces in those formats avoids a conversion step.

The cut has to happen in code if documents arrive continuously, and it should respect pages: a page number is something an answer can cite, and a character-count split does not know where a page ends. What you need is a page-aware cut in .NET that reads DOCX and PPTX, writes DOCX and PPTX, and handles tables that run across a page boundary.

Sample snapshot: the catalogue in this article has 15 pages and 420 table rows. It is made up for the sample: a title, an introduction and three sections (pump and valve PV with 140 rows, motor and drive MD with 120, seal and gasket SG with 160), with a page break before sections 2 and 3.

What Changed in 26.11

Word pages are only known once the layout is calculated, which is why table-heavy files were hard to cut by page. GroupDocs.Merger 26.11 changed this. The release notes say that page operations on Word documents, such as splitting by pages and intervals and ExtractPages, now follow the real Word page layout, and they describe the earlier behaviour like this: “Before, a table spanning several pages came back whole as one page, and the remaining pages were copies of the first page or empty documents.”

The Solution: GroupDocs.Merger Inside the Ingestion Step

GroupDocs.Merger for .NET gives the ingestion step three calls. GetDocumentInfo reports the page count, Split cuts the document by page numbers, and ExtractPages keeps only the pages you name. The same calls work on DOCX and PPTX, and each output file is written in the format of its source.

To follow the code, you need:

  • .NET 8 or later
  • GroupDocs.Merger for .NET 26.11 (get a temporary license)
  • A Word or PowerPoint file longer than your page budget

Without a licence the library runs in evaluation mode and evaluation limits apply, so it reads only the first pages. Install the package with one command:

dotnet add package GroupDocs.Merger --version 26.11.0

Implementing Page Chunks Step by Step

The walk-through cuts the 15-page catalogue into chunks of at most 4 pages. The same method cut the 6-slide deck into chunks of 2 slides.

Step 1 - Read the Page Count

Open the document and ask how long it is, so that no page number is written into the code.

string sourcePath = "resources/parts-catalogue.docx";
int maxPages = 4;

using var merger = new Merger(sourcePath);
var info = merger.GetDocumentInfo();
Console.WriteLine($"pages: {info.PageCount}");

Step 2 - Turn the Budget into Chunk Starts

SplitMode.Interval wants the first page of every chunk after the first, not chunk lengths.

int[] chunkStarts = Enumerable.Range(1, (info.PageCount - 1) / maxPages)
    .Select(chunk => chunk * maxPages + 1)
    .ToArray();

For 15 pages and a budget of 4 the starts are 5, 9 and 13, so the parts are pages 1-4, 5-8, 9-12 and 13-15.

Step 3 - Split in Interval Mode

Directory.CreateDirectory("output");
string outputPattern = "output/catalogue-chunk_{0}.{1}";

merger.Split(new SplitOptions(outputPattern, chunkStarts, SplitMode.Interval));

In the pattern, {0} is the chunk index and {1} the file extension, so a deck keeps .pptx. If the page count is within the budget, the list of starts is empty and the sample saves the document as one chunk instead of splitting it.

Step 4 - Go Down to One Page, or Pull Out Only What You Need

Page-level retrieval can use one file per page: list every page and leave the mode at its default, SplitMode.Pages. Each operation below opens its own Merger.

int[] everyPage = Enumerable.Range(1, info.PageCount).ToArray();

using (var pages = new Merger(sourcePath))
{
    pages.Split(new SplitOptions("output/page_{0}.{1}", everyPage));
}

When retrieval already knows the pages, extract them instead.

using (var source = new Merger(sourcePath))
{
    source.ExtractPages(new ExtractOptions(new[] { 6, 7 }));
    source.Save("output/catalogue-pages-6-7.docx");
}

Will a Page Chunk Cut a Table in the Middle?

Yes, where Word breaks the page. In the 4-page chunks of the sample catalogue, every one of the 420 rows appears exactly once and in order, so no row is lost or repeated. Every chunk after the first opens with the table’s repeated header row, which keeps the columns readable when a chunk is read on its own.

Real Workflow: The Catalogue in Four Chunks

In our run the console reported 15 pages, starts at 5, 9 and 13, and 4 chunks, and the second chunk opened at PV-120 and the third at MD-092.

Chunk Pages First row Last row
catalogue-chunk_0.docx 4 PV-001 PV-119
catalogue-chunk_1.docx 4 PV-120 MD-091
catalogue-chunk_2.docx 4 MD-092 SG-091
catalogue-chunk_3.docx 3 SG-092 SG-160

The ranges join without a gap or an overlap, which is the property a retrieval index needs: each row lives in one chunk, and each chunk is a file you can open at the right page. Chunks 1 and 2 also contain the header row of the next section, because each of those chunks crosses a section boundary. The extract from Step 4 does the same job at question time: pages 6 and 7 became a 2-page catalogue-pages-6-7.docx.

Choosing Between the Three Cuts

The three calls answer different questions, and the sample uses each for one of them. A page budget fits an upload cap: you state the limit once and the number of chunks follows from the page count, as the 15-page catalogue and the 6-slide deck showed. One file per page fits retrieval that returns a single page, because the file name tells you which page it came from. An extract fits the moment a question has been matched to pages, since the model then receives a short document instead of the whole catalogue.

The calls also combine. You can cut a long document once at ingestion, index the chunks, and call ExtractPages on the original later, when retrieval names the pages that answer a question. Each step reads and writes DOCX or PPTX, so no conversion sits between them. This article makes no claim about token counts or about the limits of any particular AI product; the page is the unit the sample works in.

Other Places Page Chunks Fit

Once the page cut is in place, the same API handles neighbouring jobs:

  • Slide decks: the 6-slide deck became three PPTX files of 2 slides each, starting at slides 3 and 5.
  • Handbooks: the 10-page handbook became 10 files, handbook-page_1.docx to handbook-page_10.docx.
  • Short documents: a file within the budget is saved whole as chunk 0.
  • Question-time extracts: a retrieval step passes the page numbers it found and gets back a small document in the source format.

Getting Started with Page Chunks in .NET

To try it on your own files:

Additional Resources