Add Headers and Footers in Word Documents
The following example shows how you can add headers and footers to a Word document in C# and VB.NET, using the GemBox.Document library.
using GemBox.Document;
class Program
{
static void Main()
{
// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
var document = new DocumentModel();
document.DefaultCharacterFormat.Size = 48;
var section = new Section(document,
new Paragraph(document, "First page"),
new Paragraph(document, new SpecialCharacter(document, SpecialCharacterType.PageBreak)),
new Paragraph(document, "Even page"),
new Paragraph(document, new SpecialCharacter(document, SpecialCharacterType.PageBreak)),
new Paragraph(document, "Odd page"),
new Paragraph(document, new SpecialCharacter(document, SpecialCharacterType.PageBreak)),
new Paragraph(document, "Even page"));
document.Sections.Add(section);
// Add default (odd) header.
section.HeadersFooters.Add(
new HeaderFooter(document, HeaderFooterType.HeaderDefault,
new Paragraph(document, "Default Header")));
// Add default (odd) footer with page number.
section.HeadersFooters.Add(
new HeaderFooter(document, HeaderFooterType.FooterDefault,
new Paragraph(document, "Default Footer")));
// Add first header.
section.HeadersFooters.Add(
new HeaderFooter(document, HeaderFooterType.HeaderFirst,
new Paragraph(document, "First Header")));
// Add first footer.
section.HeadersFooters.Add(
new HeaderFooter(document, HeaderFooterType.FooterFirst,
new Paragraph(document, "First Footer"),
new Paragraph(document,
new Field(document, FieldType.Page),
new Run(document, " of "),
new Field(document, FieldType.NumPages))
{
ParagraphFormat = new ParagraphFormat() { Alignment = HorizontalAlignment.Right }
}));
// Add even header.
section.HeadersFooters.Add(
new HeaderFooter(document, HeaderFooterType.HeaderEven,
new Paragraph(document, "Even Header")));
// Add even footer with page number.
section.HeadersFooters.Add(
new HeaderFooter(document, HeaderFooterType.FooterEven,
new Paragraph(document, "Even Footer"),
new Paragraph(document,
new Field(document, FieldType.Page),
new Run(document, " of "),
new Field(document, FieldType.NumPages))
{
ParagraphFormat = new ParagraphFormat() { Alignment = HorizontalAlignment.Right }
}));
document.Save("Header and Footer.%OutputFileType%");
}
}
Imports GemBox.Document
Module Program
Sub Main()
' If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Dim document As New DocumentModel()
document.DefaultCharacterFormat.Size = 48
Dim section As New Section(document,
New Paragraph(document, "First page"),
New Paragraph(document, New SpecialCharacter(document, SpecialCharacterType.PageBreak)),
New Paragraph(document, "Even page"),
New Paragraph(document, New SpecialCharacter(document, SpecialCharacterType.PageBreak)),
New Paragraph(document, "Odd page"),
New Paragraph(document, New SpecialCharacter(document, SpecialCharacterType.PageBreak)),
New Paragraph(document, "Even page"))
document.Sections.Add(section)
' Add default (odd) header.
section.HeadersFooters.Add(
New HeaderFooter(document, HeaderFooterType.HeaderDefault,
New Paragraph(document, "Default Header")))
' Add default (odd) footer with page number.
section.HeadersFooters.Add(
New HeaderFooter(document, HeaderFooterType.FooterDefault,
New Paragraph(document, "Default Footer")))
' Add first header.
section.HeadersFooters.Add(
New HeaderFooter(document, HeaderFooterType.HeaderFirst,
New Paragraph(document, "First Header")))
' Add first footer.
section.HeadersFooters.Add(
New HeaderFooter(document, HeaderFooterType.FooterFirst,
New Paragraph(document, "First Footer"),
New Paragraph(document,
New Field(document, FieldType.Page),
New Run(document, " of "),
New Field(document, FieldType.NumPages)) With
{
.ParagraphFormat = New ParagraphFormat() With {.Alignment = HorizontalAlignment.Right}
}))
' Add even header.
section.HeadersFooters.Add(
New HeaderFooter(document, HeaderFooterType.HeaderEven,
New Paragraph(document, "Even Header")))
' Add even footer with page number.
section.HeadersFooters.Add(
New HeaderFooter(document, HeaderFooterType.FooterEven,
New Paragraph(document, "Even Footer"),
New Paragraph(document,
New Field(document, FieldType.Page),
New Run(document, " of "),
New Field(document, FieldType.NumPages)) With
{
.ParagraphFormat = New ParagraphFormat() With {.Alignment = HorizontalAlignment.Right}
}))
document.Save("Header and Footer.%OutputFileType%")
End Sub
End Module

In the GemBox.Document component, headers and footers are represented with HeaderFooter
elements, repeated on each page. They are defined for each Section
element, whose content can occupy one or more pages. Each Section.HeadersFooters
collection can contain only one instance of each HeaderFooterType
type.
In other words, you can define different headers and footers for the first page, even pages, and odd pages. If you need different headers and footers on various pages, you can set up multiple sections and create the desired headers and footers for each one of them. The following example shows how to link headers and footers to the previous section using the GemBox.Document library. In GemBox.Document, you can control whether headers and footers are linked between sections by using the Linked to previous headers and footers
using GemBox.Document;
class Program
{
static void Main()
{
// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
var document = new DocumentModel();
document.DefaultCharacterFormat.Size = 28;
// Create sections.
var section1 = new Section(document, new Paragraph(document, "First section content")
{ ParagraphFormat = { SpaceBefore = 40, SpaceAfter = 0 } });
var section2 = new Section(document, new Paragraph(document, "Second section content")
{ ParagraphFormat = { SpaceBefore = 40, SpaceAfter = 0 } });
var section3 = new Section(document, new Paragraph(document, "Third section content")
{ ParagraphFormat = { SpaceBefore = 40, SpaceAfter = 0 } });
// Add sections to the document.
document.Sections.Add(section1);
document.Sections.Add(section2);
document.Sections.Add(section3);
// Create a header in the first section.
section1.HeadersFooters.Add(
new HeaderFooter(document, HeaderFooterType.HeaderDefault,
new Paragraph(document, "Shared Header (linked across sections)")));
// Link headers in the second and third sections to the first section header.
section2.HeadersFooters.SetLinkedToPrevious(HeaderFooterType.HeaderDefault, true);
section3.HeadersFooters.SetLinkedToPrevious(HeaderFooterType.HeaderDefault, true);
document.Save("Linked To Previous.%OutputFileType%");
}
}
Imports GemBox.Document
Module Program
Sub Main()
' If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Dim document As New DocumentModel()
document.DefaultCharacterFormat.Size = 28
' Create sections.
Dim section1 As New Section(document, New Paragraph(document, "First section content") With
{.ParagraphFormat = New ParagraphFormat() With {.SpaceBefore = 40, .SpaceAfter = 0}})
Dim section2 As New Section(document, New Paragraph(document, "Second section content") With
{.ParagraphFormat = New ParagraphFormat() With {.SpaceBefore = 40, .SpaceAfter = 0}})
Dim section3 As New Section(document, New Paragraph(document, "Third section content") With
{.ParagraphFormat = New ParagraphFormat() With {.SpaceBefore = 40, .SpaceAfter = 0}})
' Add sections to the document.
document.Sections.Add(section1)
document.Sections.Add(section2)
document.Sections.Add(section3)
' Create a header in the first section.
section1.HeadersFooters.Add(
New HeaderFooter(document, HeaderFooterType.HeaderDefault,
New Paragraph(document, "Shared Header (linked across sections)")))
' Link headers in the second and third sections to the first section header.
section2.HeadersFooters.SetLinkedToPrevious(HeaderFooterType.HeaderDefault, True)
section3.HeadersFooters.SetLinkedToPrevious(HeaderFooterType.HeaderDefault, True)
document.Save("Linked To Previous.%OutputFileType%")
End Sub
End Module
HeaderFooterCollection.SetLinkedToPrevious
method. When a particular HeaderFooterType
is linked, the section will reuse the header or footer from the previous section. In that case, accessing the collection's indexer (HeaderFooterCollection[HeaderFooterType]
) will return the header or footer from the earlier section. If there is no previous section, or if none of the previous sections define that type, the indexer will return null
.