Headers.vb
''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Word

'' Shows how to add headers and footers to a document.
Public Class Headers
    Public Function CreateDocx() As GcWordDocument
        Const NPARS = 30

        Dim doc = New GcWordDocument()

        '' Add a section and some text to it
        Dim sec1 = doc.Body.Sections.First
        Dim pars1 = sec1.GetRange().Paragraphs
        pars1.Add("Section 1").Style = doc.Styles(BuiltInStyleId.Heading2)
        For i = 0 To NPARS - 1
            pars1.Add($"Section 1, paragraph {i + 1}: " + LoremIpsumPar())
        Next

        '' Add styles for primary, first and even page headers
        Const snHdrPrimary = "Primary page header"
        Dim sHdrPrimary = doc.Styles.Add(snHdrPrimary, StyleType.Paragraph)
        sHdrPrimary.ParagraphFormat.Alignment = ParagraphAlignment.Left
        sHdrPrimary.Font.Color.RGB = Color.Blue

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

        Const snHdrEvenPages = "Even header pages"
        Dim 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
        Dim 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
        Dim sec2 = doc.Body.Paragraphs.Last.AddSectionBreak()
        Dim pars2 = sec2.GetRange().Paragraphs
        pars2.Add("Section 2").Style = doc.Styles(BuiltInStyleId.Heading2)
        For i = 0 To NPARS - 1
            pars2.Add($"Section 2, paragraph {i + 1}: " + LoremIpsumPar())
        Next

        '' Set page orientation to landscape
        sec2.PageSetup.Size.Orientation = PageOrientation.Landscape

        '' Unlink and specify own headers for section 2
        Dim hdr = sec2.Headers(HeaderFooterType.Primary)

        '' Add styles for primary and even page headers in secton 2
        Const snHdrPrimary2 = "Primary page header 2"
        Dim sHdrPrimary2 = doc.Styles.Add(snHdrPrimary2, StyleType.Paragraph)
        sHdrPrimary2.ParagraphFormat.Alignment = ParagraphAlignment.Left
        sHdrPrimary2.Font.Color.RGB = Color.Purple

        Const snHdrEvenPages2 = "Even header pages 2"
        Dim 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
    End Function
End Class