Skip to content
Document Authoring DA  API Docs v1.10.0
npmGitHub

HeadersFooters

HeadersFooters:

Collection providing access to different header or footer variants.

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.

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 number
const defaultFooter = footers.default();
if (defaultFooter) {
const para = defaultFooter.addParagraph();
para.asTextView().setText('Page ');
// Note: Page number fields would be added separately
}
// 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 exist
const count = headers.replaceText(/Draft/g, 'Final');
console.log(`Updated ${count} occurrences across all header variants`);

Check which variants exist:

const headers = section.headersAndFooters().headers();
const hasDefault = headers.default() !== null;
const hasFirst = headers.first() !== null;
const hasEven = headers.even() !== null;
console.log(`Header variants: default=${hasDefault}, first=${hasFirst}, even=${hasEven}`);

replaceText: ReplaceTextSignature

Replaces all occurrences of a pattern across all configured variants.

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.

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 }
});

default(): BlockLevelContainer | null

Gets the default header or footer.

BlockLevelContainer | null

The default HeaderFooter, or null if not configured

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.

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(): BlockLevelContainer | null

Gets the first page header or footer.

BlockLevelContainer | null

The first page HeaderFooter, or null if not configured

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.

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(): BlockLevelContainer | null

Gets the even page header or footer.

BlockLevelContainer | null

The even page HeaderFooter, or null if not configured

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.

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');
}