Compare PDF Files using REST API in NodeJs

You can compare two or more PDF documents programmatically on the Cloud. The comparison lets you identify similarities and differences in documents. In this article, you will learn how to compare PDF files using a REST API in Node.js.

The following topics shall be covered in this article:

Document Comparison REST API and Node.js SDK

I will be using the Node.js SDK of GroupDocs.Comparison Cloud API for comparing PDF documents. It allows you to compare ‎two or more documents and find the differences. As a result, it creates a resultant file containing the differences. It also enables you to ‎accept or reject the ‎retrieved changes. You can easily integrate it ‎into your existing Node.js ‎applications to compare documents, spreadsheets, ‎presentations, ‎Visio diagrams, emails, and files of many other formats. It also provides .NET, Java, PHP, Python, and Ruby SDKs as its document comparison family members for the Cloud API.

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

npm install groupdocs-comparison-cloud --save

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 PDF Files using a REST API in Node.js

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

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

Upload the PDF Files

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

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

Compare PDF Files using Node.js

You can compare two PDF documents programmatically by following the steps mentioned below:

  • Create an instance of CompareApi
  • Set the source .pdf file
  • Set the target .pdf file
  • Define ComparisonOptions
  • Assign source and target files
  • Set the output file path
  • Create ComparisonsRequest
  • Get results by calling the CompareApi.comparisons() method

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

Compare PDF Files using Node.js

Compare PDF Files using Node.js

The resultant file also contains a summary page at the end of the document as shown below:

Compare PDF Files using REST API in Node.js

Download the Resultant File

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

Compare Multiple PDF Files using Node.js

You can compare multiple PDF documents programmatically by following the simple steps mentioned below:

  • Create an instance of CompareApi
  • Set the source .pdf file
  • Set multiple target .pdf files
  • Create ComparisonOptions instance
  • Assign source and target files
  • Set the output file path
  • Create ComparisonsRequest
  • Get results by calling the CompareApi.comparisons() method

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

Customize Comparison Results using Node.js

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

  • Create an instance of CompareApi
  • Set the source .pdf file
  • Set the target .pdf file
  • Create Settings instance
  • Set compare sensitivity
  • Customize Items Style
  • Create ComparisonOptions instance
  • Assign source and target files
  • Set the output file path
  • Assign settings
  • Create ComparisonsRequest
  • Get results by calling the CompareApi.comparisons() method

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

Get List of Changes using Node.js

You can get a complete list of found differences after comparing PDF documents programmatically by following the simple steps mentioned below:

  • Create an instance of CompareApi
  • Set the source .pdf file
  • Set the target .pdf file
  • Define ComparisonOptions
  • Create ComparisonsRequest
  • Get results by calling the CompareApi.comparisons() method
  • Show all changes one by one

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

Get List of Changes using Node.js

Get List of Changes using Node.js

Compare and Save with Password & Metadata using Node.js {#Compare-and-Save-with-Password-&-Metadata-using-Nodejs}

Please follow the steps mentioned below to password-protect the resultant file and save it with metadata:

  • Create an instance of CompareApi
  • Set the source .pdf file
  • Set the target .pdf file
  • Create Settings instance
  • Set Metadata and Password
  • Create ComparisonOptions instance
  • Assign source and target files
  • Set the output file path
  • Assign settings
  • Create ComparisonsRequest
  • Get results by calling the CompareApi.comparisons() method

The following code example shows how to save the resultant file with password and metadata using a REST API in Node.js.

Try Online

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

Conclusion

In this article, you have learned how to compare PDF documents on the cloud. You have also learned how to compare multiple PDF files, customize changes style and get a list of changes. Moreover, you have learned how to programmatically upload multiple PDF files to the cloud and then download the resultant file from the cloud. 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