Document comparison is the process of checking new versions of a document against previous copies in order to identify changes made by different contributors. These differences may include additions or omissions of words, sentences or paragraphs, and formatting adjustments. This article demonstrates how to compare two Word documents in Java using Spire.Doc for Java.
- Compare Two Documents and Save Result in a Third Word Document
- Compare Two Documents and Return Insertions and Deletions in Lists
Below is a screenshot of the two Word documents that’ll be compared.
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<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>12.9.4</version> </dependency> </dependencies>
Compare Two Documents and Save Result in a Third Word Document
Saving the comparison result in a separate Word document allows users to see all the changes made to the original document, including insertions, deletions as well as modifications on formatting. The following are the steps to compare two documents and save the result in a third Word document using Spire.Doc for Java.
- Load two Word documents separately while initialing the Document objects.
- Compare these two documents using Document.compare() method.
- Save the result in a third Word document using Document.saveToFile() method.
- Java
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(); } }
Compare Two Documents and Return Insertions and Deletions in Lists
Sometimes, we may only care about the insertions and deletions instead of the whole differences. The following are the steps to get insertions and deletions in two separate lists.
- Load two Word documents separately while initialing the Document objects.
- Compare two documents using Document.compare() method.
- Get the revisions using the constructor function of the DifferRevisions class.
- Get a list of insertions using DifferRevisions.getInsertRevisions() method.
- Get a list of deletions using DifferRevisions.getDeleteRevisions() method.
- Loop through the elements in the two lists to get the specific insertion and deletion.
- Java
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 insertRevisionsList = differRevisions.getInsertRevisions(); //Return the deletion revisions in a list List 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()); } } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.