· 

Java - Compare Two Word Documents and Find the Differences

When you want to see the changes in current Word document different from original one, you would compare them in Microsoft Word to find the difference. In this article, I am going to introduce how to programmatically compare the changed version against the original and find out the difference by using Spire.Doc for Java.

Below is a screenshot of the two versions of the sample document.

Installing Spire.Doc.jar

If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application.

<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</artifactId>
        <
version>4.6.2</version>
    </
dependency>
</
dependencies>

Using the code

import com.spire.doc.Document;

public class WordDocuments {

   
public static void main(String[] args) {

       
//Create a Document instance to load document 1
       
Document doc1 = new Document();
        doc1.loadFromFile(
"C:\\Users\\Administrator\\Desktop\\Doc1.docx");

       
//Create a Document instance to load document 2
       
Document doc2 = new Document();
        doc2.loadFromFile(
"C:\\Users\\Administrator\\Desktop\\Doc2.docx");

       
//Compare the two documents and find differences based on document 1
       
doc1.compare(doc2,"Jacky");

       
//Save the result to file
       
doc1.saveToFile("output/Result.docx"
);
    }
}

Output

Write a comment

Comments: 0