> ## Documentation Index
> Fetch the complete documentation index at: https://docs.reducto.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Comments

## Add a comment

A comment anchors to a range or a paragraph.

```ts theme={null}
const range = doc.body.search("indemnify").getFirst();
const comment = range.insertComment("Is this mutual?");

doc.body.paragraphs.items[3].insertComment("Check the cap.");
```

The author is `doc.revisionAuthor`.

## Comment across paragraphs

Join two ranges with `expandTo`. The comment opens in the first paragraph and closes in the last.

```ts theme={null}
const start = doc.body.search("Compelled disclosure").getFirst();
const end = doc.body.search("protective order.").getFirst();
start.expandTo(end).insertComment("Move this clause next to Exceptions.");

const ps = doc.body.paragraphs.items;
ps[4].getRange("Whole").expandTo(ps[6].getRange("Whole")).insertComment("Check all three.");
```

Both ranges must be in the same story. A body range and a footer range cannot share a comment.

## Comment on a tracked change

A deletion's range holds its struck text, as in Word. Its `text` is empty, but `isEmpty` is false.
Join it to the insertion that replaces it to comment on the whole change.

```ts theme={null}
const changes = paragraph.getRange().getTrackedChanges().items;
const deleted = changes.find((c) => c.type === "Deleted")!.getRange();
const added = changes.find((c) => c.type === "Added")!.getRange();
deleted.expandTo(added).insertComment("Agreed: sixty days.");
```

A range that starts or ends on a deletion keeps the struck text for `insertComment`, `font`,
`delete`, `getOoxml`, `getTrackedChanges` and `compareLocationWith`. `insertText` at its
`Start`, a tracked `Replace` and `insertParagraph("Before")` go before the struck text.

## Read comments

```ts theme={null}
for (const c of doc.comments.items) {
  console.log(c.authorName, c.creationDate, c.content, "→", c.getRange().text);
  for (const reply of c.replies.items) console.log("  ↳", reply.authorName, reply.content);
}
```

A range, a paragraph and the body also have `getComments()` for the comments inside them.

## Reply, resolve and delete

```ts theme={null}
const thread = doc.comments.getFirst();
thread.reply("Yes, both parties.");
thread.resolved = true;
thread.content = "Is this mutual? (edited)";

doc.comments.items.filter((c) => c.resolved).forEach((c) => c.delete());
```

`delete()` also removes the replies and the anchors in the text.

## Show comments in a PDF

```sh theme={null}
npx docx pdf input.docx -o review.pdf --comments balloons
```

Comments then show in a margin to the right of each page, as Word prints them.
