Compare MS Word files

Microsoft Word supports the “Compare” option enabling users to compare word documents and identify the newly added changes. This feature is useful as you can perform a Text compare operation in word documents side by side. However, a programmatic solution of text difference checker can be way faster and more efficient and can easily perform bulk text diff operations. In this article, we are going to perform a Text compare operation using REST-API so that we can find the differences.

Text Compare API

Aspose.Words Cloud SDK for .NET enables you to create, manipulate and transform leading MS Word formats (DOC, DOCX, DOCM etc) to other supported formats. At the same time, it also provides the capability to compare Word documents and highlight the difference. Now in order to use the SDK, please install it from NuGet library using the following command on the package manager console:

Install-Package Aspose.Words-Cloud

For more information, you may consider visiting the following link for more information on How to install Aspose.Cloud SDKs. Now after the installation, we also need to create a free account by visiting Aspose.Cloud dashboard. Simply Sign up with your GitHub or Google account or click the Create a new Account button. The Client Credentials displayed on the page will be used in subsequent sections.

Compare Text online using C#

Given below are the steps and code snippet explaining the details on how to compare two word documents using C# .NET code snippet.

  • First, we need to define ClientID and ClientSecret information in code
  • Secondly, create an object of the Configuration class by providing ClientID and ClientSecret details as arguments
  • Thirdly, create an object of WordsApi class and pass the Configuration instance as an argument
  • Now create an instance of CompareData class
  • In order to specifically identify the name of the person as identified in the resultant file, use the Author property of CompareData class
  • The ComparingWithDocument property of CompareData class is used to specify the target document to compare the content
  • Now create an instance of CompareDocumentRequest class which takes the input/source file, CompareData object, and resultant file name as arguments
  • Finally, call the CompareDocument(..) method to initiate the comparison operation

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

Compare Word Documents using cURL Commands

In this section, we are going to learn the steps on how to compare Word documents using cURL commands. As our APIs are only accessible to authorized personnel only, so 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=c235e685-1aab-4cda-a95b-54afd63eb87f&client_secret=b8da4ee37494f2ef8da3c727f3a0acb9" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Given below cURL command shows how to perform file compare online using the cURL command. It accesses CompareDocument method and the result of this operation is written to the output document that contains track changes as a number of edit and format revisions. Please note that if you do not provide the output document details, the revisions are saved to the original document.

curl -X PUT "https://api.aspose.cloud/v4.0/words/file-sample.docx/compareDocument?destFileName=ComparisonResult.docx" \
-H  "accept: application/json" 
-H  "Authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "{\"ComparingWithDocument\":\"file-sample-updated.docx\",\"Author\":\"Nayyer\",\"DateTime\":\"2021-04-27T16:50:55.869Z\",\"CompareOptions\":{\"IgnoreCaseChanges\":true,\"IgnoreTables\":true,\"IgnoreFields\":true,\"IgnoreFootnotes\":true,\"IgnoreComments\":true,\"IgnoreTextboxes\":true,\"IgnoreFormatting\":true,\"IgnoreHeadersAndFooters\":true,\"Target\":\"Current\"}}"

Once the command is executed, the information which was deleted is separately specified and the newly added content is highlighted in yellow color.

Original and Modified document preview

Image 1:- Preview of Original Document and Modified document.

Preview of Text Compare

Image 2:- Preview of Text Compare.

Conclusion

In this article, we have explained the steps to compare word documents using Aspose.Words Cloud API. Please note that the APIs can also be accessed through the swagger interface. Also, our Cloud SDKs are open source and their complete source code can be downloaded from GitHub.

We also recommend visiting Product Documentation for further details on other exciting features being offered by API. Your feedback is very important to us. So, please feel free to contact us using our Support Forums.

See Also

You may consider visiting the following links for information on