Compare MS Word documents using C# REST API

Compare MS Word files

Since the release of Microsoft Word 2010 and 2007, the “Compare” option has been introduced in MS Word which allows users to compare two versions of the same document to identify the newly added changes by authors. This feature is very useful when someone makes changes in a copy of your word document without enabling the Track Changes option, so it gives you the leverage to compare word documents side by side. However, when you are programmatically creating as well as manipulating MS Word documents (DOCX, DOC, etc), it gets difficult to manually compare the files using MS Word application. So in order to facilitate complete automation, we recommend using REST-based API to compare MS Word documents and find the occurring differences.

Word processing REST API

Aspose.Words Cloud SDK for .NET enables you to create, manipulate and transform leading MS Word formats (DOC, DOCX, DOCM, RTF, DOTX, DOTM, DOT, ODT, OTT, etc) to other supported formats with few code lines. No software download or installation required. All the processing using REST APIs in the cloud and no MS Office automation is required. Also, save the documents in cloud storage for all the processing.

Compare DOCX files using cURL commands

Aspose.Words Cloud SDK for .NET is a programming SDK developed on top of Aspose.Words Cloud REST APIs. Our Cloud REST APIs can be accessed from any platform and the easiest way is to use cURL commands. Since our APIs are only accessible to authorized personnel only, so the first step is to register an account over Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up. Otherwise, click on the Create a new Account button and provide the required information.

Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

The following command shows how 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 we can compare two MS Word files (DOCX format) 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 Document with Modifications
Image 2:- Preview of Document highlighting modifications.

Compare MS Word files in C# REST API

In order to accomplish the requirement to compare MS Word files in C# language, we need to use Aspose.Words Cloud SDK for .NET. So the first step is to install the programming SDK in Visual Studio. Aspose.Words Cloud SDK for .NET can be installed from NuGet library using the following command on the package manager console

Install-Package Aspose.Words-Cloud

You may also consider visiting the following link for more information on How to install Aspose.Cloud SDKs.

Given below are the steps and code snippet to compare two DOCX files using C#.

  • First, we need to define ClientID and ClientSecret information in code
  • Secondly, create an object of 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 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 download from the following links

Conclusion

The main focus in the above article has been on the explanation of steps to accomplish MS Word file comparison using Aspose.Words Cloud API. Our Cloud APIs can also be accessed through the swagger interface. Furthermore, 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 following links for information on