Multiple Headers
A section supports exactly three headers: default, first-page and even. Anything beyond that, such as a header per chapter, means more than one section. Sections are created on a paragraph, and a section’s properties govern that paragraph and everything before it back to the previous section break.
// Content that should carry the first header goes here.
hdr := doc.AddHeader()
hdr.AddParagraph().AddRun().AddText("My Document Title")
para := doc.AddParagraph()
section := para.Properties().AddSection(wml.ST_SectionMarkNextPage)
section.SetHeader(hdr, wml.ST_HdrFtrDefault)
// Content after this paragraph belongs to the next section.The direction catches people out. The section break paragraph closes the run of content
above it rather than opening the content below it, so the header you attach applies
backwards. Content added after that paragraph falls into the following section, and the
last stretch of the document, which has no closing break, is governed by
doc.BodySection().
Where the break falls
AddSection takes a wml.ST_SectionMark that decides how the new section starts:
| Value | Break behavior |
|---|---|
wml.ST_SectionMarkNextPage | Next section starts on a new page. |
wml.ST_SectionMarkContinuous | No page break; the next section continues on the same page. |
wml.ST_SectionMarkNextColumn | Next section starts in the next column. |
wml.ST_SectionMarkEvenPage | Next section starts on the next even-numbered page. |
wml.ST_SectionMarkOddPage | Next section starts on the next odd-numbered page. |
wml.ST_SectionMarkUnset | Section properties are set, but no break element is written. |
NextPage is the one you want for chapter headers. Continuous is for changing column
layout mid-page and will not give you a visible header change where you expect, because
both headers would then need to render on the same page.
Limitations
AddSection overwrites any section properties already on that paragraph and returns a
fresh, empty section. Page size, margins and column layout are not copied from the body
section, so a section that should match the rest of the document needs
SetPageSizeAndOrientation and SetPageMargins called on it again. That is also the
mechanism for a single landscape page in a portrait document; see
Page size and orientation.
Headers do not carry forward. A section with no header of its own gets none, not the
previous section’s, so every section that should show a header needs its own SetHeader
call. The same header value can be passed to several sections, and each reference points
back at the same header part.
Only one paragraph can hold a given section’s properties. Calling AddSection twice on
the same paragraph leaves you with only the second section.
The even/odd and first-page mechanisms still apply per section, and the
evenAndOddHeaders setting that enables the even type is document-wide rather than
per section.
Run the example
The example writes five paragraphs, closes them with a next-page section carrying “My Document Title”, writes five more, and gives the trailing body section a header reading “Different Title”.
git clone https://github.com/unidoc/unioffice-examples.git
cd unioffice-examples/document/header-footer-multiple
go run main.goIf this is your first time using UniOffice, follow the getting started guide to create an API key and set up your development environment.