We already discussed the comparison of two Word documents even if they were password protected. What if, there are more than two files for comparison? In this article, we will look at how to compare multiple Word documents in Java. Further, we will learn how to accept or reject each of the identified changes to get the desired output.

Compare Multiple Word Documents to find differences in Java

The recent short series of articles already covered the Java way how to:

By the end of this article, you will also have a good understanding of how to:

If your development environment is not yet ready to perform the comparison, let’s quickly prepare it.

Java Library to Compare Two or More Word Documents – Setup

I hope you are familiar with the document comparison Java API by now. I will be using the same GroupDocs.Comparison to move ahead. You can download or quickly check the development environment of your maven-based applications using the following configurations:

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

Now let’s jump to the objective and start with comparing multiple Word files.

Compare More than Two Word Files in Java

Comparing multiple files is not so different as compared to comparing only two files. Just keep on adding more files to the Comparer and finally, hit the compare as you do when comparing two files. The following are the steps to compare more than two Word documents in Java to identify the changes and highlight the found differences.

  • Load the first DOC/DOCX file using the Comparer class.
  • Add the second file to Comparer using the add() method.
  • Keep on adding further document(s) to the Comparer using the same/similar add() methods.
  • Call the compare method to perform the comparison and get the summary of identified changes.

The following example shows how to compare multiple Word files in Java and get the changes in the resultant document.

Accept or Reject Identified Changes within Word Files using Java

Pretty sure that you are familiar with the track changes feature of Microsoft Word. Similar to that, you can programmatically accept or reject each of the identified changes for the final output document. The following steps show how you can compare and then accept or reject the identified changes within the DOC/DOCX files of Word documents.

  • Load the source DOC/DOCX document using the Comparer class.
  • Add the second Word document to the Comparer using the add() method.
  • Compare the loaded files using the compare() method.
  • Fetch the collection of identified differences using the getChanges() method.
  • Now traverse the changes, analyze, and then you can set the ComparisonAction of each change accordingly.
    • Set it as Accept to agree with the change.
    • Set it as Reject to deny the change.
  • Call the applyChanges() method to get the modified document with embraced changes.

The following Java code snippet compares two DOCX documents and then shows how to accept and reject identified changes.

Get a Free API License

You can get a free temporary license to use the API without the evaluation limitations to compare multiple Word documents.

Build Your Own Comparison Application

Here are some important links that will help you in building your own document comparison application.

Features | Documentation | API Reference | Running Examples – Source Code | Support | Release Notes

See Also