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

Document

Document:

Represents the root of a document structure in the programmatic API.

Document is the top-level entry point for programmatically accessing and modifying document content. It provides access to the document Body, which contains all sections and their content.

You typically obtain a Document instance from a transaction context when using the programmatic API to modify documents. The document is organized hierarchically:

Document
└─ Body
└─ Section(s)
├─ Content (BlockLevelContainer)
│ └─ Paragraph(s) / Table(s)
└─ Headers & Footers

For document-wide operations, you can use the convenience replaceText method to search and replace across all content without manually traversing the hierarchy.

Access document structure and iterate through content:

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 blockLevels = content.blocklevels();
console.log(`Section has ${blockLevels.length} paragraphs/tables`);
}

Replace text across entire document:

// Replace all occurrences of "TODO" with "DONE" throughout the document
const count = document.replaceText(/TODO/g, 'DONE');
console.log(`Replaced ${count} occurrences across entire document`);

Replace text with formatting across document:

// Highlight all instances of "IMPORTANT" in the entire document
document.replaceText(/IMPORTANT/gi, {
text: 'IMPORTANT',
formatting: {
highlight: '#ffff00',
bold: true
}
});

Typical usage in a transaction:

await docAuthDoc.transaction(async (context) => {
const { draft } = context;
// 'draft' is a Programmatic.Document
const body = draft.body();
const sections = body.sections();
// Add a new section
const newSection = body.addSection();
const para = newSection.content().addParagraph();
para.asTextView().setText('New section content');
// Or use document-wide replace
draft.replaceText(/old/g, 'new');
return { commit: true };
});

replaceText: ReplaceTextSignature

Replaces all occurrences of a pattern throughout the entire document.

Convenience method that searches and replaces text across all document content, including all sections, paragraphs, tables, headers, and footers. This is equivalent to manually calling Body.replaceText on the document body, but provides a simpler API for document-wide operations.

See ReplaceTextSignature for detailed parameter and usage information.

Simple document-wide replacement:

const count = document.replaceText('old text', 'new text');
console.log(`Replaced ${count} occurrences`);

Replace with formatting:

document.replaceText(/ERROR/g, {
text: 'ERROR',
formatting: { color: '#ff0000', bold: true }
});

Dynamic replacement with callback:

// Convert all numbers to currency format
document.replaceText(/\$?(\d+(\.\d{2})?)/g, (match) => {
const amount = parseFloat(match.replace('$', ''));
return {
text: `$${amount.toFixed(2)}`,
formatting: { color: '#00aa00' }
};
});

body(): Body

Gets the document body containing all sections.

Body

The Body of the document

Returns the document’s body, which contains all sections. Each section has its own content (paragraphs and tables) and headers/footers. Use this as the entry point for navigating and modifying document structure.

const body = document.body();
const sections = body.sections();
// Work with sections
for (const section of sections) {
const content = section.content();
// ... work with content
}