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

Body

Body:

Represents the document body containing all sections.

Body is the container for all Section elements in a document. Each section can have its own page setup (size, margins, orientation) and headers/footers. Most documents have a single section, but documents can be divided into multiple sections for different page layouts or header/footer configurations.

The body is accessed via Document.body and provides methods to:

Access and iterate through sections:

const body = document.body();
const sections = body.sections();
console.log(`Document has ${sections.length} sections`);
for (const section of sections) {
const content = section.content();
const paragraphs = content.blocklevels()
.filter(bl => bl.type === 'paragraph');
console.log(`Section has ${paragraphs.length} paragraphs`);
}

Add a new section to the document:

const body = document.body();
// Add section at the end
const newSection = body.addSection();
// Configure page setup
const pageSetup = newSection.pageSetup();
pageSetup.setPageSize({ width: 12240000, height: 15840000 }); // Letter size
// Add content
const para = newSection.content().addParagraph();
para.asTextView().setText('This is a new section');

Replace text across all sections:

const body = document.body();
// Replace across entire document body
const count = body.replaceText(/TODO/g, {
text: 'DONE',
formatting: { highlight: '#00ff00' }
});
console.log(`Replaced ${count} occurrences across all sections`);

Remove a section:

const body = document.body();
const sections = body.sections();
if (sections.length > 1) {
// Remove the last section
const removed = body.removeSection(sections.length - 1);
if (removed) {
console.log('Removed last section');
}
}

replaceText: ReplaceTextSignature

Replaces all occurrences of a pattern across all sections.

Convenience method that searches and replaces text across all sections in the body, including all content, headers, and footers. This provides a simpler API than manually iterating through sections.

See ReplaceTextSignature for detailed parameter and usage information.

const count = body.replaceText(/old/g, 'new');
console.log(`Replaced ${count} occurrences`);
body.replaceText(/ERROR/gi, {
text: 'ERROR',
formatting: { color: '#ff0000', bold: true }
});

sections(): Section[]

Gets all sections in the document body.

Section[]

An array of Section objects

Returns all sections in the document in order. Most documents have a single section, but documents can contain multiple sections with different page setups or headers/footers.

const sections = body.sections();
console.log(`Document has ${sections.length} sections`);
sections.forEach((section, index) => {
console.log(`Section ${index + 1}`);
});

addSection(index?): Section

Adds a new section to the document body.

number

Optional zero-based position to insert the section. If omitted, adds at the end.

Section

The newly created Section

Creates and inserts a new section at the specified position. If no index is provided, the section is added at the end. The new section is initialized with default page setup and empty content.

Use this when you need different page layouts, orientations, or headers/footers for different parts of your document.

Add section at the end:

const newSection = body.addSection();
const para = newSection.content().addParagraph();
para.asTextView().setText('New section content');

Insert section at specific position:

// Insert as the second section (index 1)
const section = body.addSection(1);

removeSection(index): Section | undefined

Removes a section from the document body.

number

Zero-based index of the section to remove

Section | undefined

The removed Section, or undefined if the index is invalid

Removes the section at the specified index. If the index is out of bounds, returns undefined. Be cautious when removing sections as this removes all content within that section.

const sections = body.sections();
if (sections.length > 1) {
// Remove the last section
const removed = body.removeSection(sections.length - 1);
if (removed) {
console.log('Successfully removed section');
}
}

Remove specific section with confirmation:

const indexToRemove = 2;
const sections = body.sections();
if (indexToRemove < sections.length) {
const removed = body.removeSection(indexToRemove);
console.log(`Removed section ${indexToRemove + 1}`);
}