w:hdr (Header)
Root element of a header part (header1.xml, etc.), containing block-level content rendered at the top of each page.
Child elements
Description
w:hdr is the root element of a header part (header{n}.xml). It is defined in ECMA-376 Part 1 §17.10.4.
The header part is a separate XML file in the OOXML package, linked from the main document part via a w:headerReference relationship inside w:sectPr. w:hdr can contain any combination of paragraphs (w:p) and tables (w:tbl), just like w:body, but must contain at least one paragraph. Header content is rendered at the top of each page according to the header’s reference type (default, first, even/odd).
Multiple header parts can exist in a document (e.g., header1.xml, header2.xml) for different sections or page types. The header part is shared within a section unless the section specifies w:evenAndOddHeaders for separate even/odd pages or w:titlePg for a different first page.
Attributes
| Attribute | Type | Possible Values | Description |
|---|---|---|---|
| (none) | — | — | Content is expressed entirely via child paragraphs and tables. |
Header Reference Types
| Type | Usage | Purpose |
|---|---|---|
default |
Standard header | Applies to all pages (or odd pages if evenAndOddHeaders is set) |
first |
First page header | Applies to first page only when w:titlePg is active |
even |
Even-page header | Applies to even pages when w:evenAndOddHeaders is active |
Examples
Simple right-aligned text header
<!-- header1.xml -->
<w:hdr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:p>
<w:pPr>
<w:jc w:val="right"/>
</w:pPr>
<w:r><w:t>My Document Header</w:t></w:r>
</w:p>
</w:hdr>
var headerPart = mainPart.AddNewPart<HeaderPart>();
string headerId = mainPart.GetIdOfPart(headerPart);
headerPart.Header = new Header(
new Paragraph(
new ParagraphProperties(new Justification { Val = JustificationValues.Right }),
new Run(new Text("My Document Header"))));
Header with page number field and border
<!-- header1.xml: page number footer -->
<w:hdr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<w:p>
<w:pPr>
<w:pBdr>
<w:bottom w:val="single" w:sz="12" w:space="1" w:color="000000"/>
</w:pBdr>
</w:pPr>
<w:r>
<w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r>
<w:instrText> PAGE </w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType="end"/>
</w:r>
</w:p>
</w:hdr>
var headerPart = mainPart.AddNewPart<HeaderPart>();
headerPart.Header = new Header(
new Paragraph(
new ParagraphProperties(
new ParagraphBorders(
new BottomBorder { Val = BorderValues.Single, Sz = 12, Space = 1, Color = "000000" })),
new Run(new FieldChar { FieldCharType = FieldCharValues.Begin }),
new Run(new FieldCode(" PAGE ")),
new Run(new FieldChar { FieldCharType = FieldCharValues.End })));
Multi-line header with company logo table
<!-- header1.xml: company branding -->
<w:hdr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<w:tbl>
<w:tblPr>
<w:tblW w:w="5000" w:type="pct"/>
<w:tblBorders>
<w:bottom w:val="single" w:sz="6" w:space="0" w:color="808080"/>
</w:tblBorders>
</w:tblPr>
<w:tr>
<w:tc>
<w:p>
<w:r><w:t>Company Name</w:t></w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcJc w:val="right"/>
</w:tcPr>
<w:p>
<w:pPr><w:jc w:val="right"/></w:pPr>
<w:r><w:t>Document Date</w:t></w:r>
</w:p>
</w:tc>
</w:tr>
</w:tbl>
</w:hdr>
var headerPart = mainPart.AddNewPart<HeaderPart>();
headerPart.Header = new Header(
new Table(
new TableProperties(
new TableWidth { Width = "5000", Type = TableWidthUnitValues.Pct },
new TableBorders(
new BottomBorder { Val = BorderValues.Single, Sz = 6, Space = 0, Color = "808080" })),
new TableRow(
new TableCell(new Paragraph(new Run(new Text("Company Name")))),
new TableCell(
new TableCellProperties(new TableCellJustification { Val = TableCellJustificationValues.Right }),
new Paragraph(
new ParagraphProperties(new Justification { Val = JustificationValues.Right }),
new Run(new Text("Document Date")))))));
Common Header Fields
| Field Code | Output | Purpose |
|---|---|---|
PAGE |
Page number | Current page number (e.g., “5”) |
NUMPAGES |
Total pages | Total page count (e.g., “10”) |
STYLEREF "Heading1" |
Chapter title | Reference to nearest Heading1 |
DATE \\@ "MM/dd/yyyy" |
Today’s date | Current date (formatted) |
FILENAME |
File name | Document file name |
Notes
- Header part file: Each header is stored in its own XML file (e.g.,
header1.xml,header2.xml) in theword/directory; linked fromdocument.xml.rels. - Minimum content: A header must contain at least one
w:pparagraph; empty headers may cause compatibility issues. - Header types: Reference type (
default,first,even) is determined byw:headerReference w:typeinw:sectPr, not by content in the header itself. - Section-specific headers: Different sections can have different headers; each
w:sectPrcontainsw:headerReferenceelements pointing to its respective header parts. - Page fields: Use
w:fldChar(begin/separate/end) withw:instrTextto embed field codes; fields update when document is opened or Print Preview is used. - Accessibility: Headers are often hidden from accessibility readers; ensure critical content is also in the main body if needed for screen readers.
- Performance: Large/complex header content (many fields, large images) can impact rendering performance and file size.