DOC and DOCX are among the most common document formats used to draft official and personal documents. If a document is changed several times or if it has multiple versions, you can easily compare two different versions of that document. For you as a Java programmer, this article discusses the basic way of how to compare two Word documents and how to highlight the identified differences in Java.

Compare Word Documents to find differences in Java

Java API to Compare Word Documents

GroupDocs provides document and image comparison APIs for different platforms. I will use its GroupDocs.Comparison for Java APIs for comparing Word documents.

You may download the JAR file from the downloads section, or just get the repository and dependency configurations for the pom.xml of your maven-based Java applications.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-comparison</artifactId>
        <version>22.3</version> 
</dependency>

Compare Word Documents in Java

You can compare the two versions of a document to find the added, deleted, and changed content. As a result, you will get a new document that shows all the identified changes. The following are the steps to compare any two Word (DOC, DOCX) documents and highlight their differences.

  • Firstly, load the first Word document using Comparer class.
  • Then, add the second file to the Comparer using add() method.
  • Finally, compare and get the summary of the changes by just calling compare() method.

The following Java code shows how to compare Word documents and get the changes in the resultant document.

Get a Free API License

You can get a free temporary license to use the API without the evaluation limitations.

Conclusion

To sum up, we learned the simple way to compare two Word documents (DOC/DOCX) in Java. As an output, you got the summary of inserted or deleted content. In a separate article, we will discuss further ways to compare protected word documents and accept or reject the identified changes.

Try building your own document comparison Java application. You may learn more about the APIs using the documentation and API reference.

The easy way to experience the examples is from GitHub. Contact us for any query via the forum.

See Also