Compare Word Documents and Highlight Differences using Java

You can easily compare two or more Word documents and highlight the differences programmatically. You may need to compare multiple versions of the same Word file or different files for the differences and similarities in your Java application. In this article, you will learn how to compare two or more Word documents and highlight differences using Java.

The following topics are discussed/covered in this article:

Java API to Compare Word Documents

I will be using GroupDocs.Comparison for Java API for comparing the DOCX documents. It compares to detect content changes for words, paragraphs, and characters while providing a comparison document that lists a summary of differences. It also enables you to detect changes and differences in text style between similar document formats. The API supports comparing all industry-standard document formats such as PDF, HTML, Word, Excel, PowerPoint, Outlook emails, Visio diagrams, OpenDocument, AutoCAD, and images.

You can download the JAR of the API or just add the following pom.xml configuration in your Maven-based Java application to try the below-mentioned code examples.

<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

You can compare two or more Word documents by following the simple steps given below:

  1. Create an instance of the Comparer class
  2. Provide the source DOCX file path to the constructor
  3. Add target DOCX file to the comparison
  4. Call the Compare() method along with the output file path

The following code sample shows how to compare Word documents and highlights 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 resulting document also includes a summary page at the end of the document. It shows the summary of all the changes.

The Comparer class is the main class that controls the documents comparison process. The Compare() method of this class compares the source and target documents. This method saves results to the file path provided as an input parameter. The Add() method of this class, adds a file to the comparison process. You can easily add multiple files to the comparison by using the Add() method as shown below:

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

Get Text of the Changes using Java

You can get the text of the changes programmatically by following the simple steps given below:

  1. Create an instance of the Comparer class
  2. Provide the source DOCX file path to the constructor
  3. Add target DOCX file to the comparison
  4. Call the Compare() method
  5. Call the getChanges() method and get the details of changes
  6. Show the changes

The following code sample 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] 

You can get a list of changes between source and target files by calling the getChanges() method of the Comparer class. It returns a list of ChangeInfo objects. The ChangeInfo class provides methods to get details of the changes such as getText() to get the text of a specific change.

Compare Bookmarks in Word Documents

You can compare bookmarks present in Word documents programmatically by following the simple steps given below:

  1. Create an instance of the Comparer class
  2. Provide the source DOCX file path to the constructor
  3. Add target DOCX file to the comparison
  4. Create an instance of the CompareOptions
  5. Set the CompareBookmarks to true
  6. Call the Compare() method along with the output file path and CompareOptions object

The following code sample shows how to compare bookmarks in Word documents using Java.

Compare Bookmarks in Word Documents using Java

Compare Bookmarks in Word Documents using Java

You can enhance your comparison process by applying various compare options. For this purpose, the CompareOptions class enables you to set different compare options by providing various methods. The setCompareBookmarks() method enables you to compare the bookmarks available in the source and target documents.

Get a Free License

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

Conclusion

In this article, you have learned how to compare two or more Word documents and highlight differences using Java. You have also learned how to get the text of highlighted changes. Moreover, you have learned how to compare bookmarks in Word documents programmatically. You can learn more about GroupDocs.Comparison for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also