DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

1 1

Compare Two Word documents and get the difference reports in Java

It is a time-consuming job when we need to analyzes two Word documents which look similar. Spire.Doc for Java offers a method of doc1.compare(doc2, "Author") to compare the word document and generate a report in track changes to make the difference clearly. As long as you have two separate Word documents, you can compare them in three lines of codes in Java applications.
Firstly, ensure that you are using Spire.Doc for Java V 3.8.8 or above and please check the sample word document as below:
Sample word

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("Sample.docx");

        //Create a Document instance
        Document doc2 = new Document();
        //Load the second Word document
        doc2.loadFromFile("Sample1.docx");

        //Compare the two Word documents
        doc1.compare(doc2, "E-iceblue");

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

    }
}
Enter fullscreen mode Exit fullscreen mode

After run the project,we will get the Word document as below:
Output

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs