Split Documents into Page Chunks for RAG Pipelines 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: split-documents-into-page-chunks-for-rag-dotnet

Introduction

Page chunking for RAG ingestion is a GroupDocs.Merger capability for .NET that cuts a Word or PowerPoint file into smaller files along page boundaries, so each piece fits the page or upload limit of a retrieval-augmented generation (RAG) pipeline or an AI tool. A pipeline that accepts four pages per file cannot take a 15-page catalogue as it is, and a deck of six slides will not pass a limit of two. GroupDocs.Merger for .NET offers three ways to get under such a limit, and this guide compares them: a page budget, one file per page, and an extract of chosen pages. Each result is an ordinary DOCX or PPTX file in the format you started with.

Each method is judged by how it picks pages, what it writes to disk and which situation it fits. The console output and file names below come from a licensed run of the sample on GroupDocs.Merger 26.11.0.

What This Guide Covers

The guide compares SplitIntoPageBudget, SplitIntoSinglePages and ExtractPagesForQuestion, three methods of the sample project, for developers who feed Word and PowerPoint documents to a RAG pipeline or an AI tool with page or upload limits.

Prerequisites:

  • .NET 8 SDK or later and GroupDocs.Merger for .NET 26.11
  • A Word or PowerPoint file longer than your page budget; the sample uses a 15-page catalogue, a 10-page handbook and a 6-slide deck
Warning
⚠️ Without a licence the library runs in evaluation mode and evaluation limits apply: it reads only the first pages. Get a temporary licence before you judge chunk counts.

Installation

dotnet add package GroupDocs.Merger --version 26.11.0

The package is also listed on NuGet.org.

Repository Structure

split-documents-into-page-chunks-for-rag-dotnet/
β”œβ”€β”€ PageChunksForRag.csproj
β”œβ”€β”€ Program.cs
└── resources/
    β”œβ”€β”€ deck.pptx
    β”œβ”€β”€ handbook.docx
    └── parts-catalogue.docx

Program.cs holds the three methods: SplitIntoPageBudget, lines 78-108, SplitIntoSinglePages, lines 110-127 and ExtractPagesForQuestion, lines 129-148.

Quick Decision Matrix

Your limit or needRecommended methodWhy
A service accepts N pages per uploadPage budgetA few larger files, each within N pages
Answers must cite one page and open itOne file per pageEvery page is its own file
Retrieval already knows the relevant pagesExtractThe model gets only those pages
A document already fits the limitPage budgetIt is saved whole as a single chunk

Method Comparison Overview

MethodPages chosen byFiles writtenBest for
SplitIntoPageBudgetChunk starts, SplitMode.IntervalOne per chunkBatch ingestion under a page cap
SplitIntoSinglePagesEvery page listed, SplitMode.PagesOne per pagePage-level retrieval
ExtractPagesForQuestionChosen pages, ExtractOptionsOneOne focused question

Detailed Method Analysis

Method 1: Page Budget (SplitIntoPageBudget)

Overview: a limit expressed in pages becomes a list of chunk starts, and one Split call writes the chunks.

How It Works

GetDocumentInfo reports the page count, the sample derives the first page of every chunk after the first, and SplitOptions receives them with SplitMode.Interval. When the document already fits, the list is empty and the sample saves the whole file as chunk 0.

When to Use

  • βœ… A service caps pages per upload
  • βœ… Long files arrive in batches
  • ❌ Not for page-by-page citation; use Method 2

Implementation

string sourcePath = "resources/parts-catalogue.docx";
string outputPattern = "output/catalogue-chunk_{0}.{1}";
int maxPages = 4;

Directory.CreateDirectory("output");
using var merger = new Merger(sourcePath);
var info = merger.GetDocumentInfo();
int[] chunkStarts = Enumerable.Range(1, (info.PageCount - 1) / maxPages)
    .Select(chunk => chunk * maxPages + 1)
    .ToArray();

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

Pros and Cons

Advantages:

  • Few files, each within the budget
  • The budget is the only number you set

Limitations:

  • A chunk cannot be cited by page without extra bookkeeping

The catalogue reported 15 pages and starts 5, 9 and 13, and wrote four chunks of 4, 4, 4 and 3 pages. The 6-slide deck with a budget of 2 started chunks at 3 and 5 and wrote three PPTX files of 2 slides each. In our run every one of the 420 catalogue rows landed in exactly one chunk, in order, and each of the three later chunks opened with the repeated table header row.

Method 2: One File per Page (SplitIntoSinglePages)

Overview: every page number goes into the list, so each page becomes its own file.

How It Works

SplitOptions without a mode uses SplitMode.Pages, which writes one file for each listed page. The list is built from PageCount, so no page number is typed by hand.

When to Use

  • βœ… Retrieval returns single pages
  • βœ… A citation should name a page and open it
  • ❌ Not when many small files are a problem for your storage

Implementation

string sourcePath = "resources/handbook.docx";
string outputPattern = "output/handbook-page_{0}.{1}";

Directory.CreateDirectory("output");
using var merger = new Merger(sourcePath);
int pageCount = merger.GetDocumentInfo().PageCount;
int[] everyPage = Enumerable.Range(1, pageCount).ToArray();

merger.Split(new SplitOptions(outputPattern, everyPage));

Pros and Cons

Advantages:

  • The finest granularity the API offers
  • Predictable file names

Limitations:

  • File count equals page count

The 10-page handbook produced 10 files, handbook-page_1.docx to handbook-page_10.docx.

Method 3: Extract the Pages One Question Needs (ExtractPagesForQuestion)

Overview: keep chosen pages in a new document and drop the rest, so the model receives a short, focused file.

How It Works

ExtractPages takes the page numbers from ExtractOptions, and Save writes the result in the source format, so a DOCX stays a DOCX. The sample then reopens the saved file and reads its page count as a check.

When to Use

  • βœ… A retrieval step has already ranked the pages
  • βœ… The same long document answers many questions
  • ❌ Not for cutting a whole document into parts

Implementation

string sourcePath = "resources/parts-catalogue.docx";
string outputPath = "output/catalogue-pages-6-7.docx";

Directory.CreateDirectory("output");
using (var merger = new Merger(sourcePath))
{
    merger.ExtractPages(new ExtractOptions(new[] { 6, 7 }));
    merger.Save(outputPath);
}

using var extract = new Merger(outputPath);
Console.WriteLine($"pages in the extract: {extract.GetDocumentInfo().PageCount}");

Pros and Cons

Advantages:

  • Smallest input for the model
  • Works on any page list

Limitations:

  • Needs the page numbers from somewhere else

Pages 6 and 7 of the catalogue produced catalogue-pages-6-7.docx, and reopening it reported 2 pages.

Side-by-Side Feature Comparison

FeaturePage budgetOne file per pageExtract
Output unitChunk of several pagesSingle pageChosen pages
Mode or optionSplitMode.IntervalSplitMode.PagesExtractOptions
Short documentSaved whole as chunk 0One file per pageChosen pages only
Source format keptYesYesYes

Real-World Use Cases

Use Case 1: A catalogue behind a page cap

Challenge: a 15-page catalogue with 420 table rows exceeds a four-page limit. Solution: Method 1. Result: four chunks, with no row lost or repeated.

Use Case 2: A deck behind a slide cap

Challenge: a 6-slide deck exceeds a limit of two. Solution: Method 1 on a PPTX. Result: three files of 2 slides each.

Use Case 3: A question about two pages

Challenge: one answer needs only part of the catalogue. Solution: Method 3. Result: a 2-page document.

Common Pitfalls and How to Avoid Them

  1. Expecting the same file names from both split modes.

    • Problem: SplitMode.Pages names files by page number starting at 1, while SplitMode.Interval numbers chunks from 0.
    • Solution: build any lookup from the pattern you passed, not from a guess.
  2. Passing chunk lengths instead of chunk starts.

    • Problem: SplitMode.Interval cuts at the numbers you list, so lengths cut in the wrong places.
    • Solution: derive the starts from PageCount, as the sample does.
  3. Judging results from an unlicensed run.

    • Problem: evaluation limits apply and only the first pages are read.
    • Solution: apply a temporary licence before comparing chunk counts.

Notes

Page operations on Word documents follow the real Word page layout since GroupDocs.Merger 26.11; the 26.11 release notes describe the earlier behaviour. This guide makes no statement about token counts or about the limits of any particular AI product.

How do I choose between page budgets and single pages?

Choose a page budget when a service caps pages per upload and you want few, larger files. Choose single pages when retrieval should return one page at a time and cite its number. Both use SplitOptions; only the mode and the list of page numbers differ. If you need only a handful of known pages, skip splitting and call ExtractPages.

FAQ

Can the same code cut a PowerPoint file? Yes. SplitIntoPageBudget cut the 6-slide deck.pptx with the same calls as the Word catalogue.

What happens to a document shorter than the budget? The list of chunk starts is empty, so the sample saves the whole document as one chunk with the extension taken from info.Type.

Do the chunks stay in the original format? Yes. GroupDocs.Merger reads and writes DOCX and PPTX in their own format, so a catalogue chunk is a DOCX and a deck chunk is a PPTX.

Conclusion

Pick the method by the limit you face: a page budget for a cap per upload, single pages for page-level citation, an extract for one question. All three use GetDocumentInfo, Split or ExtractPages, and all three keep the source format. Start from the sample repository, swap in your own files and a licence, and compare the chunk counts with your limit.

See Also