Compare Word Documents using REST API in Node.js

As a Node.js developer, you can easily compare two or more Word documents for similarities and differences programmatically on the cloud. It can help you track changes in different versions of the same Word document or different documents. In this article, you will learn how to compare two or more Word documents using a REST API in Node.js.

The following topics shall be covered in this article:

REST API and Node.js SDK to Compare Word Documents

For comparing two or more DOCX files, we will be using the Node.js SDK of GroupDocs.Comparison Cloud API. It allows you to compare ‎two or more documents and find the differences in a resultant file. You can easily integrate the SDK ‎into your existing Node.js ‎applications to compare documents, spreadsheets, ‎presentations, ‎Visio diagrams, emails, and files of many other supported formats.

You can install GroupDocs.Comparison Cloud to your Node.js application using the following command in the console:

npm install groupdocs-comparison-cloud

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

Compare Two Word Files using a REST API in Node.js

You can compare your Word documents programmatically by following the simple steps given below:

  1. Upload the DOCX files to the Cloud
  2. Compare Word Files using Node.js
  3. Download the resultant DOCX file

Upload the DOCX Files

Firstly, upload the source and target DOCX files to the Cloud using the following code sample:

As a result, the uploaded DOCX files will be available in the files section of your dashboard on the cloud.

Compare Word Files using Node.js

You can compare two Word documents programmatically by following the steps given below:

  • Firstly, create an instance of the CompareApi.
  • Next, create an instance of the FileInfo and set the source input DOCX file path.
  • Then, create another instance of the FileInfo and set the target input DOCX file path.
  • After that, create an instance of the ComparisonOptions and assign source and target files.
  • Then, set the output file path.
  • Next, create the ComparisonsRequest with ComparisonOptions.
  • Finally, get results by calling the CompareApi.comparisons() method with ComparisonsRequest.

The following code sample shows how to compare two Word files using a REST API in Node.js.

Source and Target files.

Source and Target files.

Compare Two Word Files using a REST API in Node.js

Compare Two Word Files using a REST API in Node.js

Download the Resultant File

The above code sample will save the differences in a newly created DOCX file on the cloud. You can download it using the following code sample:

Compare Multiple DOCX Files using Node.js

You can compare multiple Word documents programmatically by following the steps given below:

  • Firstly, create an instance of the CompareApi.
  • Next, create an instance of the FileInfo and set the source input DOCX file path.
  • Then, create another instance of the FileInfo and set the target input DOCX file path.
  • Repeat above steps to add multiple target files.
  • After that, create an instance of the ComparisonOptions and assign source and target files.
  • Then, set the output file path.
  • Next, create the ComparisonsRequest with ComparisonOptions.
  • Finally, get results by calling the CompareApi.comparisons() method with ComparisonsRequest

The following code sample shows how to compare multiple Word files using a REST API in Node.js.

Get List of Changes using REST API in Node.js

You can get a complete list of found differences after comparing Word documents programmatically by following the steps given below:

  • Firstly, create an instance of the CompareApi
  • Next, create an instance of the FileInfo and set the source input DOCX file path
  • Then, create another instance of the FileInfo and set the target input DOCX file path
  • After that, create an instance of the ComparisonOptions and assign source and target files
  • Then, set the output file path
  • Next, create the PostChangesRequest with ComparisonOptions
  • After that, get results by calling the CompareApi.postChanges() method with PostChangesRequest
  • Finally, show all changes one by one

The following code sample shows how to get a list of changes using a REST API in Node.js.

Get List of Changes using REST API in Node.js

Get List of Changes using REST API in Node.js

Customize Comparison Results using Node.js

You can easily customize the style of changes programmatically by following the steps given below:

  • Firstly, create an instance of the CompareApi.
  • Next, create an instance of the FileInfo and set the source input DOCX file path.
  • Then, create another instance of the FileInfo and set the target input DOCX file path.
  • After that, create an instance of the Settings and set various compare settings such as sensitivityOfComparison.
  • Next, create instances of the ItemsStyle for the insertedItemsStyle, deletedItemsStyle, and changedItemsStyle.
  • Then, set various properties for each ItemsStyle such as highlightColor, fontColor, bold, italic, etc.
  • After that, create an instance of the ComparisonOptions and assign source and target files.
  • Then, set the output file path.
  • Next, assign settings to ComparisonOptions
  • After that, create the ComparisonsRequest with ComparisonOptions.
  • Finally, get results by calling the CompareApi.comparisons() method with ComparisonsRequest

The following code sample shows how to customize comparison results using a REST API in Node.js.

Try Online

Please try the following free online DOCX comparison tool, which is developed using the above API. https://products.groupdocs.app/comparison/docx

Conclusion

In this article, you have learned how to compare Word documents using a REST API on the cloud. Moreover, you have seen how to compare multiple DOCX files programmatically. This article also explained how to programmatically upload a DOCX file to the cloud and then download the resultant file from the Cloud. Besides, you can learn more about GroupDocs.Comparison Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also