How to Remove Specific Annotations from a PDF Document using Node.js
The annotations/comments object in a PDF is readily available for us to edit with the right programming library or API to grant us access.
Using the code examples provided further down the page, we can take advantage of two separate & free-to-use APIs which allows to 1) enumerate a list of annotation/comment objects in a PDF and 2) elect to remove only one of those comments/annotations (respectively).
We first need to install the SDK, which we can do by either running this command:
npm install cloudmersive-convert-api-client --save
Or by adding this snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Then, to ensure we can authorize our requests for free, we need to get a free-tier API key from the Cloudmersive website. We can do so by registering a free account; this will allow a limit of 800 API calls per month.
Finally, we can first structure our API call to enumerate a list of annotations/comments from a PDF document. We need to load in our PDF document and use form data for this request:
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 inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editPdfGetAnnotations(inputFile, callback);
Next, we can use the below code to specify which annotations/comments we want to remove from the document. This request will also require our PDF document’s form data, along with the annotation index made available from the initial request:
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 inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.
var annotationIndex = 56; // Number | The 0-based index of the annotation in the document
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editPdfRemoveAnnotationItem(inputFile, annotationIndex, callback);
Now we can rapidly edit the comments/annotations in our PDF documents through our web applications. Easy!