How to Add PDF Annotations in Node.js

Cloudmersive
2 min readSep 28

--

With the right APIs, we can programmatically make changes to comments/annotations in a PDF document while expending minimal effort writing code.

Using the below Node.js code examples, we can take advantage of a specialized API which allows us to add brand new annotations or comments into a PDF document. We can specify the title of our annotation, the annotation type (text, line, strikeout, etc.), the subject, and more.

To authorize our requests, we’ll just need a free-tier API key, which we can get by registering a free account on the Cloudmersive website. This allows up to 800 API calls per month with no commitment.

We can begin by installing the SDK, either by running the following command:

npm install cloudmersive-convert-api-client --save

Or by adding the following snippet to our package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

Finally, we can use the below code to structure our API call:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.EditPdfApi();

var request = new CloudmersiveConvertApiClient.AddPdfAnnotationRequest(); // AddPdfAnnotationRequest |


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editPdfAddAnnotations(request, callback);

And our request objects for each individual comment/annotation can look something like this:

{
"InputFileBytes": "string",
"AnnotationsToAdd": [
{
"Title": "string",
"AnnotationType": "string",
"PageNumber": 0,
"AnnotationIndex": 0,
"Subject": "string",
"TextContents": "string",
"CreationDate": "2023-09-28T13:37:26.726Z",
"ModifiedDate": "2023-09-28T13:37:26.726Z",
"LeftX": 0,
"TopY": 0,
"Width": 0,
"Height": 0
}
]
}

That’s all there is to it — no more code required.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.