Compare Documents

You can use Document.Compare method to compare two documents to see the difference between them. This method mimics Microsoft Word’s Compare feature and produces document difference as a number of edit and format revisions. The main idea is that if we reject all revisions then we get a document which is equal to the original document. On the contrary, if we accept all revisions then we get the final (comparison target) document.

Limitations

There are a few general limitations:

  • The document being compared must not have revisions before this method is called.
  • Markup - is limited to SmartTag only. Other markups are ignored completely.
  • Dml - Fallback shapes are compared instead of actual Dml comparison.

The following code example demonstrates the Normal Comparison Case. You can download the template files of below examples from here.

The following code example demonstrates how to test that Word Documents are “Equal”.

Compare Word Documents and Ignore Document Formatting

CompareOptions class allows to choose advanced options for document comparison operation. You can ignore document formatting, headers-footers, fields, footnotes, tables, text boxes, comments and case changes when documents are compared. Set the value of CompareOptions.IgnoreFormatting property to true to ignore the document formatting. The headers and footers contents are ignored when CompareOptions.IgnoreHeadersAndFooters is set to true. The following code example demonstrates how to ignore the document’s formatting and headers footers contents.

Set Target Document for Comparison Differences

MS Word has “Show changes in” option in compare document dialogue box and the result of the comparison depends on this option. CompareOptions.Target property serves this purpose. This property specifies which document shall be used as a target during the comparison. For example, this option together with IgnoreFormatting setting determines which document has to be used as a formatting source for ranges of equal text. ComparisonTargetType enumeration is used to specify base document which will be used during the comparison. The following code example demonstrates how to set the target document for a comparison of two documents.