Headers.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.Linq;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // Shows how to add headers and footers to a document.
    public class Headers
    {
        public GcWordDocument CreateDocx()
        {
            const int NPARS = 30;
            var doc = new GcWordDocument();

            // Add a section and some text to it:
            var sec1 = doc.Body.Sections.First;
            var pars1 = sec1.GetRange().Paragraphs;
            pars1.Add("Section 1").Style = doc.Styles[BuiltInStyleId.Heading2];
            for (int i = 0; i < NPARS; ++i)
                pars1.Add($"Section 1, paragraph {i + 1}: " + Util.LoremIpsumPar());

            // Add styles for primary, first and even page headers:
            const string snHdrPrimary = "Primary page header";
            var sHdrPrimary = doc.Styles.Add(snHdrPrimary, StyleType.Paragraph);
            sHdrPrimary.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            sHdrPrimary.Font.Color.RGB = Color.Blue;

            const string snHdrFirstPage = "First page header";
            var sHdrFirstPage = doc.Styles.Add(snHdrFirstPage, StyleType.Paragraph);
            sHdrFirstPage.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            sHdrFirstPage.Font.Color.RGB = Color.Gray;

            const string snHdrEvenPages = "Even header pages";
            var sHdrEvenPages = doc.Styles.Add(snHdrEvenPages, StyleType.Paragraph);
            sHdrEvenPages.ParagraphFormat.Alignment = ParagraphAlignment.Right;
            sHdrEvenPages.Font.Color.RGB = Color.Blue;

            // Use a different header on the first, even and odd pages:
            var p = sec1.Headers[HeaderFooterType.Primary].Body.Paragraphs.Add("DsWord Headers Sample - Section 1 - Primary Header - Page ");
            p.GetRange().SimpleFields.Add("PAGE");
            p.Style = sHdrPrimary;

            sec1.PageSetup.DifferentFirstPageHeaderFooter = true;
            p = sec1.Headers[HeaderFooterType.FirstPage].Body.Paragraphs.Add("DsWord Headers Sample - Section 1 - First Page");
            p.Style = sHdrFirstPage;

            sec1.PageSetup.OddAndEvenPagesHeaderFooter = true;
            p = sec1.Headers[HeaderFooterType.EvenPages].Body.Paragraphs.Add("DsWord Headers Sample - Section 1 - Even Pages - Page ");
            p.GetRange().SimpleFields.Add("PAGE");
            p.Style = sHdrEvenPages;

            // Add another section with different page orientation and different headers:
            var sec2 = doc.Body.Paragraphs.Last.AddSectionBreak();
            var pars2 = sec2.GetRange().Paragraphs;
            pars2.Add("Section 2").Style = doc.Styles[BuiltInStyleId.Heading2];
            for (int i = 0; i < NPARS; ++i)
                pars2.Add($"Section 2, paragraph {i + 1}: " + Util.LoremIpsumPar());

            // Set page orientation to landscape:
            sec2.PageSetup.Size.Orientation = PageOrientation.Landscape;

            // Unlink and specify own headers for section 2:
            var hdr = sec2.Headers[HeaderFooterType.Primary];

            // Add styles for primary and even page headers in secton 2:
            const string snHdrPrimary2 = "Primary page header 2";
            var sHdrPrimary2 = doc.Styles.Add(snHdrPrimary2, StyleType.Paragraph);
            sHdrPrimary2.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            sHdrPrimary2.Font.Color.RGB = Color.Purple;

            const string snHdrEvenPages2 = "Even header pages 2";
            var sHdrEvenPages2 = doc.Styles.Add(snHdrEvenPages2, StyleType.Paragraph);
            sHdrEvenPages2.ParagraphFormat.Alignment = ParagraphAlignment.Right;
            sHdrEvenPages2.Font.Color.RGB = Color.Purple;

            // NOTE: This property must be set BEFORE changing the header,
            // otherwise it will affect headers in previous sections too:
            hdr.LinkToPrevious = false;
            // NOTE: OddAndEvenPagesHeaderFooter applies to the WHOLE document,
            // not just to the current section:
            // sec2.PageSetup.OddAndEvenPagesHeaderFooter = false;
            sec2.PageSetup.DifferentFirstPageHeaderFooter = false;
            p = hdr.Body.Paragraphs.Add("DsWord Headers Sample - Section 2 - Primary Header - Page ");
            p.GetRange().SimpleFields.Add("PAGE");
            p.Style = sHdrPrimary2;

            hdr = sec2.Headers[HeaderFooterType.EvenPages];
            hdr.LinkToPrevious = false;
            p = hdr.Body.Paragraphs.Add("DsWord Headers Sample - Section 2 - Even Pages - Page ");
            p.GetRange().SimpleFields.Add("PAGE");
            p.Style = sHdrEvenPages2;

            // Done:
            return doc;
        }
    }
}