RefFieldOpts.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 REF field options to add
'' and customize a reference to a bookmarked paragraph elsewhere
'' in the document, either as the paragraph's own list number or
'' as a copy of its text.
Public Class RefFieldOpts
    Function CreateDocx() As GcWordDocument
        Dim doc = New GcWordDocument()

        doc.Body.AddParagraph("A numbered list of requirements:")

        '' A ListTemplate is used to make paragraphs part of a numbered list:
        Dim listTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.NumberDefault, "reqList")

        '' The name of the target bookmark:
        Const bmkName As String = "reqBookmark"

        Dim bookmarkedItem As Paragraph = Nothing
        For i As Integer = 1 To 5
            Dim p = doc.Body.AddParagraph($"Requirement {i}: the system shall do thing #{i}.")
            p.ListFormat.Template = listTemplate
            p.ListFormat.LevelNumber = 0
            '' This ensures item spacing consistent with MS Word:
            p.Style = doc.Styles(BuiltInStyleId.ListParagraph)
            If i = 3 Then
                bookmarkedItem = p
            End If
        Next
        '' Bookmark the 3rd requirement so it can be referenced elsewhere:
        bookmarkedItem.GetRange().Bookmarks.Add(bmkName)

        '' The RefFieldOptions class provides a convenient strong-typed access
        '' to options specific to the 'REF' MS Word field. This one pulls in
        '' the bookmarked item's own list number:
        Dim refByNumber = New RefFieldOptions(doc, bmkName) With {
            .DisplayListNumber = DisplayListNumber.AllLevels,
            .Hyperlink = True
        }
        Dim p1 = doc.Body.AddParagraph("As specified in item ").AddComplexField(refByNumber)
        p1.GetRange().Runs.Add(" above, the system shall comply.")

        '' This REF pulls in the bookmarked item's text instead, upper-cased:
        Dim refByText = New RefFieldOptions(doc, bmkName) With {
            .StringStyle = StringStyle.Upper
        }
        doc.Body.AddParagraph("For emphasis: ").AddComplexField(refByText)

        '' Update fields using a specific culture:
        doc.UpdateFields(New GrapeCity.Documents.Word.Layout.WordLayoutSettings() With {
            .FontCollection = Util.FontCollection,
            .Culture = CultureInfo.GetCultureInfo("en-US")
        })

        '' Done:
        Return doc
    End Function
End Class