StyleRefFieldOpts.vb
C# VB
'''' This code is part of Document Solutions for Word demos.'' Copyright (c) MESCIUS inc. All rights reserved.''Imports System.GlobalizationImports GrapeCity.Documents.WordImports GrapeCity.Documents.Word.Fields
'' This sample shows how to use the STYLEREF field options to build a'' "running reference" that always displays the text of the nearest'' preceding paragraph formatted with a given style (e.g. the current'' chapter Or section heading), similar to a running head in a printed'' book.'''' LIMITATION: in DsWord 9.2.0 STYLEREF fields placed in a page header'' or footer are not resolved when exporting to PDF/image.Public Class StyleRefFieldOpts Function CreateDocx() As GcWordDocument Dim doc = New GcWordDocument()
Dim h1 = doc.Styles(BuiltInStyleId.Heading1) Dim h2 = doc.Styles(BuiltInStyleId.Heading2)
'' A few chapters, each with a couple of subsections spread across '' multiple pages via filler text: Dim rnd = Util.NewRandom() For c As Integer = 1 To 3 doc.Body.AddParagraph($"Chapter {c}", h1) For s As Integer = 1 To 2 doc.Body.AddParagraph($"Chapter {c}, Section {s}", h2) Dim i As Integer = 0 Do While i < rnd.Next(4, 6) doc.Body.AddParagraph(Util.LoremIpsumPar()) i += 1 Loop
'' A "breadcrumb" callout in the middle of the body text: '' both fields always resolve to the nearest preceding '' Heading1/Heading2 paragraph, so this line stays correct '' even if content is inserted or reordered above it. Dim crumb = doc.Body.AddParagraph() crumb.GetRange().Runs.Add("You are reading: ") crumb.AddComplexField(New StyleRefFieldOptions(doc, h1.Name)) crumb.GetRange().Runs.Add(" → ") crumb.AddComplexField(New StyleRefFieldOptions(doc, h2.Name)) Next Next
'' Update fields using a specific culture: doc.UpdateFields(New GrapeCity.Documents.Word.Layout.WordLayoutSettings() With { .FontCollection = Util.FontCollection, .Culture = CultureInfo.GetCultureInfo("en-US") })
'' Done: Return doc End FunctionEnd Class