MS Word provides a built-in "Compare" feature that helps users quickly find all the differences between two Word documents. In this article, I will share how to compare two Word documents programmatically using Free Spire.Doc for Java.
Import Jar Dependency
Method 1: Download the free library and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
Sample Code
import com.spire.doc.Document;
public class CompareWordDocuments {
public static void main(String[] args) throws Exception {
//Create a Document instance
Document doc1 = new Document();
//Load the first Word document
doc1.loadFromFile("Doc1.docx");
//Create a Document instance
Document doc2 = new Document();
//Load the second Word document
doc2.loadFromFile("Doc2.docx");
//Compare the two Word documents
doc1.compare(doc2, "Author");
//Save the result to file
doc1.saveToFile("Compare.docx");
}
}