Header and Footer

Every document can contain headers and / or footers which can be defined on the first page, even pages and all other pages.

Following example shows how to add headers and footers to the first page, pages with even page numbers and all other pages.

Header and Footer Screenshot
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        DocumentModel document = new DocumentModel();

        Section section = new Section(document,
            new Paragraph(document,
                new Run(document, "First page"),
                new SpecialCharacter(document, SpecialCharacterType.PageBreak)),
            new Paragraph(document,
                new Run(document, "Even page"),
                new SpecialCharacter(document, SpecialCharacterType.PageBreak)),
            new Paragraph(document,
                new Run(document, "Odd page"),
                new SpecialCharacter(document, SpecialCharacterType.PageBreak)),
            new Paragraph(document, "Even page"));

        section.HeadersFooters.Add(
            new HeaderFooter(document, HeaderFooterType.HeaderFirst,
                new Paragraph(document,
                    new Run(document, "First Header"))));

        // Add page number.
        section.HeadersFooters.Add(
            new HeaderFooter(document, HeaderFooterType.FooterFirst,
                new Paragraph(document,
                    new Run(document, "First Footer")),
                new Paragraph(document,
                    new Field(document, FieldType.Page))
                {
                    ParagraphFormat = new ParagraphFormat() { Alignment = HorizontalAlignment.Right }
                }));

        section.HeadersFooters.Add(
            new HeaderFooter(document, HeaderFooterType.HeaderDefault,
                new Paragraph(document,
                    new Run(document, "Default Header"))));

        section.HeadersFooters.Add(
            new HeaderFooter(document, HeaderFooterType.FooterDefault,
                new Paragraph(document,
                    new Run(document, "Default Footer")),
                 new Paragraph(document,
                    new Field(document, FieldType.Page))
                 {
                     ParagraphFormat = new ParagraphFormat() { Alignment = HorizontalAlignment.Right }
                 }));

        section.HeadersFooters.Add(
            new HeaderFooter(document, HeaderFooterType.HeaderEven,
                new Paragraph(document,
                    new Run(document, "Even Header"))));

        section.HeadersFooters.Add(
            new HeaderFooter(document, HeaderFooterType.FooterEven,
                new Paragraph(document,
                    new Run(document, "Even Footer")),
                new Paragraph(document,
                    new Field(document, FieldType.Page))
                {
                    ParagraphFormat = new ParagraphFormat() { Alignment = HorizontalAlignment.Right }
                }));

        document.Sections.Add(section);

        document.Save("Header and Footer.%OutputFileType%");
    }
}
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As DocumentModel = New DocumentModel

        Dim section As New Section(document,
                New Paragraph(document,
                    New Run(document, "First page"),
                    New SpecialCharacter(document, SpecialCharacterType.PageBreak)),
                New Paragraph(document,
                    New Run(document, "Even page"),
                    New SpecialCharacter(document, SpecialCharacterType.PageBreak)),
                New Paragraph(document,
                    New Run(document, "Odd page"),
                    New SpecialCharacter(document, SpecialCharacterType.PageBreak)),
                New Paragraph(document, "Even page"))

        section.HeadersFooters.Add(
            New HeaderFooter(document, HeaderFooterType.HeaderFirst,
                New Paragraph(document,
                    New Run(document, "First Header"))))

        ' Add page number.
        section.HeadersFooters.Add(
            New HeaderFooter(document, HeaderFooterType.FooterFirst,
                New Paragraph(document,
                    New Run(document, "First Footer")),
                New Paragraph(document,
                    New Field(document, FieldType.Page)) With {
                .ParagraphFormat = New ParagraphFormat() With {
                    .Alignment = HorizontalAlignment.Right
         }}))

        section.HeadersFooters.Add(
            New HeaderFooter(document, HeaderFooterType.HeaderDefault,
                New Paragraph(document,
                    New Run(document, "Default Header"))))

        section.HeadersFooters.Add(
            New HeaderFooter(document, HeaderFooterType.FooterDefault,
                New Paragraph(document,
                    New Run(document, "Default Footer")),
                New Paragraph(document,
                    New Field(document, FieldType.Page)) With {
                .ParagraphFormat = New ParagraphFormat() With {
                    .Alignment = HorizontalAlignment.Right
         }}))

        section.HeadersFooters.Add(
            New HeaderFooter(document, HeaderFooterType.HeaderEven,
                New Paragraph(document,
                    New Run(document, "Even Header"))))

        section.HeadersFooters.Add(
            New HeaderFooter(document, HeaderFooterType.FooterEven,
                New Paragraph(document,
                    New Run(document, "Even Footer")),
                New Paragraph(document,
                    New Field(document, FieldType.Page)) With {
                .ParagraphFormat = New ParagraphFormat() With {
                    .Alignment = HorizontalAlignment.Right
         }}))

        document.Sections.Add(section)

        document.Save("Header and Footer.%OutputFileType%")

    End Sub

End Module

Check next example or download examples from GitHub.