NoteRefFieldOpts.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 NOTEREF field options to add
'' and customize a reference to a footnote or endnote marked by a
'' bookmark elsewhere in the document. A companion PAGEREF field
'' targeting the same bookmark shows that WordLayout can resolve a
'' page number for a bookmark anchored to a footnote/endnote mark.
Public Class NoteRefFieldOpts
    Function CreateDocx() As GcWordDocument
        Dim doc = New GcWordDocument()

        '' The names of the target bookmarks:
        Const fnBmk As String = "fnBookmark"
        Const enBmk As String = "enBookmark"

        Dim p1 = doc.Body.AddParagraph("See the footnote")
        p1.GetRange().Footnotes.Add("This is the footnote text.")
        '' The bookmark must be placed on the paragraph that contains the
        '' footnote's reference mark -- not on content inside the footnote's
        '' own body -- for NOTEREF/PAGEREF to be able to resolve it:
        p1.GetRange().Bookmarks.Add(fnBmk)

        p1.GetRange().Runs.Add(" and the endnote")
        p1.GetRange().Endnotes.Add("This is the endnote text.")
        p1.GetRange().Bookmarks.Add(enBmk)
        p1.GetRange().Runs.Add(".")

        '' Add some pages:
        Dim rnd = Util.NewRandom()
        Dim i As Integer = 0
        Do While i < rnd.Next(20, 25)
            doc.Body.AddParagraph(Util.LoremIpsumPar())
            i += 1
        Loop

        '' NOTEREF to the footnote, formatted like its own reference mark:
        Dim noteRefFn = New NoteRefFieldOptions(doc, fnBmk) With {.Hyperlink = True, .FormatAsReferenceMark = True}
        doc.Body.AddParagraph("Footnote reference mark: ").AddComplexField(noteRefFn)

        '' NOTEREF to the endnote, as a plain number instead of its (roman numeral) mark:
        Dim noteRefEn = New NoteRefFieldOptions(doc, enBmk) With {.Hyperlink = True, .FormatAsReferenceMark = False}
        doc.Body.AddParagraph("Endnote number: ").AddComplexField(noteRefEn)

        '' A PAGEREF to the same footnote bookmark shows that WordLayout can
        '' now resolve a page number for a bookmark anchored to a footnote mark:
        Dim pageRefFn = New PageRefFieldOptions(doc, fnBmk) With {.Hyperlink = True}
        doc.Body.AddParagraph("The footnote is on page: ").AddComplexField(pageRefFn)

        '' 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