TocFieldOpts.cs
C# VB
//// This code is part of Document Solutions for Word demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System.Linq;using System.Globalization;using GrapeCity.Documents.Word;using GrapeCity.Documents.Word.Fields;
namespace DsWordWeb.Demos{ // This sample shows how to add a TOC (Table of Contents) field to a Word document, // and specify its options. public class TocFieldOpts { public GcWordDocument CreateDocx() { var doc = new GcWordDocument();
// The TocFieldOptions class provides a convenient strong-typed access // to options specific to the 'TOC' MS Word field: var tocOpts = new TocFieldOptions(doc); tocOpts.EntryFormatting.CreateHyperlink = true; // Include paragraphs formatted only with 'Heading 1', 'Heading 2' or 'Heading 3' styles: foreach (TocStyleLevel style in tocOpts.Styles) { switch (style.Level) { case OutlineLevel.Level1: case OutlineLevel.Level2: case OutlineLevel.Level3: style.Collect = true; break; default: style.Collect = false; break; } }
// Add TOC and a section break to the document: var toc = doc.Body.Paragraphs.Add().AddComplexField(tocOpts); doc.Body.Paragraphs.Last().AddSectionBreak();
// Front matter section: page numbers use lowercase roman numerals, // restarting at "i". The TOC field correctly picks up each section's // own PageNumbering settings when it renders page number entries, so // "Preface" below will show up in the TOC with a roman-numeral page: var frontMatterSection = doc.Body.Sections.Last; frontMatterSection.PageSetup.PageNumbering.NumberStyle = NumberStyle.LowerRoman; frontMatterSection.PageSetup.PageNumbering.RestartPageNumbering = true; frontMatterSection.PageSetup.PageNumbering.StartingNumber = 1;
doc.Body.AddParagraph("Preface", doc.Styles[BuiltInStyleId.Heading1]); doc.Body.AddParagraph(Util.LoremIpsumPar()); doc.Body.AddParagraph().AddSectionBreak();
// Another front-matter section using the same roman-numeral style, but // *not* restarting the page count: it continues from "i" to "ii", making // it clear this is still front matter, and that the page numbering only // changes again -- back to arabic -- at the start of the body section: var foreword = doc.Body.Sections.Last; foreword.PageSetup.PageNumbering.NumberStyle = NumberStyle.LowerRoman; foreword.PageSetup.PageNumbering.RestartPageNumbering = false;
doc.Body.AddParagraph("Foreword", doc.Styles[BuiltInStyleId.Heading1]); doc.Body.AddParagraph(Util.LoremIpsumPar()); doc.Body.AddParagraph().AddSectionBreak();
// Body section: page numbers switch back to arabic numerals, // restarting at 1, so the TOC's body entries show plain page numbers: var bodySection = doc.Body.Sections.Last; bodySection.PageSetup.PageNumbering.NumberStyle = NumberStyle.Decimal; bodySection.PageSetup.PageNumbering.RestartPageNumbering = true; bodySection.PageSetup.PageNumbering.StartingNumber = 1;
// Create random content with 3 levels of headers: var rnd = Util.NewRandom(); for (int i = 0; i < rnd.Next(2, 4); i++) { var p = doc.Body.AddParagraph($"This is top-level header {i + 1}", doc.Styles[BuiltInStyleId.Heading1]); var par = Util.LoremIpsumPar(); doc.Body.AddParagraph(par); for (int j = 0; j < rnd.Next(3, 5); j++) { p = doc.Body.AddParagraph($"This is second-level header {j + 1}", doc.Styles[BuiltInStyleId.Heading2]); par = Util.LoremIpsumPar(); doc.Body.AddParagraph(par); for (int k = 0; k < rnd.Next(2, 3); k++) { p = doc.Body.AddParagraph($"This is third-level header {k + 1}", doc.Styles[BuiltInStyleId.Heading3]); par = Util.LoremIpsumPar(); doc.Body.AddParagraph(par); } } }
// For reference add a simple page header with page number to the document: var phdr = doc.Body.Sections.First.Headers[HeaderFooterType.Primary].Body.AddParagraph(doc.Styles[BuiltInStyleId.Closing]); phdr.AddComplexField(new PageFieldOptions(doc) { NumberFormat = "'Page '0" });
// Update fields using a specific culture: doc.UpdateFields(new GrapeCity.Documents.Word.Layout.WordLayoutSettings() { FontCollection = Util.FontCollection, Culture = CultureInfo.GetCultureInfo("en-US") });
// Done: return doc; } }}