Compare Word Documents

Perform Text Compare in Word Documents Online

The task to compare text files is very common when incorporating changes into a unified document. Therefore during the review and merge process, the text compare operation is performed and we often use utilities to compare text online. So in this article, we are going to discuss the steps on how to compare word documents and compare text files using Java SDK.

Compare Text API

Aspose.Words Cloud SDK for Java provides a huge array of features enabling you can create, edit and manipulate Word documents within Java application. Now in order to use the SDK, please add the following details to the pom.xml of the maven build project.

<repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <name>Aspose Java API</name>
        <url>https://repository.aspose.cloud/repo/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-words-cloud</artifactId>
        <version>22.5.0</version>
    </dependency>
</dependencies>

Once the SDK is installed, please register a free account over Aspose.Cloud dashboard using GitHub or Google account or simply Sign Up and obtain your Client Credentials.

Compare Word Documents in Java

In this section, we are going to discuss the details of how to compare word documents using Java code snippets.

  • The first step is to create an instance of WordsApi using client credentials
  • Secondly, upload the input and modified Word documents to cloud storage while passing the UploadFileRequest object to uploadFile(…) method of WordsApi
  • Thirdly, create a CompareData object and pass the second document as an argument to the setComparingWithDocument(…) method
  • Now create an object of CompareDocumentRequest class where we pass input Word file, CompareData object, and resultant word document as arguments
  • Finally, compare text files using compareDocument(…) method and save the output in cloud storage
Compare Word Document preview

Preview of Compare Word Document operation

The sample files used in the above example can be downloaded from the following links

Text Compare using cURL Commands

We can also access Aspose.Words Cloud via cURL commands and compare text files. So as a pre-requisite, please execute the following command to create a JWT access token based on Client ID and Client Secret details.

curl -v "https://api.aspose.cloud/connect/token" \
 -X POST \
 -d "grant_type=client_credentials&client_id=a41d01ef-dfd5-4e02-ad29-bd85fe41e3e4&client_secret=d87269aade6a46cdc295b711e26809af" \
 -H "Content-Type: application/x-www-form-urlencoded" \
 -H "Accept: application/json"

Once we have the JWT Token, please execute the following command to compare text online and save the resultant file in cloud storage.

curl -v -X PUT "https://api.aspose.cloud/v4.0/words/input-sample.docx/compareDocument?destFileName=Comparison.docx" \
-H  "accept: application/json" \
-H  "Authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "{\"ComparingWithDocument\":\"input-sample-updated.docx\",\"Author\":\"Nayyer Shahbaz\",\"DateTime\":\"2022-07-21T07:54:06.768Z\",\"CompareOptions\":{\"IgnoreCaseChanges\":true,\"IgnoreTables\":true,\"IgnoreFields\":true,\"IgnoreFootnotes\":true,\"IgnoreComments\":true,\"IgnoreTextboxes\":true,\"IgnoreFormatting\":true,\"IgnoreHeadersAndFooters\":true,\"Target\":\"Current\",\"AcceptAllRevisionsBeforeComparison\":true},\"ResultDocumentFormat\":\"docx\"}"

Conclusion

This article has explained the steps to compare documents using Java as well as cURL commands. You may consider exploring the API capabilities through the swagger interface. Furthermore, the complete source code of SDK can be downloaded from GitHub. In case you have any further queries or you face any difficulty, please visit the free support forum.

We highly recommend going through the following blogs