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:

global.clientId = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
global.clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
global.myStorage = "";
const config = new groupdocs_comparison_cloud.Configuration(clientId, clientSecret);
config.apiBaseUrl = "https://api.groupdocs.cloud";

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:

// construct FileApi
let fileApi = groupdocs_comparison_cloud.FileApi.fromConfig(config);
let resourcesFolder = 'C:\\Files\\comparison\\uploads\\';
fs.readdir(resourcesFolder, (err, files) => {
files.forEach(file => {
console.log(file);
fs.readFile(resourcesFolder + file, (err, fileStream) => {
let request = new groupdocs_comparison_cloud.UploadFileRequest(file, fileStream, myStorage);
fileApi.uploadFile(request);
});
});
});

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.

// initialize api
let compareApi = groupdocs_comparison_cloud.CompareApi.fromKeys(clientId, clientSecret);
// source file
let source = new groupdocs_comparison_cloud.FileInfo();
source.filePath = "source.docx";
// target file
let target = new groupdocs_comparison_cloud.FileInfo();
target.filePath = "target.docx";
// define compare options
let options = new groupdocs_comparison_cloud.ComparisonOptions();
options.sourceFile = source;
options.targetFiles = [target];
options.outputPath = "compared/result_compareOptions.docx";
// create comparisons request
let request = new groupdocs_comparison_cloud.ComparisonsRequest(options);
// compare
let response = await compareApi.comparisons(request);
console.log("Output file link: " + response.href);
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:

// construct FileApi
let fileApi = groupdocs_comparison_cloud.FileApi.fromConfig(config);
// create download file request
let request = new groupdocs_comparison_cloud.DownloadFileRequest("compared/result_compareOptions.docx", myStorage);
// download file
let response = await fileApi.downloadFile(request);
// save in the working directory
fs.writeFile("C:\\Files\\comparison\\result_compareOptions.docx", response, "binary", function (err) { });

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.

// initialize api
let compareApi = groupdocs_comparison_cloud.CompareApi.fromKeys(clientId, clientSecret);
// source file
let source = new groupdocs_comparison_cloud.FileInfo();
source.filePath = "source.docx";
// target file 1
let target1 = new groupdocs_comparison_cloud.FileInfo();
target1.filePath = "target1.docx";
// target file 2
let target2 = new groupdocs_comparison_cloud.FileInfo();
target2.filePath = "target2.docx";
// define compare options
let options = new groupdocs_comparison_cloud.ComparisonOptions();
options.sourceFile = source;
options.targetFiles = [target1, target2];
options.outputPath = "compared/result_Multiple.docx";
// create comparisons request
let request = new groupdocs_comparison_cloud.ComparisonsRequest(options);
// compare
let response = await compareApi.comparisons(request);
console.log("Output file link: " + response.href);

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.

// initialize api
let compareApi = groupdocs_comparison_cloud.CompareApi.fromKeys(clientId, clientSecret);
// source file
let source = new groupdocs_comparison_cloud.FileInfo();
source.filePath = "source.docx";
// target file
let target = new groupdocs_comparison_cloud.FileInfo();
target.filePath = "target.docx";
// define compare options
let options = new groupdocs_comparison_cloud.ComparisonOptions();
options.sourceFile = source;
options.targetFiles = [target];
options.outputPath = "compared/result.docx";
// create post changes request
let request = new groupdocs_comparison_cloud.PostChangesRequest(options);
// post changes
let changes = await compareApi.postChanges(request);
console.log("Changes count: " + changes.length);
changes.forEach(change => {
console.log(change.id + 1 +"- Target Text: " + change.targetText + ", Text: " + change.text + ", Type: " + change.type);
});
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.

// initialize api
let compareApi = groupdocs_comparison_cloud.CompareApi.fromKeys(clientId, clientSecret);
// source file
let source = new groupdocs_comparison_cloud.FileInfo();
source.filePath = "source.docx";
// target file
let target = new groupdocs_comparison_cloud.FileInfo();
target.filePath = "target.docx";
// define compare settings
let settings = new groupdocs_comparison_cloud.Settings();
// compare sensitivity
settings.sensitivityOfComparison = 100;
// customize changes style for inserted items
settings.insertedItemsStyle = new groupdocs_comparison_cloud.ItemsStyle();
settings.insertedItemsStyle.highlightColor = "14297642";
settings.insertedItemsStyle.fontColor = "16711680";
settings.insertedItemsStyle.underline = true;
// customize changes style for deleted items
settings.deletedItemsStyle = new groupdocs_comparison_cloud.ItemsStyle();
settings.deletedItemsStyle.fontColor = "14166746";
settings.deletedItemsStyle.bold = true;
// customize changes style for changed items
settings.changedItemsStyle = new groupdocs_comparison_cloud.ItemsStyle();
settings.changedItemsStyle.fontColor = "14320170";
settings.changedItemsStyle.italic = true;
// define compare options
let options = new groupdocs_comparison_cloud.ComparisonOptions();
options.sourceFile = source;
options.targetFiles = [target];
options.outputPath = "compared/result_compareOptions.docx";
options.settings = settings;
// create comparisons request
let request = new groupdocs_comparison_cloud.ComparisonsRequest(options);
// compare
let response = await compareApi.comparisons(request);
console.log("Output file link: " + response.href);

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