Compare Word Documents and Highlight Differences using Java

Programmatically comparing Word files is essential for version control, document review, and content auditing. Whether you need to track changes across multiple revisions or identify similarities between different contracts, Java developers can now automate this process. In this guide, you will learn how to compare two or more Word documents and highlight differences using Java.

The following topics are covered:

Java API to Compare Word Documents

For this tutorial we use the GroupDocs.Comparison for Java library, which provides robust document comparison capabilities for DOCX, PDF, HTML, Excel, PowerPoint, Outlook, Visio, OpenDocument, AutoCAD, and image formats. The API detects word, paragraph, and character changes, generates a comparison document with a summary page, and highlights style modifications.

You can download the JAR directly or add the following pom.xml dependency to your Maven project.

<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>21.6</version> 
</dependency>

Compare Word Documents using Java

Follow these steps to compare multiple Word files:

  1. Instantiate the Comparer class with the source DOCX path.
  2. Add each target DOCX file using Add.
  3. Call Compare() with the desired output path.

The sample below demonstrates how to compare Word documents and highlight the differences using Java.

Source and Target Word Documents

Source and Target Documents

Compare Two Word Documents using Java

Compare Two Word Documents using Java

The generated comparison file includes a summary page that lists every change. The Comparer class drives the comparison process, while Add() lets you include any number of target documents:

comparer.Add("target2.docx");
comparer.Add("target3.docx");

Get Text of the Changes using Java

To extract the exact text of each change, perform the following:

  1. Create a Comparer instance with the source file.
  2. Add the target file(s).
  3. Execute Compare().
  4. Invoke getChanges() to retrieve a list of change objects.
  5. Use getText() on each ChangeInfo to obtain the changed text.

The code example below shows how to get the text of the changes using Java.

Count of changes: 10
Change Type: 2, Text: 
Change Type: 2, Text:  Company  HYPERLINK "http://www.aspose.com/" Aspose Pty Ltd Division GroupDocs 
Change Type: 2, Text: 
Change Type: 2, Text: Cool 
Change Type: 3, Text: test 
Change Type: 2, Text:  
Change Type: 2, Text: signatures
Change Type: 2, Text: Our 
Change Type: 2, Text: char&#091;
Change Type: 2, Text: 255] 

The getChanges() method returns a collection of ChangeInfo objects. Each object provides details such as change type, location, and the getText() method to fetch the exact modified content.

Compare Bookmarks in Word Documents

Bookmarks are often used to mark sections for review. To compare them across documents:

  1. Create a Comparer instance with the source file.
  2. Add the target file(s).
  3. Instantiate CompareOptions.
  4. Enable CompareBookmarks.
  5. Call Compare() with the output path and the configured options.

The snippet below illustrates how to compare bookmarks in Word documents using Java.

Compare Bookmarks in Word Documents using Java

Compare Bookmarks in Word Documents using Java

The CompareOptions class offers additional settings to fine‑tune the comparison process. Enabling setCompareBookmarks(true) ensures that bookmark differences are detected and highlighted in the result document.

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article you learned how to compare two or more Word documents and highlight differences using Java, how to extract the text of each change, and how to compare bookmarks programmatically. For deeper integration details, explore the GroupDocs.Comparison for Java documentation. If you have questions, visit the forum.

See Also