TocFieldOpts.vb
''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.Globalization
Imports GrapeCity.Documents.Word
Imports 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()

        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 Function
End Class