Sections, headers, and footers
A Section is a group of consecutive pages that share a page size, margins, and headers and footers. Every FlowDocument starts with one section, and content added directly to the document goes into the current (last) section.
Headers and footers
Headers and footers are content containers owned by a section. GetOrCreateHeader and GetOrCreateFooter return the existing one or create an empty one; the content of an existing header is preserved.
Section section = doc.GetCurrentSection();
ContentContainer header = section.GetOrCreateHeader(Section.PageGroup.e_odd_pages);
Paragraph headerPara = header.AddParagraph("SYH Supply Co.");
headerPara.SetJustificationMode(Paragraph.TextJustification.e_right);
headerPara.GetTextStyledElement().SetTextColor(120, 120, 120);
ContentContainer footer = section.GetOrCreateFooter(Section.PageGroup.e_odd_pages);
Paragraph footerPara = footer.AddParagraph();
footerPara.SetJustificationMode(Paragraph.TextJustification.e_center);
footerPara.AddText("Page ");
footerPara.AddPageNumber(PageNumber.NumberType.e_current_page);
footerPara.AddText(" of ");
footerPara.AddPageNumber(PageNumber.NumberType.e_total_pages);Section section = doc.GetCurrentSection();
ContentContainer header = section.GetOrCreateHeader(Section::e_odd_pages);
Paragraph header_para = header.AddParagraph("SYH Supply Co.");
header_para.SetJustificationMode(Paragraph::e_text_justify_right);
header_para.GetTextStyledElement().SetTextColor(120, 120, 120);
ContentContainer footer = section.GetOrCreateFooter(Section::e_odd_pages);
Paragraph footer_para = footer.AddParagraph();
footer_para.SetJustificationMode(Paragraph::e_text_justify_center);
footer_para.AddText("Page ");
footer_para.AddPageNumber(PageNumber::e_current_page);
footer_para.AddText(" of ");
footer_para.AddPageNumber(PageNumber::e_total_pages);
Page groups
Each header and footer applies to one group of pages, so a section can have up to three of each.
Page group values include:
e_first_page
The first page of the section.
e_even_pages
Even numbered pages.
e_odd_pages
Odd numbered pages.
To use the same footer everywhere, create one for each group.
The first-page group is how you suppress a header on a title page: create an empty header for it and a populated one for the even and odd groups. Use RemoveHeader or RemoveFooter to drop one entirely.
Page numbers
AddPageNumber inserts a dynamic field that is resolved during pagination, so counts stay correct when content shifts. Use the current-page type for the page itself and the total-pages type for the document total.
SetNumberFormat changes the numbering system - lower roman for front matter, for example. A large set of formats is supported, including:
e_upper_romane_lower_lettere_upper_lettere_ordinale_cardinal_texte_decimal_zeroe_arabic_alphae_japanese_countinge_korean_countinge_hebrew_1e_hindi_numberse_russian_lower
Page numbers are not restricted to footers. They work in any paragraph, including cross-references in body text.
Multiple sections
AddSection appends a section and makes it current, so subsequent content goes into it. A section always starts on a new page, and can override the document defaults for page size and margins - which is how a landscape page is mixed into a portrait document.
Section settings cascade. A section only overrides the document defaults where you explicitly set a page size or margins. Headers and footers are not inherited; each section defines its own.
You can also add content to a section directly rather than through the document, which is clearer when maintaining several sections.
Further Reading
Paragraphs and text for styling header and footer content.
Floats for page-anchored content such as watermarks.
Last updated
Was this helpful?