AddTocToDocx.vb
C# VB
'''' This code is part of Document Solutions for Word demos.'' Copyright (c) MESCIUS inc. All rights reserved.''Imports System.GlobalizationImports System.IOImports GrapeCity.Documents.WordImports GrapeCity.Documents.Word.Fields
'' This sample shows how to insert a TOC (Table of Contents) field'' into an existing DOCX Word document.Public Class AddTocToDocx Function CreateDocx() As GcWordDocument Dim doc = New GcWordDocument() doc.Load(Path.Combine("Resources", "WordDocs", "JsFrameworkExcerpt.docx"))
Dim tocOpts = New TocFieldOptions(doc) tocOpts.EntryFormatting.CreateHyperlink = True
Dim fo = New FindOptions(doc) fo.FormattingOptions.ParagraphFormat.PageBreakBefore = True Dim find = doc.Body.Find("", fo).FirstOrDefault() Dim para As Paragraph If find IsNot Nothing Then para = find.Range.ParentParagraph.GetRange().Paragraphs.Insert("Table of Contents", doc.Styles(BuiltInStyleId.TocHeading), InsertLocation.Before) para = para.GetRange().Paragraphs.Insert(InsertLocation.After) Else para = doc.Body.Paragraphs.Insert("Table of Contents", doc.Styles(BuiltInStyleId.TocHeading), InsertLocation.Start) End If
para.AddComplexField(tocOpts).Update(New GrapeCity.Documents.Word.Layout.WordLayoutSettings() With { .FontCollection = Util.FontCollection, .Culture = CultureInfo.GetCultureInfo("en-US") })
Return doc End FunctionEnd Class