Back to Devexpress

Headers and Footers in Word Documents

officefileapi-15310-word-processing-document-api-word-processing-document-headers-and-footers.md

latest8.7 KB
Original Source

Headers and Footers in Word Documents

  • Oct 04, 2024
  • 4 minutes to read

Headers and footers belong to document sections. If a section does not have header or footer, the previous section’s header and footer (if any) are used. When you create a new section, it is automatically linked to the previous section and the content from the first section header is automatically inserted in the second section header.

Note

Headers and footers are not exported to HTML format.

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:

APIDescription
Section.BeginUpdateHeader
Section.BeginUpdateFooterAccesses the document’s header or footer.
Section.HasHeader
Section.HasFooterChecks whether the given section contains a header or footer.
Section.EndUpdateHeader
Section.EndUpdateFooterFinalizes the header or footer update.

The code sample below creates an empty document header:

View Example

csharp
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
Document document = wordProcessor.Document;
    Section firstSection = document.Sections[0];

    // Check whether the document already has a header (the same header for all pages).
    if (!firstSection.HasHeader(HeaderFooterType.Primary))
    {
        //If not, create an empty header.
        SubDocument headerDocument = firstSection.BeginUpdateHeader();
        headerDocument.Paragraphs.Append();
        firstSection.EndUpdateHeader(headerDocument);
    }
}
vb
Using wordProcessor As New RichEditDocumentServer()
Dim document As Document = wordProcessor.Document
    Dim firstSection As Section = document.Sections(0)

    ' Check whether the document already has a header (the same header for all pages).
    If Not firstSection.HasHeader(HeaderFooterType.Primary) Then
        'If not, create an empty header.
        Dim headerDocument As SubDocument = firstSection.BeginUpdateHeader()
        headerDocument.Paragraphs.Append()
        firstSection.EndUpdateHeader(headerDocument)
    End If
End Using

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 (text boxes 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.

View Example

csharp
Document document = server.Document;
document.AppendSection();
Section firstSection = document.Sections[0];
// Modify the header of the HeaderFooterType.First type.
SubDocument myHeader = firstSection.BeginUpdateHeader(HeaderFooterType.First);
DocumentRange range = myHeader.InsertText(myHeader.CreatePosition(0), " PAGE NUMBER ");
Field fld = 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;
vb
Dim document As Document = server.Document
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

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

APIDescription
Section.DifferentFirstPageDefines whether to display a different header/footer (if any) for the section’s first page.
Document.DifferentOddAndEvenPagesSpecifies whether the even-numbered pages should have a different header or footer than odd-numbered pages.
SectionMargins.HeaderOffset
SectionMargins.FooterOffsetSpecifies the distance between the header or footer and the page top or bottom.
Section.LinkHeaderToNext
Section.LinkFooterToNextLinks the current section’s header or footer to the next section’s header or footer.
Section.LinkHeaderToPrevious
Section.LinkFooterToPreviousLinks the current section’s header or footer to the previous section’s header or footer.
Section.IsHeaderLinkedToNext
Section.IsFooterLinkedToNextChecks whether the current section’s header or footer is linked to the next section’s header or footer.
Section.IsHeaderLinkedToPrevious
Section.IsFooterLinkedToPreviousChecks whether the current section’s header or footer is linked to the previous section’s header or footer.
Section.UnlinkHeaderFromNext
Section.UnlinkFooterFromNextUnlinks the current section’s header or footer from the next section’s header or footer.
Section.UnlinkHeaderFromPrevious
Section.UnlinkFooterFromPreviousUnlinks the current section’s header or footer from the next section’s header or footer.