IndexFieldOpts.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 use the INDEX and XE field options
'' to add an index to a DOCX Word document.
Public Class IndexFieldOpts
    Private Shared s_indexWords As String() = {
        "lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "mauris", "id", "volutpat",
        "tellus", "ullamcorper", "sem", "praesent", "et", "ante", "laoreet", "lobortis", "nunc", "congue"
    }

    Function CreateDocx() As GcWordDocument
        Dim doc = New GcWordDocument()

        Const NPARS As Integer = 30
        For i As Integer = 0 To NPARS - 1
            Dim par = doc.Body.Paragraphs.Add()
            Dim txt = Util.LoremIpsumPar()

            Dim words = txt.Split(New Char() {" "c, ","c, "."c}, 2)
            If words.Length <> 2 Then
                Throw New Exception("Unexpected")
            End If

            Dim word = words(0)
            Dim xe = New XeFieldOptions(doc)
            xe.Entry.Content.Text = word
            par.AddComplexField(xe)
            par.AddRun(txt)
        Next
        doc.Body.Paragraphs.Last.AddSectionBreak()
        Dim p = doc.Body.Paragraphs.Add(doc.Styles(BuiltInStyleId.Index1))
        Dim index = New IndexFieldOptions(doc)
        index.Columns = 2
        index.Heading = "A"
        Dim field As ComplexField = p.AddComplexField(index)

        field.Update(New GrapeCity.Documents.Word.Layout.WordLayoutSettings() With {
            .FontCollection = Util.FontCollection,
            .Culture = CultureInfo.GetCultureInfo("en-US")
        })

        Return doc
    End Function
End Class