An error occurred while retrieving the results. Please refresh the page and try again.
Compare Documents
Purpose Summary
This page outlines how to compare two Word documents for differences using Aspose.Words.
Comparing documents is a process that identifies changes between two documents and contains the changes as revisions. This process compares any two documents, including versions of one specific document, then the changes between both documents will be shown as revisions in the first document.
The comparison method is achieved by comparing words at character level or at word level. If a word contains a change of at least one character, in the result, the difference will be displayed as a change of the entire word, not a character. This process of comparison is a usual task in the legal and financial industries.
Instead of manually searching for differences between documents or between different versions of them, you can use Aspose.Words for comparing documents and getting content changes in formatting, header/footer, tables, and more.
This article explains how to compare documents and how to specify the advanced comparing properties.
Try online
You can compare two documents online by using the Document comparison online tool.
Note that the comparison method, described below, is used in this tool to ensure getting equal results. So you will get the same results even by using the online comparison tool or by using the comparison method in Aspose.Words.
Limitations and Supported File Formats
Comparing documents is a very complex feature. There are varied parts of content combination that need to be analyzed to recognize all differences. The reason for this complexity is due to the fact that Aspose.Words aims to get the same comparison results as the Microsoft Word comparison algorithm.
The general limitation for two documents being compared is that they must not have revisions before calling the compare method as this limitation exists in Microsoft Word.
Compare Two Documents
When you compare documents, differences of the latter document from the former show up as revisions to the former. When you modify a document, each edit will have its own revision after running the compare method.
Aspose.Words allows you to identify documents differences using the Compare method – this is similar to the Microsoft Word document compare feature. It allows you to check documents or document versions to find differences and changes, including formatting modifications such as font changes, spacing changes, the addition of words and paragraphs.
As a result of comparison, documents can be determined as equal or not equal. The term “equal” documents means that the comparison method is not able to represent changes as revisions. This means that both document text and text formatting are the same. But there can be other differences between documents. For example, Microsoft Word supports only format revisions for styles, and you cannot represent style insertion/deletion. So documents can have a different set of styles, and the Compare method still produces no revisions.
The following code example shows how to check if two documents are equal or not:
Specify Advanced Comparison Options
There are many different properties of the CompareOptions class which you can apply when you want to compare documents.
For example, Aspose.Words allows you to ignore changes made during a comparison operation for certain types of objects within the original document. You can select the appropriate property for the object type, such as IgnoreHeadersAndFooters, IgnoreFormatting, IgnoreComments, and others by setting them to “true”.
In addition, Aspose.Words provides the Granularity property with which you can specify whether to track changes by character or by word.
Another common property is a choice in which document to show comparison changes. For example, the “Compare documents dialogue box” in Microsoft Word has the option “Show changes in” – this also affects the comparison results. Aspose.Words provides the Target property that serves this purpose.
The following code example shows how to set the advanced comparing properties:
Related APIs
- Document.Compare – compares two documents and records differences as revisions
- CompareOptions – specifies comparison behavior (e.g., ignore headers, formatting, comments)
- Granularity – controls whether changes are tracked by character or by word
- CompareTarget – determines which document displays the changes
- Revision – represents a single change detected during comparison
- Revisions – collection of all changes recorded in the document after comparison
FAQ
-
Q: Which file formats can be compared with Aspose.Words?
A: Any format listed in the supported document formats can be used for comparison, including DOC, DOCX, ODT, RTF, HTML, PDF (as source) and others. The API works withDocumentobjects, so you can load a file of any supported type and compare it directly. -
Q: Do the source documents need to be free of revisions before calling
Compare?
A: Yes. Aspose.Words follows the same limitation as Microsoft Word: the documents must not contain existing revisions whenCompareis invoked. Remove revisions first (e.g.,document.AcceptAllRevisions();) to avoid unexpected results. -
Q: How can I ignore changes in headers, footers, or formatting during comparison?
A: Use theCompareOptionsclass. Set properties such asIgnoreHeadersAndFooters = true,IgnoreFormatting = true, orIgnoreComments = truebefore callingdocument.Compare(otherDoc, author, date, compareOptions);. -
Q: How do I determine programmatically whether two documents are identical after comparison?
A: After callingCompare, inspect theRevisionscollection of the first document. Ifdocument.Revisions.Count == 0, the documents are considered equal (no detectable changes). -
Q: What is the difference between the
GranularityandTargetoptions?
A:Granularitycontrols the level of change tracking—Charactertracks changes per character, whileWordtracks per word.Targetspecifies which document will display the revisions (e.g.,CompareOptions.Target = CompareTarget.New;shows changes in the second document). Adjust both to match the desired comparison behavior.
