PageRefFieldOpts.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 PAGEREF field options to add
'' and customize a reference to a page in the current document
'' containing a specified bookmark.
Public Class PageRefFieldOpts
    Function CreateDocx() As GcWordDocument
        Dim doc = New GcWordDocument()

        Const bmkName As String = "myBookmark"

        Dim pgRefOpt = New PageRefFieldOptions(doc, bmkName)
        pgRefOpt.NumberStyle = NumberStyle.UpperRoman
        pgRefOpt.Hyperlink = True

        Dim p = doc.Body.AddParagraph("Go to page ", doc.Styles(BuiltInStyleId.IndexHeading)).AddComplexField(pgRefOpt)
        p.GetRange().Runs.Add("...")

        Dim rnd = Util.NewRandom()
        For i As Integer = 0 To rnd.Next(20, 25) - 1
            doc.Body.AddParagraph(Util.LoremIpsumPar())
        Next
        doc.Body.AddParagraph("The end.").GetRange().Bookmarks.Add(bmkName)

        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