PageRefFieldOpts.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 PAGEREF field options to add
    // and customize a reference to a page in the current document
    // containing a specified bookmark.
    public class PageRefFieldOpts
    {
        public GcWordDocument CreateDocx()
        {
            var doc = new GcWordDocument();

            // The name of the target bookmark:
            const string bmkName = "myBookmark";

            // The PageRefFieldOptions class provides a convenient strong-typed access
            // to options specific to the 'PAGEREF' MS Word field:
            var pgRefOpt = new PageRefFieldOptions(doc, bmkName);
            pgRefOpt.NumberStyle = NumberStyle.UpperRoman;
            pgRefOpt.Hyperlink = true;

            // Add a paragraph with the PAGEREF to the document:
            var p = doc.Body.AddParagraph("Go to page ", doc.Styles[BuiltInStyleId.IndexHeading]).AddComplexField(pgRefOpt);
            p.GetRange().Runs.Add("...");

            // Add some pages:
            var rnd = Util.NewRandom();
            for (int i = 0; i < rnd.Next(20, 25); i++)
            {
                doc.Body.AddParagraph(Util.LoremIpsumPar());
            }
            // End with the bookmarked paragraph:
            doc.Body.AddParagraph("The end.").GetRange().Bookmarks.Add(bmkName);

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