//// This code is part of Document Solutions for Word demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem.Linq;usingSystem.Globalization;usingGrapeCity.Documents.Word;usingGrapeCity.Documents.Word.Fields; namespaceDsWordWeb.Demos{// 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.publicclassTcFieldOpts {publicGcWordDocumentCreateDocx() {var doc = newGcWordDocument(); // Add a page header:var pageOpts = newPageFieldOptions(doc); pageOpts.NumberStyle = NumberStyle.Decimal;var sectionPagesOpts = newSectionPagesFieldOptions(doc); sectionPagesOpts.NumberStyle = NumberStyle.Decimal;var par = doc.Body.Sections.First.Headers[HeaderFooterType.Primary].Body.AddParagraph("Page "); par.AddComplexField(pageOpts); par.GetRange().Runs.Add(" of "); par.AddComplexField(sectionPagesOpts); // Add a 'TOC' field:var tocOpts = newTocFieldOptions(doc);// Tell TOC to collect TC fields (it is off by default): tocOpts.TcFields.Collect = true;// Set other options: tocOpts.EntryFormatting.CreateHyperlink = true;// Tell the TOC to include all outline levels:foreach (TocStyleLevel style in tocOpts.Styles) style.Collect = true; // Add TOC and a page break to the beginning of the document: doc.Body.Paragraphs.Add("Table of Contents", doc.Styles[BuiltInStyleId.TocHeading]).AddComplexField(tocOpts); doc.Body.Paragraphs.Last().GetRange().Runs.Last.AddBreak(BreakType.Page); // Create random content with TC fields at 3 nesting levels:var rnd = Util.NewRandom();for (int i = 0; i < rnd.Next(2, 4); i++) {var headerText = $"Top-level header {i + 1}";var p = doc.Body.AddParagraph(headerText, doc.Styles[BuiltInStyleId.MessageHeader]); var tcOpts = newTcFieldOptions(doc); tcOpts.Content.Text = $"TC Field for {headerText}"; tcOpts.DisplayLevel = OutlineLevel.Level1; doc.Body.AddParagraph(Util.LoremIpsumPar()).AddComplexField(tcOpts); for (int j = 0; j < rnd.Next(2, 5); j++) { headerText = $"Second-level header {j + 1}"; p = doc.Body.AddParagraph("\t" + headerText, doc.Styles[BuiltInStyleId.MessageHeader]); tcOpts = newTcFieldOptions(doc); tcOpts.Content.Text = $"TC Field for {headerText}"; tcOpts.DisplayLevel = OutlineLevel.Level2; doc.Body.AddParagraph(Util.LoremIpsumPar()).AddComplexField(tcOpts); for (int k = 0; k < rnd.Next(2, 6); k++) { headerText = $"Third-level header {k + 1}"; p = doc.Body.AddParagraph("\t\t" + headerText, doc.Styles[BuiltInStyleId.MessageHeader]); tcOpts = newTcFieldOptions(doc); tcOpts.Content.Text = $"TC Field for {headerText}"; tcOpts.DisplayLevel = OutlineLevel.Level3; doc.Body.AddParagraph(Util.LoremIpsumPar()).AddComplexField(tcOpts); } } } doc.Body.AddParagraph("The End.", doc.Styles[BuiltInStyleId.IntenseQuote]); // Update fields using a specific culture: doc.UpdateFields(newGrapeCity.Documents.Word.Layout.WordLayoutSettings() { FontCollection = Util.FontCollection, Culture = CultureInfo.GetCultureInfo("en-US") }); // Done:return doc; } }}