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

Body

Body:

Represents the document body — its block-level content and the section-property handles that apply to ranges within it.

Block-level content (paragraphs and tables) is exposed through content, which returns a BlockLevelContainer.

Sections are emergent — they correspond to contiguous ranges of blocks delimited by paragraphs carrying section breaks (Paragraph.hasSectionBreak). To create a new section boundary, call Paragraph.addSectionBreak on the paragraph that should be the last block of the section being closed. To remove a boundary, call Paragraph.removeSectionBreak.

Section-level properties (page setup, headers/footers) are accessed through Section handles, reachable via sections (enumerate) or Paragraph.findSection / Table.findSection (look up the section a given block belongs to).

Note: removing a paragraph that carries a section break (via BlockLevelContainer.removeElement on body.content()) drops the break with it. The closed section’s properties are lost; the next section’s properties take over the merged range. To preserve the closed section’s properties when collapsing a boundary, use Paragraph.removeSectionBreak instead.

Iterate the body’s blocks and report each block’s section:

const body = document.body();
for (const block of body.content().blocklevels()) {
const section = block.findSection();
console.log(block.type, section?.pageSetup().pageSize());
}

Add a new section break and configure the next section’s page setup:

const body = document.body();
const content = body.content();
const blocks = content.blocklevels();
const last = blocks[blocks.length - 1];
if (last.type === 'paragraph') {
last.addSectionBreak();
// The block(s) added after `last` belong to a new trailing section.
const tail = content.addParagraph();
const trailing = tail.findSection();
trailing?.pageSetup().setPageSize({ width: 12240, height: 15840 });
}

Replace text across the entire body:

body.replaceText(/TODO/g, { text: 'DONE', formatting: { highlight: '#00ff00' } });

replaceText: ReplaceTextSignature

Replaces all occurrences of a pattern across the body, including all sections’ headers and footers.

content(): BlockLevelContainer

Returns the body’s block-level content container (paragraphs and tables).

BlockLevelContainer

const content = body.content();
for (const block of content.blocklevels()) {
if (block.type === 'paragraph') {
console.log(block.asTextView().getPlainText());
}
}
content.addParagraph().asTextView().setText('Trailing paragraph.');

sections(): Section[]

Returns the section-property handles for this body in document order.

Section[]

The returned array always has length N + 1, where N is the number of paragraphs carrying a section break. The last entry corresponds to the trailing section.

const sections = body.sections();
console.log(`Document has ${sections.length} sections`);
sections.forEach((s, i) => console.log(`Section ${i + 1}`, s.pageSetup().pageSize()));