The comparison of MS Word documents is performed to know about the difference between two versions of a document. MS Word provides a built-in option to compare the content of two documents. However, with the increased number of documents, it becomes difficult to perform the document comparison manually. In order to automate this process, this article covers how to compare two MS Word (DOC/DOCX) documents using Java.
- MS Word Document Comparison API for Java
- Compare MS Word Documents using Java
- Ignore Formatting in MS Word Document Comparison
- Set Target Document for Comparison Differences
- Set Granularity to Compare MS Word Documents
Java API to Compare MS Word Documents
Aspose.Words for Java is a powerful document manipulation API that lets you create, read, modify, and convert MS Word documents. In addition, it allows you to compare two Word documents along with considering or ignoring the formatting of the content. You can download the API or get it installed using Maven configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>20.9</version>
<classifier>jdk17</classifier>
</dependency>
Compare MS Word Documents using Java
The following are the steps to perform a simple Word document comparison using Aspose.Words for Java API.
- Load the documents to be compared using the Document class.
- Call Document.compare(Document, String, Date) method to compare the document with the one passed as an argument.
The following code sample shows how to compare two MS Word documents using Java.
Java Word Document Comparison – Ignore Formatting
There might be the case when the documents have different formatting applied to the content. In such cases, you would prefer to compare the text only while ignoring the formatting, headers/footers, footnotes, tables, comments, etc. The following are the steps to ignore the document’s formatting when comparing two MS Word documents.
- Load the documents to be compared using the Document class.
- Create an object of CompareOptions class.
- Set CompareOptions.setIgnoreFormatting(true).
- Compare the documents using Document.compare(Document, String, Date, CompareOptions) method.
The following code sample shows how to compare MS Word documents ignoring the content’s formatting using Java.
Set Target Word Document for Comparison Differences
Aspose.Words for Java also allows you to specify the source Word document during the comparison. For this, you can use CompareOptions.setTarget() property which relates to MS Word’s “Show changes in” option. The following code sample shows how to specify the target document in the comparison.
Set Granularity to Compare Word Files in Java
You can also set the granularity of changes when comparing two MS Word documents. This can be done using CompareOptions.setGranularity() property. The following are the possible granularity options.
The following code sample shows how to set granularity when comparing MS Word documents using Java.
Conclusion
In this article, you have seen how to compare two MS Word documents in various scenarios using Java. You can explore more about Aspose.Words for Java using the documentation.
Try for Free
Aspose offers a temporary license for you to try its APIs for free. You can get yours to evaluate Aspose.Words for Java.