You can compare MS Word documents to know about the difference between the two versions of a document. MS Word provides a built-in option to compare the content of two documents and highlight the differences. While working with MS Word automation in Java, you may need to compare two or more Word documents programmatically. Therefore, this article covers how to compare two Word documents in Java. We will also demonstrate how to ignore the formatting of the documents and get comparison differences in a specific Word document.
- Java Library to Compare Word Documents
- 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 Library to Compare Two Word Documents
Aspose.Words for Java is a powerful document manipulation library 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 library 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>22.9</version>
<classifier>jdk17</classifier>
</dependency>
How to Compare Two Word Documents in 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.
Java Code to Compare Word Documents
The following code sample shows how to compare two MS Word documents using Java.
Java Compare Word DOCX by Ignoring Formatting
There might be a 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 DOC files in Java.
- 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 Document in MS Word Comparison
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.
Compare Word Files with Customized Granularity
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.
Java Difference Checker Library - Free License
Aspose offers a free temporary license for you to compare Word documents without any evaluation limitations.
Read More about Java Word Library
You can explore more about Aspose.Words for Java using the documentation.
Conclusion
In this article, you have seen how to compare two Word documents in Java. We have covered various options to customize the Word document comparison programmatically in Java.