DEV Community

Alexis
Alexis

Posted on

Java - How to Compare Two Word Documents to Get Differences

We often get two versions of a document at work, for example, the original version and the revised version of a contract, a quotation, or a terms of use document. It’s not uncommon that we need to compare the two similar versions and find the differences between them. In this article, I am going to introduce how to compare two Word documents and get differences in Java by using Spire.Doc for Java.

  • Compare Two Documents and Save Result in a Word Document
  • Compare Two Documents and Return Insertions and Deletions in Lists

The following is a screenshot of the two Word documents that I am going to compare.

TwoDocuments

Add Spire.Doc.jar as Dependency

If you are working on a maven project, you can include the dependency in pom.xml file using this:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>10.6.6</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

If you are not using maven, then you can find the required jar files from the zip file available in this location. Include all the jar files into the application lib folder to run the sample code given in this tutorial.

Compare Two Documents and Save Result in a Word Document

Saving the comparison result in a Word document allows us to see all the changes made to the original document, including insertions, deletions as well as format modifications. The following are the steps to compare two documents and save result in a Word document using Spire.Doc for Java.

  • Load two Word documents separately while initialing the Document object.
  • Compare two documents using Document.compare() method.
  • Save the result in a third Word document using Document.saveToFile() method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class CompareDocuments {

    public static void main(String[] args) {

        //Load one Word document
        Document doc1 = new Document("C:\\Users\\Administrator\\Desktop\\original.docx");

        //Load the other Word document
        Document doc2 = new Document("C:\\Users\\Administrator\\Desktop\\revised.docx");

        //Compare two documents
        doc1.compare(doc2, "John");

        //Save the differences in a third document
        doc1.saveToFile("Differences.docx", FileFormat.Docx_2013);
        doc1.dispose();
    }
}
Enter fullscreen mode Exit fullscreen mode

SaveResultInWord

Compare Two Documents and Return Insertions and Deletions in Lists

Sometimes, you may only care about the insertions and deletions instead of the whole differences. The following are the steps to get insertions and deletions using Spire.Doc for Java.

  • Load two Word documents separately while initialing the Document object.
  • Compare two documents using Document.compare() method.
  • Get the revisions using constructor function of the DifferRevisions class.
  • Get a list of insertions through DifferRevisions.getInsertRevisions() method.
  • Get a list of deletions through DifferRevisions.getDeleteRevisions() method.
  • Loop through the element in the two lists to get the specific insertion and deletion.
import com.spire.doc.DifferRevisions;
import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.fields.TextRange;
import com.spire.ms.System.Collections.Generic.List;

public class CompareReturnResultsInLists {

    public static void main(String[] args) {

        //Load one Word document
        Document doc1 = new Document("C:\\Users\\Administrator\\Desktop\\original.docx");

        //Load the other Word document
        Document doc2 = new Document("C:\\Users\\Administrator\\Desktop\\revised.docx");

        //Compare the two Word documents
        doc1.compare(doc2, "Author");

        //Get the revisions
        DifferRevisions differRevisions = new DifferRevisions(doc1);

        //Return the insertion revisions in a list
        List<DocumentObject> insertRevisionsList = differRevisions.getInsertRevisions();

        //Return the deletion revisions in a list
        List<DocumentObject>  deleteRevisionsList = differRevisions.getDeleteRevisions();

        //Create two int variables
        int m = 0;
        int n = 0;

        //Loop through the insertion revision list
        for (int i = 0; i < insertRevisionsList.size(); i++)
        {
            if (insertRevisionsList.get(i) instanceof TextRange)
            {
                m += 1;
                //Get the specific revision and get its content
                TextRange textRange = (TextRange)insertRevisionsList.get(i) ;
                System.out.println("Insertion #" + m + ":" + textRange.getText());
            }
        }
        System.out.println("============================================");
        //Loop through the deletion revision list
        for (int i = 0; i < deleteRevisionsList.size(); i++)
        {
            if (deleteRevisionsList.get(i) instanceof TextRange)
            {
                n += 1;
                //Get the specific revision and get its content
                TextRange textRange = (TextRange) deleteRevisionsList.get(i) ;
                System.out.println("Deletion #" + n + ":" + textRange.getText());
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

GetResultInList

Top comments (0)