Skip to content
Document Authoring DA  API Docs v1.15.1
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 owns the flat block-level content (paragraphs and tables) plus section-property handles delimited by section breaks.

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
├─ content(): BlockLevelContainer (paragraphs and tables)
├─ sections(): Section[] (page setup + headers/footers per range)
└─ replaceText(): number (search/replace across body + headers/footers)

To navigate from a block back to its containing section, call Paragraph.findSection or Table.findSection.

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();
console.log(`Document has ${body.sections().length} sections`);
for (const block of body.content().blocklevels()) {
const section = block.findSection();
console.log(block.type, section?.pageSetup().pageSize());
}

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();
// Append a paragraph and start a new section after it
const para = body.content().addParagraph();
para.asTextView().setText('New section content');
para.addSectionBreak();
// 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.

Body

The Body of the document

Returns the document’s body, which owns the flat block-level content (paragraphs and tables) plus the section-property handles that apply to ranges within them.

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

commentThreads(): CommentThreadCollection

Gets the document-root comment thread collection.

CommentThreadCollection

A CommentThreadCollection for querying and creating comment threads

Use this as the entry point for programmatic comment workflows. The API exposes comment threads from the document root only, even when the thread is anchored to text inside a paragraph, header, footer, or table cell.

The collection works with comment threads rather than raw comment records. Query results include the top-level comment, flat replies, and an inferred text anchor reconstructed from document content.

const threads = draft.commentThreads().all({ status: 'open' });
for (const thread of threads) {
console.log(thread.comment.text);
}