# Headers and Footers

> Set header activation and frame style in XAML, add footers with page numbers and open headers for editing from code in a WPF TextControl with HeaderFooter.

- **Product:** TX Text Control .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for WPF > Document Editor > Headers and Footers
- **3343 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/headers-footers/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/headers-footers/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/headers-footers/llms-full.txt

---

Developers building a WPF editor for letters, reports or contracts need headers and footers: the letterhead on the first page, a title on every page, page numbers at the bottom. The WPF `TextControl` lets users add and edit them from the ribbon and the **Page Setup** dialog box; in XAML you set how they're activated, and in code you add them with page numbers and follow the user into them.

The code belongs in *MainWindow.xaml.cs* of a window with a `TextControl` named `textControl1`, as in [Create Your First WPF Application](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-editor/first-application/llms-full.txt); replace `SampleApp` with your namespace. Sections, parts of a document with page settings of their own, are explained in [Sections and Page Layout](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/sections/llms-full.txt). [Sections, Pages, Headers, and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/sections-headers/llms-full.txt) covers the same objects with `ServerTextControl`.

### How Headers and Footers Work

Headers and footers are [HeaderFooter](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfooter/llms-full.txt) objects of the `TXTextControl` namespace. Each is a text part with a [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfooter/selection-property/llms-full.txt), paragraphs, tables and images of its own.

![Class diagram of the header and footer classes: TextControl, WPF.TextControl and ServerTextControl with HeadersAndFooters, the activation properties and events, HeaderFooterCollection, HeaderFooter with its collections and PageNumberField.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-wpf/headers-footers-classes.58889338.webp)

- **Per section.** [Section.HeadersAndFooters](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/section/headersandfooters-property/llms-full.txt) is the [HeaderFooterCollection](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfootercollection/llms-full.txt) of one section. [TextControl.HeadersAndFooters](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/headersandfooters-property/llms-full.txt) belongs to the whole document and is meant for documents with a single section; with more sections, most collections of its headers are `null`.
- **By type.** [HeaderFooterType](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfootertype/enumeration/llms-full.txt) distinguishes `Header` and `Footer` for every page, `FirstPageHeader` and `FirstPageFooter` for the first page of a section, and `EvenHeader` and `EvenFooter` for even pages; with an even header, `Header` is the header of the odd pages.
- **Connected to the previous section.** A new section inherits the headers and footers of the one before it, with the same content. [HeaderFooter.ConnectedToPrevious](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfooter/connectedtoprevious-property/llms-full.txt) set to `false` separates them.

### Add Headers and Footers in the Ribbon

The **Headers & Footers** group of the **Insert** tab, a [RibbonInsertTab](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/ribboninserttab/llms-full.txt), has **Header** with **Edit Header** and **Remove Header**, **Footer** with **Edit Footer** and **Remove Footer**, and, while a header or footer is being edited, **Page Number** with **Insert Page Number**, **Format Page Number...** and **Remove Page Number**.

For all headers and footers at once, the **Page Setup** dialog box has a **Headers and Footers** tab:

![The Page Setup dialog box on its Headers and Footers tab, with the Header list open showing None, All sections equal and All sections different, and the distance fields for header, footer and first page.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-wpf/page-setup-headers-footers.7b859779.webp)

- **Header**, **Header on first**, **Footer** and **Footer on first** choose which headers and footers exist. For the **Whole document**, the **Header** and **Footer** lists offer **None**, **All sections equal** and **All sections different**; for a **Section**, **None**, **Same as previous section** and **This section different**.
- **Different header odd and even** and **Different footer odd and even** add the even variants.
- **Distance from edge** positions headers and footers, with separate values for the first page.

[SectionFormatDialog](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/sectionformatdialog-method/llms-full.txt) opens the dialog box from code; `SectionFormatDialog(1)` shows the **Headers and Footers** tab.

### Set Up the Editing Behavior in XAML

How users enter a header or footer and how an active one is marked are properties of the `TextControl`, which you set as attributes in *MainWindow.xaml* together with the event handlers. `WPF` is the prefix of the namespace `TXTextControl.WPF`:

```xml
<WPF:TextControl x:Name="textControl1"
                 HeaderFooterActivationStyle="ActivateDoubleClick"
                 HeaderFooterFrameStyle="DividingLine"
                 HeaderFooterActivated="TextControl1_HeaderFooterActivated"
                 HeaderFooterDeactivated="TextControl1_HeaderFooterDeactivated" />
```

- [HeaderFooterActivationStyle](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/headerfooteractivationstyle-property/llms-full.txt) is `ActivateClick` for a single click or `ActivateDoubleClick` for a double-click into a header or footer.
- [HeaderFooterFrameStyle](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/headerfooterframestyle-property/llms-full.txt) marks the active header or footer with a `DividingLine`, a `DottedFrame`, a `SingleFrame` or `NoFrame`.
- [HeaderFooterActivated](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/headerfooteractivated-event/llms-full.txt) occurs when a header or footer receives the input position, and [HeaderFooterDeactivated](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/headerfooterdeactivated-event/llms-full.txt) when it loses it.

### Add Page Numbers and Edit a Header From Code

The following handlers add a footer with "Page x of y" to the current section, open its header for editing like **Edit Header**, and show in the title bar which part the user is editing:

```csharp
using System.Windows;
using TXTextControl;

namespace SampleApp;

public partial class MainWindow : Window
{
    private void AddFooter_Click(object sender, RoutedEventArgs e)
    {
        // Add a footer to the section with the input position
        Section section = textControl1.Sections.GetItem();
        section.HeadersAndFooters.Add(HeaderFooterType.Footer);
        HeaderFooter footer = section.HeadersAndFooters.GetItem(HeaderFooterType.Footer);

        // "Page x of y"
        footer.Selection.Text = "Page ";
        footer.PageNumberFields.Add(new PageNumberField(1, NumberFormat.ArabicNumbers));
        footer.Selection.Text = " of ";

        PageNumberField numberOfPages = new PageNumberField();
        numberOfPages.ShowNumberOfPages = true;
        footer.PageNumberFields.Add(numberOfPages);

        footer.Selection.ParagraphFormat.Alignment = TXTextControl.HorizontalAlignment.Center;

        // 0.5 inch from the bottom of the page, in twips
        footer.Distance = 720;
    }

    private void EditHeader_Click(object sender, RoutedEventArgs e)
    {
        Section section = textControl1.Sections.GetItem();

        // GetItem throws an ArgumentException for a header that doesn't exist
        bool hasHeader = false;

        foreach (HeaderFooter headerFooter in section.HeadersAndFooters)
        {
            if (headerFooter.Type == HeaderFooterType.Header)
            {
                hasHeader = true;
            }
        }

        if (!hasHeader)
        {
            section.HeadersAndFooters.Add(HeaderFooterType.Header);
        }

        // Move the input position into the header
        section.HeadersAndFooters.GetItem(HeaderFooterType.Header).Activate();
        textControl1.Focus();
    }

    private void TextControl1_HeaderFooterActivated(object sender, HeaderFooterEventArgs e)
    {
        Title = "Editing: " + e.HeaderFooter.Type;
    }

    private void TextControl1_HeaderFooterDeactivated(object sender, HeaderFooterEventArgs e)
    {
        Title = "Editing: main text";
    }
}
```

The code works as follows:

- [SectionCollection.GetItem](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectioncollection/getitem-method/llms-full.txt) without a parameter returns the section with the input position.
- [HeaderFooterCollection.Add](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfootercollection/add-method/llms-full.txt) adds a header or footer of a type. [GetItem](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfootercollection/getitem-method/llms-full.txt) returns it, and throws an `ArgumentException` for a type that doesn't exist, so `EditHeader_Click` looks for an existing header first.
- A [PageNumberField](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/pagenumberfield/llms-full.txt) can only be used in a header or footer. [HeaderFooter.PageNumberFields](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfooter/pagenumberfields-property/llms-full.txt) adds it at the input position; its [constructor](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/pagenumberfield/constructor/llms-full.txt) takes the start number and a [NumberFormat](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/numberformat/enumeration/llms-full.txt) such as `ArabicNumbers` or `RomanNumbers`, and [ShowNumberOfPages](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/pagenumberfield/shownumberofpages-property/llms-full.txt) shows the number of pages instead.
- [HeaderFooter.Distance](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfooter/distance-property/llms-full.txt) is measured in twips from the page edge; the default is 567 twips, 1 cm.
- [HeaderFooter.Activate](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfooter/activate-method/llms-full.txt) moves the input position into the header, and `textControl1.Focus()` gives the keyboard focus from the button back to the editor. [Deactivate](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfooter/deactivate-method/llms-full.txt) returns to the main text.
- [HeaderFooterEventArgs.HeaderFooter](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfootereventargs/headerfooter-property/llms-full.txt) is the header or footer of the event, and its [Type](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfooter/type-property/llms-full.txt) says which one.

Page number fields update when the number of pages changes. [HeaderFooterCollection.Remove](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfootercollection/remove-method/llms-full.txt) removes headers and footers by type.

### Put Images and Other Content Into a Header

[HeaderFooter.Images](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfooter/images-property/llms-full.txt), [HeaderFooter.Tables](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfooter/tables-property/llms-full.txt) and [HeaderFooter.Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfooter/barcodes-property/llms-full.txt) take a logo, a table or a barcode with the `Add` methods described in [Images](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/images/llms-full.txt) and [Charts and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/charts-barcodes/llms-full.txt). An image placed behind the text of a header is repeated on every page of its section. The blog article [Merging Multiple Records with First-Page Headers and Footers](https://www.textcontrol.com/blog/2025/04/14/merging-multiple-records-with-first-page-headers-and-footers/) uses first page headers in a mail merge.

### Next Steps

- [Sections and Page Layout](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/sections/llms-full.txt): page settings and headers per section.
- [Fields, Hyperlinks, and Bookmarks](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/fields/llms-full.txt): fields in the text and in headers.
- [Footnotes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/footnotes/llms-full.txt): notes at the bottom of a page.
- [Sections, Pages, Headers, and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/sections-headers/llms-full.txt): headers and footers with `ServerTextControl`.

---

## Nearby

- **Previous:** [Charts and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/charts-barcodes/llms-full.txt)
- **Next:** [Sections and Page Layout](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/sections/llms-full.txt)
