Compare Word documents in a DOCX file

Requirements

Apryse SDK's DOCX Compare utility compares two Microsoft Word documents and produces an output DOCX document in which every difference between them is recorded as a tracked change. DOCX Compare can be used to:

  • Review edits between revisions.
  • Build an audit trail of what changed.
  • Hand an editable, annotated document back to an author.

Because the output is a plain DOCX file, the differences can be viewed, accepted, or rejected in any DOCX editor.

The older version of the input Word document is called the original document, while the newer version is the revised document. The differences are always expressed relative to the original: content that exists only in the revised document is recorded as a tracked insertion, and content that exists only in the original document is recorded as a tracked deletion.

The comparison runs entirely within the Apryse SDK with no external or system dependencies. Results are deterministic and identical across Windows, Linux, macOS, and Android.

How DOCX Compare works

The comparison operates directly on the DOCX file format:

  • Text diffing: The algorithm used for text comparison produces diffs that read naturally, instead of an optimally minimal diff.
  • Broad document coverage: In addition to the main body text, the comparison also examines auxiliary content, including tables, lists, headers, footers, and styles.
  • Formatting changes: Differences in text, style, and table formatting are captured as tracked formatting changes, distinct from insertions and deletions.

Usage

The DOCX Compare method takes the paths to the original and revised DOCX files and the path where the output DOCX will be written. It returns a result object describing the outcome of the comparison.

1DOCXCompareOptions options;
2
3DOCXCompareResult result = DOCXCompare::Compare(
4 "original.docx",
5 "revised.docx",
6 "output_with_changes.docx",
7 &options);

The options argument is optional in some languages. When omitted, the comparison uses default settings.

Alternatively, the function CompareWithFilter can be used if the documents are not on disk.

Handling documents that already contain tracked changes

If either input document already contains tracked changes, the changes are automatically accepted before the comparison is performed, so the comparison reflects the final resolved text of each document. The resulting object reports whether each input contained pre-existing tracked changes.

Comparison result

Both Compare and CompareWithFilter return a DOCXCompareResult object with information about the comparison.

Comparison options

The comparison can be customized via a DOCXCompareOptions object, which controls how the resulting tracked changes are authored and how certain content is interpreted.

RevisedAuthor sets the author name recorded on the tracked changes in the output document. If it is not set, the author is taken from the last modified by value in the revised document's metadata. If that value is unavailable, the fixed author string "Comparison" is used.

The date stamped on the tracked changes can be controlled in two ways:

  • SetRevisedDate sets an explicit date, in ISO 8601 format (YYYY-MM-DDThh:mm:ss).
  • Alternatively, SetDateMode selects how the date is determined automatically:
    • e_revised_save_date (default) — Use the save date recorded in the revised document's metadata.
    • e_current_date — Use the current date and time.
    • e_empty_date — Leave the date empty.

TreatStruckoutTextAsDeleted controls how text that carries strikeout formatting is handled.

  • When this option is false (the default), text whose only change is the addition of strikeout formatting is reported as a tracked formatting change.
  • When this option is true, both input documents are pre-processed to remove struck-out text before comparison, so that such text is instead reported as a tracked deletion.

For example, suppose the original document contains the text ABC, and the revised document contains the same ABC but struck out. With the option disabled, this appears as a formatting change; with the option enabled it appears as a deletion.

Error handling

For comparison failures, like an invalid revised date or a malformed input document, an exception is thrown. Wrap comparison calls in the appropriate error-handling construct for your language and handle the failure accordingly.

Sample

Sample code

View the DOCX Compare sample code in C#, Go, Java, C++, Javascript, Python, PHP, Ruby, VB, and Objective-C.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales