DOCX
Comments and Revisions
Document comments, track changes, and structured document tags (content controls)
Comments
Add comments to document content using CommentRangeStart, CommentRangeEnd, and CommentReference:
import {
Document,
Paragraph,
TextRun,
Comments,
Comment,
CommentRangeStart,
CommentRangeEnd,
CommentReference,
} from "@office-open/docx";
const doc = new Document({
comments: {
comments: new Comments({
children: [
new Comment({
id: 0,
author: "Author Name",
date: new Date(),
children: [
new Paragraph({ children: [new TextRun("Please review this section.")] }),
],
}),
],
}),
},
sections: [
{
children: [
new Paragraph({
children: [
new CommentRangeStart(0),
new TextRun("This text has a comment attached."),
new CommentRangeEnd(0),
new CommentReference(0),
],
}),
],
},
],
});
Track Revisions
Track insertions, deletions, and moves in documents:
import { Paragraph, TextRun, InsertedTextRun, DeletedTextRun } from "@office-open/docx";
// Insertion
new Paragraph({
children: [
new InsertedTextRun({
author: "Author",
date: new Date(),
children: [new TextRun("This text was inserted.")],
}),
],
});
// Deletion
new Paragraph({
children: [
new DeletedTextRun({
author: "Author",
date: new Date(),
children: [new TextRun("This text was deleted.")],
}),
],
});
Structured Document Tags (Content Controls)
Use SDT to create editable regions and form controls:
import { StructuredDocumentTagRun, StructuredDocumentTagBlock } from "@office-open/docx";
// Inline content control
new Paragraph({
children: [
new StructuredDocumentTagRun({
alias: "FirstName",
tag: "first-name",
children: [new TextRun("John")],
}),
],
});
// Block-level content control
new StructuredDocumentTagBlock({
alias: "Section",
tag: "custom-section",
children: [new Paragraph("Editable content inside the control.")],
});
SDTs support various control types for forms, templates, and document automation.
Document Protection
Restrict editing on the document using settings:
import { Document } from "@office-open/docx";
const doc = new Document({
features: {
updateFields: true,
},
settings: {
protection: {
type: "readOnly",
password: "secret",
},
},
sections: [
// ...
],
});
Protection types: "readOnly", "comments", "trackedChanges", "forms".
Track Changes Options
When using InsertedTextRun and DeletedTextRun:
| Option | Type | Description |
|---|---|---|
author | string | Author name for the revision |
date | Date | Timestamp of the change |
id | number | Revision ID (auto-generated if omitted) |