FieldTocUseParagraphOutlineLevel Property |
Namespace: Aspose.Words.Fields
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.TOC.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]; }