TcFieldOpts.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 create a Word document with a Table of Contents (TOC)
    // that is built with custom TOC entries created using TC (TOC Entry Item) fields.
    public class TcFieldOpts
    {
        public GcWordDocument CreateDocx()
        {
            var doc = new GcWordDocument();

            // Add a page header:
            var pageOpts = new PageFieldOptions(doc);
            pageOpts.NumberStyle = NumberStyle.Decimal;
            var sectionPagesOpts = new SectionPagesFieldOptions(doc);
            sectionPagesOpts.NumberStyle = NumberStyle.Decimal;
            var par = doc.Body.Sections.First.Headers[HeaderFooterType.Primary].Body.AddParagraph("Page ");
            par.AddComplexField(pageOpts);
            par.GetRange().Runs.Add(" of ");
            par.AddComplexField(sectionPagesOpts);

            // Add a 'TOC' field:
            var tocOpts = new TocFieldOptions(doc);
            // Tell TOC to collect TC fields (it is off by default):
            tocOpts.TcFields.Collect = true;
            // Set other options:
            tocOpts.EntryFormatting.CreateHyperlink = true;
            // Tell the TOC to include all outline levels:
            foreach (TocStyleLevel style in tocOpts.Styles)
                style.Collect = true;

            // Add TOC and a page break to the beginning of the document:
            doc.Body.Paragraphs.Add("Table of Contents", doc.Styles[BuiltInStyleId.TocHeading]).AddComplexField(tocOpts);
            doc.Body.Paragraphs.Last().GetRange().Runs.Last.AddBreak(BreakType.Page);

            // Create random content with TC fields at 3 nesting levels:
            var rnd = Util.NewRandom();
            for (int i = 0; i < rnd.Next(2, 4); i++)
            {
                var headerText = $"Top-level header {i + 1}";
                var p = doc.Body.AddParagraph(headerText, doc.Styles[BuiltInStyleId.MessageHeader]);

                var tcOpts = new TcFieldOptions(doc);
                tcOpts.Content.Text = $"TC Field for {headerText}";
                tcOpts.DisplayLevel = OutlineLevel.Level1;
                doc.Body.AddParagraph(Util.LoremIpsumPar()).AddComplexField(tcOpts);

                for (int j = 0; j < rnd.Next(2, 5); j++)
                {
                    headerText = $"Second-level header {j + 1}";
                    p = doc.Body.AddParagraph("\t" + headerText, doc.Styles[BuiltInStyleId.MessageHeader]);

                    tcOpts = new TcFieldOptions(doc);
                    tcOpts.Content.Text = $"TC Field for {headerText}";
                    tcOpts.DisplayLevel = OutlineLevel.Level2;

                    doc.Body.AddParagraph(Util.LoremIpsumPar()).AddComplexField(tcOpts);

                    for (int k = 0; k < rnd.Next(2, 6); k++)
                    {
                        headerText = $"Third-level header {k + 1}";
                        p = doc.Body.AddParagraph("\t\t" + headerText, doc.Styles[BuiltInStyleId.MessageHeader]);

                        tcOpts = new TcFieldOptions(doc);
                        tcOpts.Content.Text = $"TC Field for {headerText}";
                        tcOpts.DisplayLevel = OutlineLevel.Level3;

                        doc.Body.AddParagraph(Util.LoremIpsumPar()).AddComplexField(tcOpts);
                    }
                }
            }
            doc.Body.AddParagraph("The End.", doc.Styles[BuiltInStyleId.IntenseQuote]);

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