A newer version of this page is available. Switch to the current version.

Headers and Footers

  • 4 minutes to read

Each section has a header and footer. If a section does not have headers or footers, the previous sections’ headers and footers (if any) are used.

Create Headers and Footers

You can use the Section.BeginUpdateHeader and Section.BeginUpdateFooter methods to access the section’s header or footer. Pass one of the HeaderFooterType enumeration values as a type parameter to define the header or footer’s type. Use the following API to access headers and footers in code:

API

Description

Section.BeginUpdateHeader

Section.BeginUpdateFooter

Accesses the document’s header or footer.

Section.HasHeader

Section.HasFooter

Checks whether the given section contains a header or footer.

Section.EndUpdateHeader

Section.EndUpdateFooter

Finalizes the header or footer update.

The code sample below creates an empty document header:

Dim firstSection As Section = document.Sections(0)
' Create an empty header.
Dim newHeader As SubDocument = firstSection.BeginUpdateHeader()
firstSection.EndUpdateHeader(newHeader)
' Check whether the document already has a header (the same header for all pages).
If firstSection.HasHeader(HeaderFooterType.Primary) Then
    Dim headerDocument As SubDocument = firstSection.BeginUpdateHeader()
    document.ChangeActiveDocument(headerDocument)
    document.CaretPosition = headerDocument.CreatePosition(0)
    firstSection.EndUpdateHeader(headerDocument)
End If

Modify Headers and Footers

Headers’ and footers’ content are in a separate SubDocument. Call the Section.BeginUpdateHeader or Section.BeginUpdateFooter method to access the header’s or footer’s content. Headers and footers can contain inline images, floating objects (textboxes or images) and tables. You cannot add comments to headers’ or footers’ text.

NOTE

The header or footer fields belong to a separate FieldCollection. Retrieve the header’s or footer’s SubDocument and call the FieldCollection.Update method to update these fields.

The following code snippet opens the first section’s header for editing. The header type is HeaderFooterType.First, so it is displayed for the first page only if the corresponding mode is enabled (use the Section.DifferentFirstPage property). Then, the text string and a PAGE field are inserted and the Section.EndUpdateHeader method is called to finalize the modification.

document.AppendSection()
Dim firstSection As Section = document.Sections(0)
' Modify the header of the HeaderFooterType.First type.
Dim myHeader As SubDocument = firstSection.BeginUpdateHeader(HeaderFooterType.First)
Dim range As DocumentRange = myHeader.InsertText(myHeader.CreatePosition(0), " PAGE NUMBER ")
Dim fld As Field = myHeader.Fields.Create(range.End, "PAGE \* ARABICDASH")
myHeader.Fields.Update()
firstSection.EndUpdateHeader(myHeader)
' Display the header of the HeaderFooterType.First type on the first page.
firstSection.DifferentFirstPage = True
TIP

Call the Document.ChangeActiveDocument method to switch to the header’s or footer’s SubDocument. This step allows you to select text ranges or place the caret in the header or footer. Call the Document.ChangeActiveDocument again to switch to the main document body.

Use the following API to change header/footer options in code:

API

Description

Section.DifferentFirstPage

Defines whether to display a different header/footer (if any) for the section’s first page.

Document.DifferentOddAndEvenPages

Specifies whether the even-numbered pages should have a different header or footer than odd-numbered pages.

Section.LinkHeaderToNext

Section.LinkFooterToNext

Links the current section’s header or footer to the next section’s header or footer.

Section.LinkHeaderToPrevious

Section.LinkFooterToPrevious

Links the current section’s header or footer to the previous section’s header or footer.

Section.IsHeaderLinkedToNext

Section.IsFooterLinkedToNext

Checks whether the current section’s header or footer is linked to the next section’s header or footer.

Section.IsHeaderLinkedToPrevious

Section.IsFooterLinkedToPrevious

Checks whether the current section’s header or footer is linked to the previous section’s header or footer.

Section.UnlinkHeaderFromNext

Section.UnlinkFooterFromNext

Unlinks the current section’s header or footer from the next section’s header or footer.

Section.UnlinkHeaderFromPrevious

Section.UnlinkFooterFromPrevious

Unlinks the current section’s header or footer from the next section’s header or footer.

See Also