HeadersFooters
HeadersFooters:
{}Collection providing access to different header or footer variants.
Remarks
Section titled “Remarks”HeadersFooters represents a collection of header or footer variants for a section.
The same type is used for both headers (via HeadersAndFooters.headers) and
footers (via HeadersAndFooters.footers).
Documents can have up to three variants for headers/footers:
- Default: Used for most pages (typically odd pages in two-sided printing)
- First: Used for the first page only (often different for title pages)
- Even: Used for even pages in two-sided printing
Each variant can be null if not configured in the document. Check for null before
attempting to access or modify content.
Examples
Section titled “Examples”Access different header variants:
const headers = section.headersAndFooters().headers();
// Default header (most pages)const defaultHeader = headers.default();if (defaultHeader) { const para = defaultHeader.addParagraph(); para.asTextView().setText('Chapter Title');}
// First page header (title page)const firstHeader = headers.first();if (firstHeader) { const para = firstHeader.addParagraph(); para.asTextView().setText('Document Title');}
// Even page header (for two-sided printing)const evenHeader = headers.even();if (evenHeader) { const para = evenHeader.addParagraph(); para.asTextView().setText('Chapter Title - Even Page');}Set up page numbers in footer variants:
const footers = section.headersAndFooters().footers();
// Default footer with page numberconst defaultFooter = footers.default();if (defaultFooter) { const para = defaultFooter.addParagraph(); para.asTextView().setText('Page ');}
// First page footer (often empty or different)const firstFooter = footers.first();if (firstFooter) { // Leave empty or add copyright notice const para = firstFooter.addParagraph(); para.asTextView().setText('© 2024 Company Name');}Replace text across all variants:
const headers = section.headersAndFooters().headers();
// Replace across all header variants that existconst count = headers.replaceText(/Draft/g, 'Final');console.log(`Updated ${count} occurrences across all header variants`);Check which variants exist:
const headers = section.headersAndFooters().headers();
console.log(`Header variants: default=${hasDefault}, first=${hasFirst}, even=${hasEven}`);Properties
Section titled “Properties”replaceText
Section titled “replaceText”replaceText:
ReplaceTextSignature
Replaces all occurrences of a pattern across all configured variants.
Remarks
Section titled “Remarks”Convenience method that searches and replaces text across all variants (default, first, and even) that are configured. Only operates on non-null variants.
See ReplaceTextSignature for detailed parameter and usage information.
Examples
Section titled “Examples”const headers = section.headersAndFooters().headers();const count = headers.replaceText('Company', 'Corporation');console.log(`Updated ${count} occurrences in headers`);const footers = section.headersAndFooters().footers();footers.replaceText(/v\d+\.\d+/g, { text: 'v2.0', formatting: { fontSize: 9 }});Methods
Section titled “Methods”default()
Section titled “default()”default():
BlockLevelContainer|null
Gets the default header or footer.
Returns
Section titled “Returns”The default HeaderFooter, or null if not configured
Remarks
Section titled “Remarks”Returns the default (primary) header or footer used for most pages. In two-sided
printing, this is typically used for odd pages when an even page variant is also
configured. Returns null if the default variant is not configured in the document.
Example
Section titled “Example”const headers = section.headersAndFooters().headers();const defaultHeader = headers.default();
if (defaultHeader) { const para = defaultHeader.addParagraph(); para.asTextView().setText('Document Header');} else { console.log('No default header configured');}first()
Section titled “first()”first():
BlockLevelContainer|null
Gets the first page header or footer.
Returns
Section titled “Returns”The first page HeaderFooter, or null if not configured
Remarks
Section titled “Remarks”Returns the header or footer specifically for the first page of the section.
This is commonly used for title pages that need different headers/footers from
the rest of the document. Returns null if the first page variant is not
configured in the document.
Example
Section titled “Example”const headers = section.headersAndFooters().headers();const firstHeader = headers.first();
if (firstHeader) { const para = firstHeader.addParagraph(); para.asTextView().setText('Title Page Header');} else { console.log('No first page header configured');}even()
Section titled “even()”even():
BlockLevelContainer|null
Gets the even page header or footer.
Returns
Section titled “Returns”The even page HeaderFooter, or null if not configured
Remarks
Section titled “Remarks”Returns the header or footer specifically for even-numbered pages, typically
used in two-sided (duplex) printing where odd and even pages have different
headers/footers (e.g., page numbers on different sides). Returns null if the
even page variant is not configured in the document.
Example
Section titled “Example”const headers = section.headersAndFooters().headers();const evenHeader = headers.even();
if (evenHeader) { const para = evenHeader.addParagraph(); para.asTextView().setText('Even Page Header');} else { console.log('No even page header configured');}