How to Work with Word Index and Tables in .NET

How to Work with Word Index and Tables in .NET

How to Work with Word Index and Tables in .NET

This guide shows how to insert and build the field-driven reference structures Word documents use to summarize their own content — a table of contents, an alphabetical index, and a table of authorities — with Aspose.Words FOSS for .NET. Each is compiled from marker fields elsewhere in the document: FieldToc compiles heading-based and FieldTC-marked entries, FieldIndex compiles FieldXE markers, and FieldToa compiles FieldTA citations. Every field class in this family derives from the base Field type in the Aspose.Words.Fields namespace.


Step-by-Step Guide

Step 1: Insert a Table of Contents

Call DocumentBuilder.InsertTableOfContents(switches) at the position where the TOC should appear, passing a Word-style field-switch string — for example "\o \"1-3\" \h \z \u" — to control which heading levels are included and how the result is formatted, the same switches Word writes when a TOC is inserted through its UI. This inserts a FieldToc, which also exposes the same structure directly as properties: HeadingLevelRange/EntryLevelRange for included outline levels, InsertHyperlinks, PreserveTabs/PreserveLineBreaks, and BookmarkName. For a table of figures instead of a table of contents, set TableOfFiguresLabel or CaptionlessTableOfFiguresLabel on the same FieldToc.


Step 2: Mark Custom Table of Contents Entries

Not every entry a TOC should include comes from a heading style. Insert a FieldTC at a specific location to mark it as a TOC entry independent of paragraph style: FieldTC.Text is the entry text, EntryLevel is the outline level it should appear at, and OmitPageNumber suppresses the page number for that entry. When a document needs more than one TOC — for example a main TOC and a separate table of figures — set FieldTC.TypeIdentifier to match the EntryIdentifier of the FieldToc that should collect it.


Step 3: Build an Index from Marked Entries

Mark each location that belongs in the index with a FieldXE, setting Text to the entry text; EntryType groups entries into sub-levels using colon-separated syntax, and PageRangeBookmarkName marks a bookmark whose extent becomes a page range in the index rather than a single page number. FieldIndex compiles every FieldXE marker in the document into the alphabetical result — set Heading to insert letter-heading separators (A, B, C…), LetterRange to restrict the index to part of the alphabet, and RunSubentriesOnSameLine to control sub-entry layout. PageNumberSeparator, PageRangeSeparator, SequenceSeparator, and CrossReferenceSeparator control the punctuation between an entry and its page reference. Word’s built-in index visual styles are available as the FieldIndexFormat enum (Template, Classic, Fancy, Modern, Bulleted, Formal, Simple).


Step 4: Build a Table of Authorities from Citations

Mark each citation in the text with a FieldTA: LongCitation and ShortCitation hold the citation text, EntryCategory groups it under a heading such as “Cases” or “Statutes”, and IsBold/IsItalic control its formatting. FieldToa compiles those citations into a table — EntryCategory selects which category to compile, UseHeading controls whether category headings appear, UsePassim applies the “passim” convention for sources cited many times, and PageRangeSeparator/PageNumberListSeparator/SequenceSeparator/EntrySeparator/RemoveEntryFormatting control layout.


Step 5: Refresh Fields After the Document Changes

A TOC, index, or table of authorities does not update itself when surrounding content changes. Call Update() on the specific field — for example FieldToc.Update() — to recompute its result, or FieldToc.UpdatePageNumbers() when only the page-number portion needs refreshing. To refresh every field in a document at once rather than one at a time, call Document.UpdateFields(). Remember that page numbers in results from these fields evaluate to placeholder values in this edition, since page layout and pagination are not part of it.


Step 6: Include Entries From Other Documents

Insert a FieldRD to have a TOC, index, or table of authorities pull in entries from another file when it compiles. FieldRD.FileName names the referenced document, and IsPathRelative controls whether that path resolves relative to the current document. Add one FieldRD per additional document and update the compiling field afterward so the combined result includes the referenced content.


Common Issues and Fixes

A table of contents doesn’t include expected headings. The TOC’s switch string (or HeadingLevelRange) excludes those outline levels. Adjust the switches passed to InsertTableOfContents, or set FieldToc.HeadingLevelRange directly.

An index entry doesn’t appear where expected. The corresponding FieldXE marker is missing, or its EntryType sub-level syntax doesn’t match what FieldIndex expects. Confirm the FieldXE markers exist and their EntryType values are formatted consistently.

Field results look stale after editing content. The field wasn’t updated after the edit. Call Update() on the specific field, or Document.UpdateFields() to refresh every field at once.

Entries from another document aren’t included when compiling. No FieldRD referencing that document exists, or IsPathRelative doesn’t match how the path should resolve. Add a FieldRD with the correct FileName and IsPathRelative setting.


Frequently Asked Questions

How do I insert a table of contents programmatically?

Call DocumentBuilder.InsertTableOfContents(switches) with a Word-style switch string, such as "\o \"1-3\" \h \z \u", at the position where the TOC should appear.

How do I mark text as an index entry?

Insert a FieldXE field at that location with its Text property set to the entry text; FieldIndex compiles all FieldXE markers in the document into the index result.

What’s the difference between FieldTC and a heading-based TOC entry?

Word’s default TOC behavior collects entries from paragraphs using heading styles. FieldTC marks an arbitrary location as a TOC entry independent of paragraph style, which FieldToc can include via matching EntryIdentifier/TypeIdentifier values.

How do I refresh a TOC or index after the document changes?

Call Update() on the specific field (or UpdatePageNumbers() on a FieldToc for page numbers only), or Document.UpdateFields() to refresh every field in the document.

Can a TOC or index be compiled across multiple documents?

Yes — insert a FieldRD pointing at each additional document via FileName. The compiling field (FieldToc, FieldIndex, or FieldToa) includes entries from referenced documents once it is updated.


See Also