w:ins, w:del, w:rPrChange …), so Word, Google Docs and LibreOffice show them as usual.
The examples use Node. In a page, import WordDocument from @reductoai/docx/officejs or @reductoai/docx/browser and use fromBytes / toBytes. The editing code is the same.
Turn on tracking
revisionAuthor. Set changeTrackingMode = "Off" to make clean edits again.
Replace, insert and delete
doc.body.search does not search text boxes, headers, footers or notes; doc.search adds the body’s text boxes. Search headers and footers through their own handles, for example section.getHeader("Primary").search(...).
Comment on a change
revisionAuthor. See Comments for replies and resolve.
Read the revisions
rev.getRange() gives the text as a Range, and rev.getRange().paragraph gives its paragraph. A paragraph or range also has getTrackedChanges().
doc.revisions groups changes as Word’s Reviewing Pane does, in Word’s order.
What joins into one revision:
- Adjacent insertions or deletions by one author are one revision, also across paragraphs.
- Format changes join only in one paragraph, and only when they record the same old formatting.
- Neighbouring paragraph format changes join when the paragraphs have the same new formatting and the changes record the same properties, whatever the old values.
- A format change on text that the author’s next insertion continues joins that insertion’s format change.
- Cell, row and table format changes are one revision for each run of adjacent rows by one author. Its text is those rows.
- A move is one revision for each side.
- New or deleted cells of a column list their text first, then one
CellInsertionorCellDeletionfrom the first cell to the end of the last cell’s row. - In an inserted row, another author’s deletion is one revision for each cell. The deletion in the first cell comes before the row.
- A revision at the start of a paragraph, table or section comes before that paragraph’s, table’s or section’s own format change.
- A deleted row comes before its insertion when both cover the same row.
text of a revision:
- A section’s format change has the whole section as its text.
- An equation’s text is its letters as Word shows them, such as
𝑥−1. - A picture reads as
/. - A format change on a field code reads as the code.
acceptAllRevisions() and rejectAllRevisions() still settle them:
- A format change that the style already gives (bold on a bold heading).
- A table’s own format change with no cell or row format change by its author.
getTrackedChanges() keeps a finer list. Use it when you must settle one cell or one paragraph alone.
Before and after text
Accept and reject
accept() or reject(), the handle is no longer valid. Read doc.revisions again for the new list.
Render the redline
To get a PDF that shows the changes, as Word prints with All Markup:await doc.toPdf("msa-redline.pdf", { trackedChanges: "inline", comments: "balloons" }). See PDF: show tracked changes and comments.
Redline from an LLM
A common pattern: an LLM proposes edits as find-and-replace pairs, and OfficeJS applies them as tracked changes for a human to review.doc.body.text or await doc.readPage(n), and ask for quotes that occur once. Report missed back to the model so it can try again.