PageFieldOpts.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
{
    // TBD:
    public class PageFieldOpts
    {
        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.

            // PAGE field pgOptBody for the page header:
            var pgOptHeader = new PageFieldOptions(doc);
            pgOptHeader.NumberStyle = NumberStyle.Decimal;

            // PAGE field pgOptBody for page numbers in the body:
            var pgOptBody = new PageFieldOptions(doc);
            pgOptBody.NumberStyle = NumberStyle.UpperRoman;

            // Add a page header that includes the page number:
            doc.Body.Sections.First.Headers[HeaderFooterType.Primary].Body.AddParagraph("Page header - page ").AddComplexField(pgOptHeader);

            // Pick a style that will be used for page numbers in the document body:
            var pageStyle = doc.Styles[BuiltInStyleId.IndexHeading];

            // Add some pages, with periodic inclusions of the PAGE field:
            var rnd = Util.NewRandom();
            for (int i = 0; i < rnd.Next(6, 12); i++)
            {
                var p = doc.Body.AddParagraph("This text is on page ", pageStyle).AddComplexField(pgOptBody);
                p.GetRange().Runs.Add(".");
                var par = Util.LoremIpsumPar();
                doc.Body.AddParagraph(par);
            }

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