'''' This code is part of Document Solutions for Word demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.GlobalizationImportsGrapeCity.Documents.WordImportsGrapeCity.Documents.Word.Fields '' This sample shows how to add a TOC (Table of Contents) field to a Word document,'' and specify its options.PublicClassTocFieldOptsFunctionCreateDocx() AsGcWordDocumentDim doc = NewGcWordDocument() Dim tocOpts = NewTocFieldOptions(doc) tocOpts.EntryFormatting.CreateHyperlink = TrueForEach style AsTocStyleLevelIn tocOpts.StylesSelectCase style.LevelCaseOutlineLevel.Level1, OutlineLevel.Level2, OutlineLevel.Level3 style.Collect = TrueCaseElse style.Collect = FalseEndSelectNext Dim toc = doc.Body.Paragraphs.Add().AddComplexField(tocOpts) doc.Body.Paragraphs.Last().AddSectionBreak() '' Front matter section: page numbers use lowercase roman numerals,'' restarting at "i". The TOC field correctly picks up each section's'' own PageNumbering settings when it renders page number entries, so'' "Preface" below will show up in the TOC with a roman-numeral page:Dim frontMatterSection = doc.Body.Sections.Last frontMatterSection.PageSetup.PageNumbering.NumberStyle = NumberStyle.LowerRoman frontMatterSection.PageSetup.PageNumbering.RestartPageNumbering = True frontMatterSection.PageSetup.PageNumbering.StartingNumber = 1 doc.Body.AddParagraph("Preface", doc.Styles(BuiltInStyleId.Heading1)) doc.Body.AddParagraph(Util.LoremIpsumPar()) doc.Body.AddParagraph().AddSectionBreak() '' Another front-matter section using the same roman-numeral style, but'' *not* restarting the page count: it continues from "i" to "ii", making'' it clear this is still front matter, and that the page numbering only'' changes again -- back to arabic -- at the start of the body section:Dim foreword = doc.Body.Sections.Last foreword.PageSetup.PageNumbering.NumberStyle = NumberStyle.LowerRoman foreword.PageSetup.PageNumbering.RestartPageNumbering = False doc.Body.AddParagraph("Foreword", doc.Styles(BuiltInStyleId.Heading1)) doc.Body.AddParagraph(Util.LoremIpsumPar()) doc.Body.AddParagraph().AddSectionBreak() '' Body section: page numbers switch back to arabic numerals,'' restarting at 1, so the TOC's body entries show plain page numbers:Dim bodySection = doc.Body.Sections.Last bodySection.PageSetup.PageNumbering.NumberStyle = NumberStyle.Decimal bodySection.PageSetup.PageNumbering.RestartPageNumbering = True bodySection.PageSetup.PageNumbering.StartingNumber = 1 Dim rnd = Util.NewRandom()Dim i AsInteger = 0DoWhile i < rnd.Next(2, 4)Dim p = doc.Body.AddParagraph($"This is top-level header {i + 1}", doc.Styles(BuiltInStyleId.Heading1))Dim par = Util.LoremIpsumPar() doc.Body.AddParagraph(par)Dim j AsInteger = 0DoWhile j < rnd.Next(3, 5) p = doc.Body.AddParagraph($"This is second-level header {j + 1}", doc.Styles(BuiltInStyleId.Heading2)) par = Util.LoremIpsumPar() doc.Body.AddParagraph(par)Dim k AsInteger = 0DoWhile k < rnd.Next(2, 3) p = doc.Body.AddParagraph($"This is third-level header {k + 1}", doc.Styles(BuiltInStyleId.Heading3)) par = Util.LoremIpsumPar() doc.Body.AddParagraph(par) k += 1Loop j += 1Loop i += 1Loop Dim phdr = doc.Body.Sections.First.Headers(HeaderFooterType.Primary).Body.AddParagraph(doc.Styles(BuiltInStyleId.Closing)) phdr.AddComplexField(NewPageFieldOptions(doc) With {.NumberFormat = "'Page '0"}) doc.UpdateFields(NewGrapeCity.Documents.Word.Layout.WordLayoutSettings() With { .FontCollection = Util.FontCollection, .Culture = CultureInfo.GetCultureInfo("en-US") }) Return docEndFunctionEndClass