DEV Community

loading...

Compare Two Word documents and get the difference reports in Java

eiceblue profile image E-iceblue Product Family ・1 min read

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");

    }
}

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

Discussion (0)

pic
Editor guide