RdFieldOpts.vb
''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.Collections.Generic
Imports System.Globalization
Imports System.IO
Imports GrapeCity.Documents.Word
Imports GrapeCity.Documents.Word.Fields

'' This sample shows how to add an RD field to a Word document,
'' and specify its options.
Public Class RdFieldOpts
    Function CreateDocx() As GcWordDocument
        Const SubDocCount As Integer = 3
        Dim tFnames = New List(Of String)()
        For i As Integer = 1 To SubDocCount
            Dim subDoc = New GcWordDocument()

            subDoc.Body.Sections.First.PageSetup.PageNumbering.StartingNumber = i + 1

            subDoc.Body.AddParagraph($"Heading from sub-document {i}.", subDoc.Styles(BuiltInStyleId.Heading1))

            Dim ta = New TaFieldOptions(subDoc)
            ta.Category = 1
            ta.LongCitation.Text = $"Table of authorities entry from sub-document {i}."
            Dim p = subDoc.Body.AddParagraph()
            p.AddComplexField(ta)
            p.AddRun($"Table of authorities entry from sub-document {i}.")

            Dim xe = New XeFieldOptions(subDoc)
            xe.Entry.Content.Text = $"Index entry from sub-document {i}"
            p = subDoc.Body.AddParagraph()
            p.AddComplexField(xe)
            p.AddRun($"Index entry from sub-document {i}.")

            tFnames.Add(Path.GetTempFileName())
            subDoc.Save(tFnames.Last())
        Next

        Dim doc = New GcWordDocument()

        Dim toc = New TocFieldOptions(doc)
        Dim h = doc.Body.AddParagraph("Table of Contents", doc.Styles(BuiltInStyleId.Title))
        h.AddComplexField(toc)

        Dim toa = New ToaFieldOptions(doc)
        doc.Body.AddParagraph("Table of Authorities", doc.Styles(BuiltInStyleId.Title))
        h = doc.Body.AddParagraph(doc.Styles(BuiltInStyleId.Title))
        h.AddComplexField(toa)

        Dim index = New IndexFieldOptions(doc)
        index.PageNumbers.Separator = vbTab
        h = doc.Body.AddParagraph("Index", doc.Styles(BuiltInStyleId.Title))
        h.AddComplexField(index)

        For i As Integer = 1 To SubDocCount
            Dim rd = New RdFieldOptions(doc) With {
                .FilePath = ToUri(tFnames(i - 1)).ToString(),
                .Relative = False
            }
            h = doc.Body.AddParagraph()
            h.AddComplexField(rd)
        Next

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

        tFnames.ForEach(Sub(f) File.Delete(f))
        Return doc
    End Function

    Private Shared Function ToUri(pathOrUrl As String) As Uri
        Dim existing As Uri = Nothing
        If Uri.TryCreate(pathOrUrl, UriKind.Absolute, existing) Then
            Return existing
        End If
        Return New Uri(Path.GetFullPath(pathOrUrl))
    End Function
End Class