---
title: "Add a header and footer to Word in C#"
url: https://recursiveai.org/onedocument/cookbook/add-word-header-and-footer/
description: "Create a Word DOCX with a section’s own header and footer in C#. Reopen the file to read both parts and see how section inheritance works."
version: "1.0.0"
---

```shell
dotnet add package RecursiveAI.OneDocument.Documents
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneDocument.Documents;
using var doc = Document.Create();
doc.Body.AddParagraph("Quarterly results");
var section = doc.Sections[0];
var type = HeaderFooterType.Default;
section.AddOwn(HeaderFooterKind.Header, type)
    .Paragraphs().First().AddRun("Finance");
section.AddOwn(HeaderFooterKind.Footer, type)
    .Paragraphs().First().AddRun("Q3");
doc.SaveAs("report.docx");
```

Unzip [the example](https://recursiveai.org/onedocument/cookbook/add-word-header-and-footer/add-word-header-and-footer.zip), then `dotnet run`.

Writes `report.docx` with “Finance” above the body and “Q3” below it; it prints nothing.

## The full program

The download adds both parts to a new document, then opens the saved file and reads the section’s own header and footer with `GetOwn`.

```csharp
using RecursiveAI.OneDocument.Documents;
using static RecursiveAI.OneDocument.Documents.HeaderFooterKind;
using static RecursiveAI.OneDocument.Documents.HeaderFooterType;

using var doc = Document.Create();
doc.Body.AddParagraph("Quarterly results");
var section = doc.Sections[0];
var header = section.AddOwn(Header, Default);
var footer = section.AddOwn(Footer, Default);
header.Paragraphs().First().AddRun("Finance team");
footer.Paragraphs().First().AddRun("Quarter 3");
doc.SaveAs("report.docx");

using var saved = Document.Open("report.docx");
var savedSection = saved.Sections[0];
var savedHeader = savedSection.GetOwn(Header, Default)
    ?? throw new InvalidDataException("Missing header part.");
var savedFooter = savedSection.GetOwn(Footer, Default)
    ?? throw new InvalidDataException("Missing footer part.");
Console.WriteLine($"Header: {savedHeader.Text}");
Console.WriteLine($"Footer: {savedFooter.Text}");
```

```text
Header: Finance team
Footer: Quarter 3
```

If `GetOwn` returns `null`, the section has no readable part of that kind and type of its own; `Resolve` checks what it inherits.

## Variations

To add a separate first-page header in the full program, insert this before `SaveAs`. Since the first page also uses the first-page footer type, give it a footer explicitly if you want one there.

```csharp
section.SetTitlePage(true);
section.AddOwn(HeaderFooterKind.Header,
    HeaderFooterType.First)
    .Paragraphs().First().AddRun("Cover page");
section.AddOwn(HeaderFooterKind.Footer,
    HeaderFooterType.First)
    .Paragraphs().First().AddRun("Quarter 3");
```

The default parts are still present; the first page uses “Cover page” and “Quarter 3” instead.

## Related recipes

- [Create a Word document](https://recursiveai.org/onedocument/cookbook/create-word-document/)
- [Add a table to Word](https://recursiveai.org/onedocument/cookbook/add-table-to-word/)
- [Read a Word document](https://recursiveai.org/onedocument/cookbook/read-word-document/)

## The short answer

Use `Section.AddOwn` twice: once for the header and once for the footer. Pass `HeaderFooterType.Default` to both for ordinary pages. Each new part starts with one empty paragraph, so add a run to that paragraph and save the document as DOCX. Body text still goes in `doc.Body`.

## How do I add a header and footer to a Word document in C#?

Work on a section rather than adding paragraphs at the beginning and end of the body. A body paragraph appears once where it was inserted. A section’s header and footer are separate parts referenced by that section, so Word can display them on the pages that use that section’s settings. In a new document, `doc.Sections[0]` is the first section. `AddOwn` creates the part and its reference, so you don’t need to name the internal XML files yourself.

When working with an existing document, `AddOwn` returns an existing own part unchanged if there already is one for the requested kind and type. Adding a run to its first paragraph adds text; it doesn’t replace what’s there. Check whether the part already exists if you want to distinguish creating it from editing it.

## How do I make the header different on the first page in Word?

Set the section’s title-page option, then create its `First` header as shown in the variation. Keep `Default` for later pages, or add `Even` parts when the document has `EvenAndOddHeaders` enabled. The [page selection rules](https://recursiveai.org/onedocument/docs/docx-tables-sections/#headers-and-footers-17105) give the title page priority over even-page numbering. OneDocument does not substitute the default part when a first- or even-page part is missing. If you still want a footer on the first page, supply the matching type or check that the section inherits it.

## How do I unlink a header from the previous section in Word?

Give the later section its own header of the relevant type. Without its own reference, it uses the nearest earlier section’s header of the same type. Headers and footers are independent: adding a header does not change which footer a section uses. `GetOwn` tells you about the section itself; `Resolve` follows the inheritance chain. Use that distinction when the text you see on a page is present even though `GetOwn` finds nothing.
