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
- Compare Word Documents using Java
- Get Text of the Changes using Java
- Compare Bookmarks in Word Documents
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:
- Create an instance of the Comparer class
- Provide the source DOCX file path to the constructor
- Add target DOCX file to the comparison
- 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.
// initialize comparer | |
Comparer comparer = new Comparer("C:\\Files\\source.docx"); | |
// add target file | |
comparer.add("C:\\Files\\target.docx"); | |
// compare and save compare results | |
comparer.compare("C:\\Files\\result.docx"); |
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:
- Create an instance of the Comparer class
- Provide the source DOCX file path to the constructor
- Add target DOCX file to the comparison
- Call the Compare() method
- Call the getChanges() method and get the details of changes
- Show the changes
The following code sample shows how to get the text of the changes using Java.
// initialize comparer | |
Comparer comparer = new Comparer("C:\\Files\\source.docx"); | |
// add target file | |
comparer.add("C:\\Files\\target.docx"); | |
// compare | |
final Path resultPath = comparer.compare(); | |
// get changes | |
ChangeInfo[] changes = comparer.getChanges(); | |
System.out.println("Count of changes: " + changes.length); | |
for (ChangeInfo change : changes) { | |
System.out.printf("Change Type: %d, Text: %s%n", change.getType(), change.getText()); | |
} |
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[
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:
- Create an instance of the Comparer class
- Provide the source DOCX file path to the constructor
- Add target DOCX file to the comparison
- Create an instance of the CompareOptions
- Set the CompareBookmarks to true
- 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.
// initialize comparer | |
Comparer comparer = new Comparer("C:\\Files\\source.docx"); | |
// add target file | |
comparer.add("C:\\Files\\target.docx"); | |
// define compare options | |
CompareOptions compareOptions = new CompareOptions(); | |
compareOptions.setCompareBookmarks(true); | |
// compare and save compare results | |
comparer.compare("C:\\Files\\result.docx", compareOptions); |
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.