''
'' 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 create a Word document with a Table of Contents (TOC)
'' that is built with custom TOC entries created using TC (TOC Entry Item) fields.
Public Class TcFieldOpts
Function CreateDocx() As GcWordDocument
Dim doc = New GcWordDocument()
Dim pageOpts = New PageFieldOptions(doc)
pageOpts.NumberStyle = NumberStyle.Decimal
Dim sectionPagesOpts = New SectionPagesFieldOptions(doc)
sectionPagesOpts.NumberStyle = NumberStyle.Decimal
Dim par = doc.Body.Sections.First.Headers(HeaderFooterType.Primary).Body.AddParagraph("Page ")
par.AddComplexField(pageOpts)
par.GetRange().Runs.Add(" of ")
par.AddComplexField(sectionPagesOpts)
Dim tocOpts = New TocFieldOptions(doc)
tocOpts.TcFields.Collect = True
tocOpts.EntryFormatting.CreateHyperlink = True
For Each style As TocStyleLevel In tocOpts.Styles
style.Collect = True
Next
doc.Body.Paragraphs.Add("Table of Contents", doc.Styles(BuiltInStyleId.TocHeading)).AddComplexField(tocOpts)
doc.Body.Paragraphs.Last().GetRange().Runs.Last.AddBreak(BreakType.Page)
Dim rnd = Util.NewRandom()
Dim i As Integer = 0
Do While i < rnd.Next(2, 4)
Dim headerText = $"Top-level header {i + 1}"
Dim p = doc.Body.AddParagraph(headerText, doc.Styles(BuiltInStyleId.MessageHeader))
Dim tcOpts = New TcFieldOptions(doc)
tcOpts.Content.Text = $"TC Field for {headerText}"
tcOpts.DisplayLevel = OutlineLevel.Level1
doc.Body.AddParagraph(Util.LoremIpsumPar()).AddComplexField(tcOpts)
Dim j As Integer = 0
Do While j < rnd.Next(2, 5)
headerText = $"Second-level header {j + 1}"
p = doc.Body.AddParagraph(vbTab & headerText, doc.Styles(BuiltInStyleId.MessageHeader))
tcOpts = New TcFieldOptions(doc)
tcOpts.Content.Text = $"TC Field for {headerText}"
tcOpts.DisplayLevel = OutlineLevel.Level2
doc.Body.AddParagraph(Util.LoremIpsumPar()).AddComplexField(tcOpts)
Dim k As Integer = 0
Do While k < rnd.Next(2, 6)
headerText = $"Third-level header {k + 1}"
p = doc.Body.AddParagraph(vbTab & vbTab & headerText, doc.Styles(BuiltInStyleId.MessageHeader))
tcOpts = New TcFieldOptions(doc)
tcOpts.Content.Text = $"TC Field for {headerText}"
tcOpts.DisplayLevel = OutlineLevel.Level3
doc.Body.AddParagraph(Util.LoremIpsumPar()).AddComplexField(tcOpts)
k += 1
Loop
j += 1
Loop
i += 1
Loop
doc.Body.AddParagraph("The End.", doc.Styles(BuiltInStyleId.IntenseQuote))
doc.UpdateFields(New GrapeCity.Documents.Word.Layout.WordLayoutSettings() With {
.FontCollection = Util.FontCollection,
.Culture = CultureInfo.GetCultureInfo("en-US")
})
Return doc
End Function
End Class