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

namespace DsWordWeb.Demos
{
    // This sample shows how to use the NOTEREF field options to add
    // and customize a reference to a footnote or endnote marked by a
    // bookmark elsewhere in the document. A companion PAGEREF field
    // targeting the same bookmark shows that WordLayout can resolve a
    // page number for a bookmark anchored to a footnote/endnote mark.
    public class NoteRefFieldOpts
    {
        public GcWordDocument CreateDocx()
        {
            var doc = new GcWordDocument();

            // The names of the target bookmarks:
            const string fnBmk = "fnBookmark";
            const string enBmk = "enBookmark";

            var p1 = doc.Body.AddParagraph("See the footnote");
            p1.GetRange().Footnotes.Add("This is the footnote text.");
            // The bookmark must be placed on the paragraph that contains the
            // footnote's reference mark -- not on content inside the footnote's
            // own body -- for NOTEREF/PAGEREF to be able to resolve it:
            p1.GetRange().Bookmarks.Add(fnBmk);

            p1.GetRange().Runs.Add(" and the endnote");
            p1.GetRange().Endnotes.Add("This is the endnote text.");
            p1.GetRange().Bookmarks.Add(enBmk);
            p1.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());
            }

            // NOTEREF to the footnote, formatted like its own reference mark:
            var noteRefFn = new NoteRefFieldOptions(doc, fnBmk) { Hyperlink = true, FormatAsReferenceMark = true };
            doc.Body.AddParagraph("Footnote reference mark: ").AddComplexField(noteRefFn);

            // NOTEREF to the endnote, as a plain number instead of its (roman numeral) mark:
            var noteRefEn = new NoteRefFieldOptions(doc, enBmk) { Hyperlink = true, FormatAsReferenceMark = false };
            doc.Body.AddParagraph("Endnote number: ").AddComplexField(noteRefEn);

            // A PAGEREF to the same footnote bookmark shows that WordLayout can
            // now resolve a page number for a bookmark anchored to a footnote mark:
            var pageRefFn = new PageRefFieldOptions(doc, fnBmk) { Hyperlink = true };
            doc.Body.AddParagraph("The footnote is on page: ").AddComplexField(pageRefFn);

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