TocFieldOpts.vb
C# VB
'''' This code is part of Document Solutions for Word demos.'' Copyright (c) MESCIUS inc. All rights reserved.''Imports System.GlobalizationImports GrapeCity.Documents.WordImports GrapeCity.Documents.Word.Fields
'' This sample shows how to add a TOC (Table of Contents) field to a Word document,'' and specify its options.Public Class TocFieldOpts Function CreateDocx() As GcWordDocument Dim doc = New GcWordDocument()
Dim tocOpts = New TocFieldOptions(doc) tocOpts.EntryFormatting.CreateHyperlink = True For Each style As TocStyleLevel In tocOpts.Styles Select Case style.Level Case OutlineLevel.Level1, OutlineLevel.Level2, OutlineLevel.Level3 style.Collect = True Case Else style.Collect = False End Select Next
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 As Integer = 0 Do While 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 As Integer = 0 Do While 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 As Integer = 0 Do While 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 += 1 Loop j += 1 Loop i += 1 Loop
Dim phdr = doc.Body.Sections.First.Headers(HeaderFooterType.Primary).Body.AddParagraph(doc.Styles(BuiltInStyleId.Closing)) phdr.AddComplexField(New PageFieldOptions(doc) With {.NumberFormat = "'Page '0"})
doc.UpdateFields(New GrapeCity.Documents.Word.Layout.WordLayoutSettings() With { .FontCollection = Util.FontCollection, .Culture = CultureInfo.GetCultureInfo("en-US") })
Return doc End FunctionEnd Class