Recursive AI

Update a Word table of contents in C# without Interop

OneDocument is a free, open-source Word, Excel and PowerPoint document library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Fix the stale page number for “Routes” in guide.docx from the zip.

Terminal
dotnet add package RecursiveAI.OneDocument.Rendering

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneDocument.Documents;
using RecursiveAI.OneDocument.Layout;
 
using var doc = Document.Open("guide.docx");
doc.UpdateFields();
doc.SaveAs("guide-updated.docx");

Saves “Routes” on page 2 in guide-updated.docx. The page number is stored in the file, so you can read it without Word or Interop.

Unzip the example, then dotnet run.

The full program

The sample has a live TOC field, followed by a page break and a Heading 1 paragraph named “Routes.” The heading is on page 2 but the field still shows page 1. The program reads the field result before updating, then reopens the saved copy and reads it again, without updating the reopened file. The | in the output stands for a tab.

C#
using RecursiveAI.OneDocument.Documents;
using RecursiveAI.OneDocument.Layout;
 
using var doc = Document.Open("guide.docx");
PrintContents(doc, "Before");
 
var report = doc.UpdateFields();
doc.SaveAs("guide-updated.docx");
Console.WriteLine($"Tables rebuilt: {report.TablesBuilt}");
 
using var saved = Document.Open("guide-updated.docx");
PrintContents(saved, "Reopened");
 
static void PrintContents(Document doc, string label)
{
    var toc = doc.Fields().Single(field => field.Type == "TOC");
    Console.WriteLine($"{label}: {toc.ResultText.Replace("\t", " | ").Trim()}");
}

dotnet run prints:

Text
Before: Routes | 1
Tables rebuilt: 1
Reopened: Routes | 2

If the contents list is ordinary text instead of a TOC field, UpdateFields() won’t rebuild it. A locked field also keeps its stored result.

Variations

Read the page numbers without updating. Each entry in the rebuilt table of contents stores its page number in a PAGEREF field.

C#
using RecursiveAI.OneDocument.Documents;
 
using var saved = Document.Open("guide-updated.docx");
foreach (var field in saved.Fields())
    if (field.Type == "PAGEREF")
        Console.WriteLine(field.ResultText);
Text
2

The short answer

Install OneDocument’s Rendering package, open the DOCX and call doc.UpdateFields(), then save the file. This updates the document in memory: it rebuilds the table of contents and writes the computed page numbers into the field results. Just rendering a PDF is different: it writes the results to the rendered pages, not back into the DOCX.

Does updating a Word table of contents change its formatting?

UpdateFields() rebuilds the whole list; it’s not the same as changing only the page numbers. The new entries use TOC 1 through TOC 9 paragraph styles by level. If a style is missing, the library applies direct formatting, including indentation and a right tab with a dot leader. Keep your TOC formatting in those styles and check a saved copy if the old entries had manual formatting. See how entries are generated.

The entries go into the layout before page numbers are calculated. A longer contents list can push a heading to a later page, so the layout can take more than one pass. Page numbers are those of OneDocument’s layout; different fonts or wrapping can change pagination elsewhere.

Why isn’t my table of contents updating in Word?

Start by checking that it’s a real TOC field, not a manually typed list. Next check which content the field includes. For example, TOC \o "1-1" selects Heading 1 paragraphs by style name, so bold text styled as Normal won’t show up. A field can instead use outline levels (\u), specified styles (\t) or TC entry fields (\f); see the supported switches.

OneDocument doesn’t change locked fields. It also keeps the stored result when a TOC field isn’t closed or its code and result span separate containers. Look for layout.field-skipped in the update report’s Notes. If the field layout doesn’t settle before the configured pass limit, look for layout.fields-unsettled. Finally, make sure you save after the update and reopen the output file, not the original.

All onedocument recipes