FieldToc Class
Implements the TOC field.
Inheritance Hierarchy
SystemObject
  Aspose.Words.FieldsField
    Aspose.Words.FieldsFieldToc

Namespace: Aspose.Words.Fields
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.11
Syntax
public class FieldToc : Field

The FieldToc type exposes the following members.

Constructors
  NameDescription
Public methodFieldToc
Initializes a new instance of the FieldToc class
Properties
  NameDescription
Public propertyCode exampleBookmarkName
Gets or sets the name of the bookmark that marks the portion of the document used to build the table.
Public propertyCode exampleCaptionlessTableOfFiguresLabel
Gets or sets the name of the sequence identifier used when building a table of figures that does not include caption's label and number.
Public propertyCode exampleCustomStyles
Gets or sets a list of styles other than the built-in heading styles to include in the table of contents.
Public propertyCode exampleDisplayResult
Gets the text that represents the displayed field result.
(Inherited from Field.)
Public propertyCode exampleEnd
Gets the node that represents the field end.
(Inherited from Field.)
Public propertyCode exampleEntryIdentifier
Gets or sets a string that should match type identifiers of TC fields being included.
Public propertyCode exampleEntryLevelRange
Gets or sets a range of levels of the table of contents entries to be included.
Public propertyCode exampleEntrySeparator
Gets or sets a sequence of characters that separate an entry and its page number.
Public propertyCode exampleFormat
Gets a FieldFormat object that provides typed access to field's formatting.
(Inherited from Field.)
Public propertyCode exampleHeadingLevelRange
Gets or sets a range of heading levels to include.
Public propertyCode exampleHideInWebLayout
Gets or sets whether to hide tab leader and page numbers in Web layout view.
Public propertyCode exampleInsertHyperlinks
Gets or sets whether to make the table of contents entries hyperlinks.
Public propertyCode exampleIsDirty
Gets or sets whether the current result of the field is no longer correct (stale) due to other modifications made to the document.
(Inherited from Field.)
Public propertyCode exampleIsLocked
Gets or sets whether the field is locked (should not recalculate its result).
(Inherited from Field.)
Public propertyIsPageNumberOmittingLevelRangeSpecified
Public propertyCode exampleLocaleId
Gets or sets the LCID of the field.
(Inherited from Field.)
Public propertyCode examplePageNumberOmittingLevelRange
Gets or sets a range of levels of the table of contents entries from which to omits page numbers.
Public propertyCode examplePrefixedSequenceIdentifier
Gets or sets the identifier of a sequence for which a prefix should be added to the entry's page number.
Public propertyCode examplePreserveLineBreaks
Gets or sets whether to preserve newline characters within table entries.
Public propertyCode examplePreserveTabs
Gets or sets whether to preserve tab entries within table entries.
Public propertyCode exampleResult
Gets or sets text that is between the field separator and field end.
(Inherited from Field.)
Public propertyCode exampleSeparator
Gets the node that represents the field separator. Can be null.
(Inherited from Field.)
Public propertyCode exampleSequenceSeparator
Gets or sets the character sequence that is used to separate sequence numbers and page numbers.
Public propertyCode exampleStart
Gets the node that represents the start of the field.
(Inherited from Field.)
Public propertyCode exampleTableOfFiguresLabel
Gets or sets the name of the sequence identifier used when building a table of figures.
Public propertyCode exampleType
Gets the Microsoft Word field type.
(Inherited from Field.)
Public propertyCode exampleUseParagraphOutlineLevel
Gets or sets whether to use the applied paragraph outline level.
Methods
  NameDescription
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodCode exampleGetFieldCode
Returns text between field start and field separator (or field end if there is no separator). Both field code and field result of child fields are included.
(Inherited from Field.)
Public methodCode exampleGetFieldCode(Boolean)
Returns text between field start and field separator (or field end if there is no separator).
(Inherited from Field.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Protected methodMemberwiseClone (Inherited from Object.)
Protected methodNeedStoreOldResultNodes (Inherited from Field.)
Public methodCode exampleRemove
Removes the field from the document. Returns a node right after the field. If the field's end is the last child of its parent node, returns its parent paragraph. If the field is already removed, returns null.
(Inherited from Field.)
Public methodToString (Inherited from Object.)
Public methodCode exampleUnlink
Performs the field unlink.
(Inherited from Field.)
Public methodCode exampleUpdate
Performs the field update. Throws if the field is being updated already.
(Inherited from Field.)
Public methodCode exampleUpdate(Boolean)
Performs a field update. Throws if the field is being updated already.
(Inherited from Field.)
Public methodCode exampleUpdatePageNumbers
Updates the page numbers for items in this table of contents.
Remarks
Builds a table of contents (which can also be a table of figures) using the entries specified by TC fields, their heading levels, and specified styles, and inserts that table at this place in the document.
Examples
Shows how to insert a TOC and populate it with entries based on heading styles.
public void FieldToc()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // The table of contents we will insert will accept entries that are only within the scope of this bookmark
    builder.StartBookmark("MyBookmark");

    // Insert a list num field using a document builder
    FieldToc fieldToc = (FieldToc)builder.InsertField(FieldType.FieldTOC, true);

    // Limit possible TOC entries to only those within the bookmark we name here
    fieldToc.BookmarkName = "MyBookmark";

    // Normally paragraphs with a "Heading n" style will be the only ones that will be added to a TOC as entries
    // We can set this attribute to include other styles, such as "Quote" and "Intense Quote" in this case
    fieldToc.CustomStyles = "Quote; 6; Intense Quote; 7";

    // Styles are normally separated by a comma (",") but we can use this property to set a custom delimiter
    doc.FieldOptions.CustomTocStyleSeparator = ";";

    // Filter out any headings that are outside this range
    fieldToc.HeadingLevelRange = "1-3";

    // Headings in this range won't display their page number in their TOC entry
    fieldToc.PageNumberOmittingLevelRange = "2-5";

    fieldToc.EntrySeparator = "-";
    fieldToc.InsertHyperlinks = true;
    fieldToc.HideInWebLayout = false;
    fieldToc.PreserveLineBreaks = true;
    fieldToc.PreserveTabs = true;
    fieldToc.UseParagraphOutlineLevel = false;

    InsertNewPageWithHeading(builder, "First entry", "Heading 1");
    builder.Writeln("Paragraph text.");
    InsertNewPageWithHeading(builder, "Second entry", "Heading 1");
    InsertNewPageWithHeading(builder, "Third entry", "Quote");
    InsertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");

    // These two headings will have the page numbers omitted because they are within the "2-5" range
    InsertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
    InsertNewPageWithHeading(builder, "Sixth entry", "Heading 3");

    // This entry will be omitted because "Heading 4" is outside of the "1-3" range we set earlier
    InsertNewPageWithHeading(builder, "Seventh entry", "Heading 4");

    builder.EndBookmark("MyBookmark");
    builder.Writeln("Paragraph text.");

    // This entry will be omitted because it is outside the bookmark specified by the TOC
    InsertNewPageWithHeading(builder, "Eighth entry", "Heading 1");

    Assert.AreEqual(" TOC  \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", fieldToc.GetFieldCode());

    fieldToc.UpdatePageNumbers();
    doc.UpdateFields();
    doc.Save(ArtifactsDir + "Field.FieldTOC.docx");
}

/// <summary>
/// Start a new page and insert a paragraph of a specified style
/// </summary>
public void InsertNewPageWithHeading(DocumentBuilder builder, string captionText, string styleName)
{
    builder.InsertBreak(BreakType.PageBreak);
    string originalStyle = builder.ParagraphFormat.StyleName;
    builder.ParagraphFormat.Style = builder.Document.Styles[styleName];
    builder.Writeln(captionText);
    builder.ParagraphFormat.Style = builder.Document.Styles[originalStyle];
}
See Also