Skip to content

Odd and Even Headers

Books and reports usually carry a different header on left- and right-hand pages: the chapter title on one side, the document title on the other. Word models that as two headers on the same section, one with type wml.ST_HdrFtrEven and one with type wml.ST_HdrFtrDefault, plus a document-wide setting that turns the pairing on.

evenHdr := doc.AddHeader()
evenHdr.AddParagraph().AddRun().AddText("Even Header")
doc.BodySection().SetHeader(evenHdr, wml.ST_HdrFtrEven)

oddHdr := doc.AddHeader()
oddHdr.AddParagraph().AddRun().AddText("Odd Header")
doc.BodySection().SetHeader(oddHdr, wml.ST_HdrFtrDefault)

boolTrue := true
doc.Settings.X().EvenAndOddHeaders = &wml.CT_OnOff{
    ValAttr: &sharedTypes.ST_OnOff{Bool: &boolTrue},
}

There is no odd header type. The default header serves the odd pages once the even one exists, which is why the second SetHeader above passes wml.ST_HdrFtrDefault.

The flag has no typed setter

document.Settings exposes exactly two methods, SetUpdateFieldsOnOpen and RemoveMailMerge. evenAndOddHeaders is not among them, so the only way to set it is through Settings.X(), which returns the raw *wml.Settings:

doc.Settings.X().EvenAndOddHeaders = &wml.CT_OnOff{}

The bare struct is enough. UniOffice reads a CT_OnOff as on whenever the element is present and its value attribute is not explicitly false, so an empty one means the same as the ValAttr: true version the example writes. The longer form needs the schema/soo/ofc/sharedTypes import; the short one does not.

This is the failure that costs people an afternoon. Set the even header, skip the flag, and the header part is written into the .docx correctly, referenced correctly from the section, and never rendered. Word applies the default header to every page and reports nothing. There is no error and no warning to trace, because as far as the file format is concerned nothing is wrong.

Limitations

The flag is document-wide, not per section. Every section in the document either alternates or does not; you cannot alternate headers in one chapter and use a single header in the next.

Which pages count as even is decided by Word’s page numbering, not by the order in which you added content. A section that starts numbering at a different value with Section.SetPageNumberStart shifts which header lands where.

Setting the even header without a default one leaves odd pages bare rather than falling back. Both types have to be set.

The first-page header is a separate mechanism with its own flag, Section.SetTitlePage(true), and it takes precedence over the odd/even pair on page one.

Run the example

The example writes a hundred paragraphs so the document runs to enough pages to see the alternation, then sets both headers and the flag.

git clone https://github.com/unidoc/unioffice-examples.git
cd unioffice-examples/document/even-odd-header
go run main.go

If this is your first time using UniOffice, follow the getting started guide to create an API key and set up your development environment.

View the full source
Last updated on