GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API.

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.

Screenshot
Header and Footer Screenshot

See the full code below, use Run Example to execute.

using System;
using System.IO;
using System.Linq;
using GemBox.Document;
using GemBox.Document.Drawing;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // 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.docx");
    }
}
Imports System
Imports System.IO
Imports System.Linq
Imports GemBox.Document
Imports GemBox.Document.Drawing

Module Samples

    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.docx")

    End Sub

End Module

Check next sample.