compare word documents in Java

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 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.

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.

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.

See Also

Convert DOC/DOCX to JPG in C# .NETConvert DOC/DOCX to JPG in JavaConvert DOC/DOCX to PNG in C# .NET
Convert DOC/DOCX to PNG in JavaConvert DOCX to HTML in C#Convert DOCX to TXT in Python
Merge MS Word Documents using JavaCreate Rich Word Documents in JavaMS Word DOC/DOCX to PDF in Java
PNG to Word in C# .NETJPG to Word in C# .NETImage to Word in C#
Word to HTML in C#Word DOCX to Markdown in JavaExtract Images from Word DOC in Java
Word DOC to Markdown in JavaWord DOC DOCX to Markdown in C#Extract Text from Word Documents in Java
Merge MS Word Documents using C# .NETWord DOC to PNG, JPEG, BMP, GIF, or TIFF in C#Word DOC to PNG, JPEG, BMP, GIF, or TIFF in Java