SectionFieldOpts.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using GrapeCity.Documents.Word;
using GrapeCity.Documents.Word.Fields;

namespace DsWordWeb.Demos
{
    // This sample shows how to use the SECTION and SECTIONPAGES field options
    // to insert the number of the current section, and the number of pages in
    // the current section, into the document.
    public class SectionFieldOpts
    {
        public GcWordDocument CreateDocx()
        {
            var doc = new GcWordDocument();

            // The PageFieldOptions class provides a convenient strong-typed access
            // to options specific to the 'PAGE' MS Word field.

            // Add a simple page header 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" });

            // SectionFieldOptions provides access to properties specific to the SECTION field:
            var sectionFldOpts = new SectionFieldOptions(doc);
            sectionFldOpts.NumberStyle = NumberStyle.UpperRoman;

            // SectionPagesFieldOptions provides access to properties specific to the SECTIONPAGES field:
            var sectionPagesFldOpts = new SectionPagesFieldOptions(doc);
            sectionPagesFldOpts.NumberFormat = "' ('0' page(s))'";

            // Add a few sections with random content:
            var rnd = Util.NewRandom();
            var numSections = rnd.Next(3, 6);
            for (int i = 0; i < numSections; i++)
            {
                var p = doc.Body.AddParagraph("Section ", doc.Styles[BuiltInStyleId.Heading1]);
                p.AddComplexField(sectionFldOpts);
                p.AddComplexField(sectionPagesFldOpts);
                for (int j = 0; j < rnd.Next(5, 10); j++)
                {
                    var par = Util.LoremIpsumPar();
                    doc.Body.AddParagraph(par);
                }
                if (i < numSections - 1)
                    doc.Body.Paragraphs.Last.AddSectionBreak(SectionStart.Continuous);
            }

            // 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;
        }
    }
}