Skip to content

Page Layout

Page setup in a Word document belongs to a section, not to the document. A document.Section owns the page size and orientation, the margins, the column layout, the page numbering format, and the headers and footers that appear on its pages. doc.BodySection() returns the section covering everything after the last section break, so on a document with no breaks it is the whole file. Adding a section is how you get a landscape page in the middle of a portrait document, or a different header per chapter, and AddSection returns an empty section rather than a copy of the one before it, so page size and margins have to be set again on each one.

Attaching a header takes two calls that are easy to confuse. doc.AddHeader() creates the header part and gives you something to add paragraphs and tables to, but nothing displays it. section.SetHeader(hdr, type) is what makes it appear. A header created and never set is written into the .docx and silently ignored.

The type argument is a wml.ST_HdrFtr, and each of the three values behaves differently:

TypeApplies toExtra setup
wml.ST_HdrFtrDefaultEvery page not covered by another type.None.
wml.ST_HdrFtrFirstThe first page of the section.section.SetTitlePage(true).
wml.ST_HdrFtrEvenEven-numbered pages.The evenAndOddHeaders document setting.

There is no odd type. The default header serves odd pages once an even one exists. And the even one does nothing on its own: Word ignores it unless the document also carries the evenAndOddHeaders setting, which document.Settings has no typed setter for, so you reach through the raw schema:

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

Leave that out and the even header is written to the file but never rendered, with no error to tell you.

One more thing to know before you start: SetHeader appends a reference to the section instead of replacing an existing one of the same type. Calling it twice with the same type leaves two references and no defined winner. To change a header that is already attached, find it with GetHeader first.

Six of the guides below cover headers because the examples repo has six separate header examples, from a single header on every page through to adding one to a document that already exists. Start with the plain one and move along only as far as your document needs.

Where to look

GuideCovers
Page Size and OrientationPaper size, portrait against landscape, per-section setup, and the argument order that catches people out.
Headers and FootersThe basic case: one header and one footer throughout. Start here.
Header with a TableUsing a table to position header content.
Combined Header TypesFirst-page, odd and even variants together.
Odd and Even HeadersFacing-page headers, and the setting without which they do nothing.
Multiple HeadersOne header per section.
Headers in an Existing DocumentEditing a header rather than creating one.
Page NumbersThe page number field, and why it can render blank.
Page CountWhat the page count actually reports.
Last updated on