SeqFieldOpts.cs
C# VB
//// This code is part of Document Solutions for Word demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System.Drawing;using System.Globalization;using GrapeCity.Documents.Word;using GrapeCity.Documents.Word.Fields;
namespace DsWordWeb.Demos{ // This sample shows how to use the SEQ field options to enumerate arbitrary // items in a DOCX Word document: numbered chapters, paragraphs numbered // continuously across the whole document, and figures numbered independently // within each chapter. public class SeqFieldOpts { public GcWordDocument CreateDocx() { var doc = new GcWordDocument();
// Three independent SEQ counters are used together in this sample: // "Chapter" numbers the chapters themselves, "paragraphs" numbers every // paragraph across the whole document (it is never reset), and "Figure" // numbers the figures within the current chapter only (it is reset at // the start of each chapter, see below). var paragraphSeqOpts = new SeqFieldOptions(doc, "paragraphs") { NumberFormat = "'Paragraph '0':'" }; Color[] figureColors = { Color.SteelBlue, Color.IndianRed, Color.MediumSeaGreen, Color.Goldenrod }; int colorIdx = 0; var rnd = Util.NewRandom();
for (int c = 1; c <= 2; c++) { // The chapter heading carries a hidden SEQ field: figure captions // later in this chapter look this value up by identifier (see below): doc.Body.AddParagraph($"Chapter {c}", doc.Styles[BuiltInStyleId.Heading1]) .AddComplexField(new SeqFieldOptions(doc, "Chapter") { Hidden = true });
// A couple of numbered paragraphs, continuing the running count // from the previous chapter: for (int i = 0; i < rnd.Next(2, 4); i++) { doc.Body.AddParagraph(doc.Styles[BuiltInStyleId.Heading2]).AddComplexField(paragraphSeqOpts); doc.Body.AddParagraph(Util.LoremIpsumPar()); }
// A couple of figures, each a small placeholder graphic (a filled // rectangle stands in for a real picture) followed by its caption. for (int fig = 1; fig <= 2; fig++) { doc.Body.Paragraphs.Add(""); var shape = doc.Body.Runs.Last.GetRange().Shapes.Add(120, 80, GeometryType.Rectangle); shape.Fill.Type = FillType.Solid; shape.Fill.SolidFill.RGB = figureColors[colorIdx++ % figureColors.Length];
// The NumberFormat picture below uses a `numbered-item` reference // (backtick-delimited SEQ identifier, per ECMA-376 17.16.4.2): it // looks backward for the nearest SEQ field with that identifier // and inlines its number, so a single SEQ field combines the // chapter number with its own running count to produce // chapter-scoped captions: "Fig. 11", "Fig. 12" in chapter 1, // then "Fig. 21", "Fig. 22" in chapter 2 -- without needing a // separate REF field or a STYLEREF-based chapter lookup. // Note: a literal separator character placed directly between // the `identifier` reference and the following digit placeholder // (e.g. a hyphen for "Fig. 1-1") currently renders as a single // space instead of that literal character; placing the digit // placeholder immediately next to the `identifier` (with no // characters in between) avoids the issue. doc.Body.AddParagraph().AddComplexField(new SeqFieldOptions(doc, "Figure") { NumberFormat = "'Fig. '`Chapter`0" }) .GetRange().Runs.Add(": a placeholder graphic."); } // Reset the figure counter before the next chapter: doc.Body.AddParagraph().AddComplexField(new SeqFieldOptions(doc, "Figure") { Behavior = SeqFieldBehavior.ResetTo, ResetTo = 0, Hidden = true }); }
// 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; } }}