Files
docx/docs/usage/comments.md
T
DolanandClaude Opus 5.5 c169dd1a83 feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded (#3556)
* feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded

The docs' code examples were plain snippets, often fragments that couldn't run. A code block marked ```ts live is now
an example readers can see and change: the page runs it with docx, draws the document it makes next to the code with
docx-preview, and offers the .docx to download. Clicking the code opens an editor (Monaco) that knows docx's types,
the pages are drawn again as the code changes, and View closes the editor. Split, Code and Preview show the code and
the pages side by side, or one of them. The demos a page includes are live too.

Every example in the docs that builds or patches a document is now live: 443 of them, each a whole file with its
imports, so it runs as written. Code about other things, such as installing, bundlers or servers, stays plain.

Running them showed docs that were wrong, fixed here:
- Landscape: docx swaps a landscape page's width and height itself, so the docs' swapped sizes made a tall page
  marked as landscape
- Comments: `comments.children` takes plain objects, not `new Comment(...)`
- Custom properties are text only
- APIs that no longer exist: MathText, Row, `.then` on Packer.toStream, patchDocument(buffer, ...),
  createParagraph().heading1(), a border's `value`, TextRun's `.break()`
- The templates and patcher examples read files that aren't in the repository. They now patch the templates in
  demo/assets, or make their own

`npm run build.docs` copies docx's browser builds into docs/live-examples/lib, so the docs use the docx they
describe, and the Pages workflow runs it. The folder is gitignored, and cspell and ESLint skip it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* docs: the bullet examples draw a bullet, and the patcher's usage writes the patched document

"ὠ" is U+1F60, a Greek omega, since \u takes four hex digits, so the two live bullet examples on
the numbering page drew it as each item's bullet. They use "•" now. The patcher's usage example
now awaits patchDocument and writes the document it makes, as the example after it does.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 13:00:29 +01:00

12 KiB

Comments

!> Comments require an understanding of Sections and Paragraphs.

Comments allow you to add annotations to specific parts of your document, similar to the review feature in Microsoft Word.

Overview

To add comments in docx:

  1. Define all comments in the comments block of the Document
  2. Each comment has a unique id
  3. Mark where comments appear using CommentRangeStart, CommentRangeEnd, and CommentReference

Basic Comment

Add a comment to a range of text:

import { CommentRangeEnd, CommentRangeStart, CommentReference, Document, Paragraph, TextRun } from "docx";

const doc = new Document({
    comments: {
        children: [
            {
                id: 0,
                author: "John Smith",
                date: new Date(),
                children: [new Paragraph("This needs to be reviewed.")],
            },
        ],
    },
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new TextRun("This is normal text. "),
                        new CommentRangeStart(0),
                        new TextRun("This text has a comment."),
                        new CommentRangeEnd(0),
                        new CommentReference(0),
                    ],
                }),
            ],
        },
    ],
});

Comment Options

Property Type Notes Description
id number Required Unique identifier
author string Optional Comment author name
date Date Optional Comment timestamp
initials string Optional Author initials
children Paragraph[] Required Comment content
parentId number Optional ID of parent comment for reply threading
resolved boolean Optional Whether the comment thread is marked as resolved. The entire thread displays as resolved in Word

Point Comment

Add a comment at a specific point (not a range):

import { CommentReference, Document, Paragraph, TextRun } from "docx";

const doc = new Document({
    comments: {
        children: [
            {
                id: 0,
                author: "John Smith",
                date: new Date(),
                children: [new Paragraph("A comment at this point.")],
            },
        ],
    },
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new TextRun("Check this point"),
                        new CommentReference(0), // Comment appears here
                    ],
                }),
            ],
        },
    ],
});

Multiple Comments

Add several comments to a document:

import { CommentRangeEnd, CommentRangeStart, CommentReference, Document, Paragraph, TextRun } from "docx";

const doc = new Document({
    comments: {
        children: [
            {
                id: 0,
                author: "Alice",
                date: new Date("2024-01-15"),
                children: [new Paragraph("First comment")],
            },
            {
                id: 1,
                author: "Bob",
                date: new Date("2024-01-16"),
                children: [new Paragraph("Second comment")],
            },
        ],
    },
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new CommentRangeStart(0),
                        new TextRun("Text with first comment."),
                        new CommentRangeEnd(0),
                        new CommentReference(0),
                        new TextRun(" "),
                        new CommentRangeStart(1),
                        new TextRun("Text with second comment."),
                        new CommentRangeEnd(1),
                        new CommentReference(1),
                    ],
                }),
            ],
        },
    ],
});

Reply Threads

Create comment replies using the parentId property to build a thread. Reply comments must also have CommentRangeStart, CommentRangeEnd, and CommentReference wrapping the same text as their parent — Word uses these to anchor each comment to document text, and without them the comment has no visible anchor and may be discarded:

import { CommentRangeEnd, CommentRangeStart, CommentReference, Document, Paragraph, TextRun } from "docx";

const doc = new Document({
    comments: {
        children: [
            {
                id: 0,
                author: "Alice",
                date: new Date("2024-01-15"),
                children: [new Paragraph("Is this correct?")],
            },
            {
                id: 1,
                author: "Bob",
                date: new Date("2024-01-16"),
                parentId: 0, // This is a reply to comment 0
                children: [new Paragraph("Yes, I verified it.")],
            },
            {
                id: 2,
                author: "Alice",
                date: new Date("2024-01-17"),
                parentId: 0, // Another reply to comment 0
                children: [new Paragraph("Thanks for checking!")],
            },
        ],
    },
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new CommentRangeStart(0),
                        new CommentRangeStart(1),
                        new CommentRangeStart(2),
                        new TextRun("Text with a comment thread."),
                        new CommentRangeEnd(0),
                        new TextRun({ children: [new CommentReference(0)] }),
                        new CommentRangeEnd(1),
                        new TextRun({ children: [new CommentReference(1)] }),
                        new CommentRangeEnd(2),
                        new TextRun({ children: [new CommentReference(2)] }),
                    ],
                }),
            ],
        },
    ],
});

Resolved Comments

Mark a comment thread as resolved using the resolved property. Setting resolved: true on any comment in a thread causes Word to display the entire thread as resolved. For clarity, set it on the root comment:

import { CommentRangeEnd, CommentRangeStart, CommentReference, Document, Paragraph, TextRun } from "docx";

const doc = new Document({
    comments: {
        children: [
            {
                id: 0,
                author: "Charlie",
                date: new Date("2024-01-15"),
                resolved: true, // Marks the entire thread as resolved
                children: [new Paragraph("This timeline needs updating.")],
            },
            {
                id: 1,
                author: "Diana",
                date: new Date("2024-01-16"),
                parentId: 0,
                children: [new Paragraph("Done - updated the dates.")],
            },
        ],
    },
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new CommentRangeStart(0),
                        new CommentRangeStart(1),
                        new TextRun("The project finishes in March."),
                        new CommentRangeEnd(0),
                        new TextRun({ children: [new CommentReference(0)] }),
                        new CommentRangeEnd(1),
                        new TextRun({ children: [new CommentReference(1)] }),
                    ],
                }),
            ],
        },
    ],
});

Rich Text Comments

Comments can contain formatted text:

import { CommentRangeEnd, CommentRangeStart, CommentReference, Document, Paragraph, TextRun } from "docx";

const doc = new Document({
    comments: {
        children: [
            {
                id: 0,
                author: "Reviewer",
                children: [
                    new Paragraph({
                        children: [
                            new TextRun({ text: "Important: ", bold: true }),
                            new TextRun("Please verify the figures in this section."),
                        ],
                    }),
                    new Paragraph("See page 12 of the source document."),
                ],
            },
        ],
    },
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new CommentRangeStart(0),
                        new TextRun("Sales grew by 12% this year."),
                        new CommentRangeEnd(0),
                        new CommentReference(0),
                    ],
                }),
            ],
        },
    ],
});

Complete Example

Document with review comments:

import { CommentRangeEnd, CommentRangeStart, CommentReference, Document, HeadingLevel, Packer, Paragraph, TextRun } from "docx";
import * as fs from "fs";

const doc = new Document({
    comments: {
        children: [
            {
                id: 0,
                author: "Editor",
                date: new Date(),
                children: [new Paragraph("Consider rephrasing this for clarity.")],
            },
            {
                id: 1,
                author: "Fact Checker",
                date: new Date(),
                children: [
                    new Paragraph({
                        children: [new TextRun({ text: "Verified ", bold: true }), new TextRun("- Source: Annual Report 2023")],
                    }),
                ],
            },
        ],
    },
    sections: [
        {
            children: [
                new Paragraph({
                    text: "Quarterly Report",
                    heading: HeadingLevel.HEADING_1,
                }),
                new Paragraph({
                    children: [
                        new TextRun("Our "),
                        new CommentRangeStart(0),
                        new TextRun("company achieved remarkable growth"),
                        new CommentRangeEnd(0),
                        new CommentReference(0),
                        new TextRun(" this quarter. "),
                        new CommentRangeStart(1),
                        new TextRun("Revenue increased by 25%"),
                        new CommentRangeEnd(1),
                        new CommentReference(1),
                        new TextRun(" compared to last year."),
                    ],
                }),
            ],
        },
    ],
});

Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("document.docx", buffer);
});

Demo

Basic usage

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/comments/comments.ts

Comment Replies and Resolved state

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/comments/comment-replies.ts