SectionFieldOpts.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 SECTION and SECTIONPAGES field options
'' to insert the number of the current section and the number of pages in
'' the current section into the document.
Public Class SectionFieldOpts
    Function CreateDocx() As GcWordDocument
        Dim doc = New GcWordDocument()

        Dim sectionFldOpts = New SectionFieldOptions(doc)
        sectionFldOpts.NumberStyle = NumberStyle.UpperRoman

        Dim sectionPagesFldOpts = New SectionPagesFieldOptions(doc)
        sectionPagesFldOpts.NumberFormat = "', '0' page(s)'"

        Dim rnd = Util.NewRandom()
        Dim numSections = rnd.Next(3, 6)
        For i As Integer = 0 To numSections - 1
            Dim p = doc.Body.AddParagraph("Section ", doc.Styles(BuiltInStyleId.Heading1))
            p.AddComplexField(sectionFldOpts)
            p.AddComplexField(sectionPagesFldOpts)
            For j As Integer = 0 To rnd.Next(5, 10) - 1
                Dim par = Util.LoremIpsumPar()
                doc.Body.AddParagraph(par)
            Next
            If i < numSections - 1 Then
                doc.Body.Paragraphs.Last.AddSectionBreak(SectionStart.Continuous)
            End If
        Next

        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