Annotations are comments, popups, and various other graphical objects in the document providing additional information. You can easily add various types of annotations to your documents programmatically on the cloud. You can also extract or remove all the annotations from documents using Node.js. In this article, you will learn how to extract or remove annotations from PDF documents using a REST API in Node.js.
The following topics shall be covered in this article:
- Document Annotation REST API and Node.js SDK
- Extract or Remove Annotations from PDF Files using a REST API
Document Annotation REST API and Node.js SDK
I will be using the Node.js SDK of GroupDocs.Annotation Cloud API for extracting or removing the annotations from PDF documents. It allows you to build document annotator tools in Node.js. Such tools can be used to add, edit, or delete annotations, watermark overlays, text replacements, redactions, sticky notes, and text markups to all popular document formats such as PDF, Word, Excel, PowerPoint, Outlook, and image formats. It also provides .NET, Java, PHP, Ruby, and Python SDKs as its document annotation family members for the Cloud API.
You can install GroupDocs.Annotation Cloud to your Node.js application using the following command in the console:
npm install groupdocs-annotation-cloud
Please get your Client ID and Client Secret from the dashboard before you start following the steps and available code examples. Once you have your ID and secret, add in the code as shown below:
global.clientId = "659fe7da-715b-4744-a0f7-cf469a392b73";
global.clientSecret = "b377c36cfa28fa69960ebac6b6e36421";
global.myStorage = "";
const configuration = new groupdocs_annotation_cloud.Configuration(clientId, clientSecret);
configuration.apiBaseUrl = "https://api.groupdocs.cloud";
Extract or Remove Annotations from PDF Files using a REST API in Node.js
You can extract or delete all the annotations from the PDF documents by following the simple steps mentioned below:
- Upload the PDF file to the Cloud
- Extract Annotations from PDF Files in Node.js
- Remove Annotations from PDF Files in Node.js
- Download the updated file
Upload the Document
Firstly, upload the PDF file to the Cloud using the code example given below:
// api initialization
var fileApi = new groupdocs_annotation_cloud.FileApi(configuration);
// input file
var resourcesFolder = 'C:\\Files\\sample.pdf';
// read file
fs.readFile(resourcesFolder, (err, fileStream) => {
// create upload file request
var request = new groupdocs_annotation_cloud.UploadFileRequest("sample.pdf", fileStream, myStorage);
// upload file
fileApi.uploadFile(request)
});
As a result, the uploaded PDF file will be available in the files section of your dashboard on the cloud.
Extract Annotations from PDF Files in Node.js
You can extract all the annotations from PDF documents programmatically by following the steps mentioned below:
- Create an instance of the AnnotateApi
- Create an instance of the FileInfo
- Set the input file path
- Create a request by calling the ExtractRequest method with FileInfo object
- Get results by calling the AnnotateApi.extract() method with ExtractRequest object
The following code snippet shows how to extract annotations from the PDF document using a REST API in Node.js.
// initialize api
let annotateApi = groupdocs_annotation_cloud.AnnotateApi.fromKeys(clientId, clientSecret);
// input file
let fileInfo = new groupdocs_annotation_cloud.FileInfo();
fileInfo.filePath = "sample.pdf";
// create extract request
let request = new groupdocs_annotation_cloud.ExtractRequest(fileInfo);
// extract
let result = await annotateApi.extract(request);
// show results
console.log("GetAnnotations: annotations count = " + result.length);
result.forEach(element => console.log("Annotation ID:" + element.id + " Element Text: " + element.text));
Remove Annotations from PDF Documents in Node.js
You can delete the annotations from PDF documents programmatically by following the steps mentioned below:
- Create an instance of the AnnotateApi
- Create an instance of the FileInfo
- Set the input file path
- Create an instance of the RemoveOptions
- Set the FileInfo to RemoveOptions
- Provide annotation IDs to remove
- Set the output file path
- Create a request by calling the RemoveAnnotationsRequest method with RemoveOptions object
- Get results by calling the AnnotateApi.removeAnnotations() method
The following code example shows how to remove annotations from the PDF document using a REST API in Node.js. You can get annotation IDs using the extract() method with ExtractRequest as described earlier.
// api initialization
let annotateApi = groupdocs_annotation_cloud.AnnotateApi.fromKeys(clientId, clientSecret);
// input file path
let fileInfo = new groupdocs_annotation_cloud.FileInfo();
fileInfo.filePath = "sample.pdf";
// define remove options
let options = new groupdocs_annotation_cloud.RemoveOptions();
options.fileInfo = fileInfo;
options.annotationIds = [0, 1, 2, 3];
options.outputPath = "Output/output.pdf";
// create remove annotation request
let request = new groupdocs_annotation_cloud.RemoveAnnotationsRequest(options);
// Remove annotations
let result = await annotateApi.removeAnnotations(request);
console.log("DeleteAnnotations: annotations delete: " + result.href);
Download the Output File
The above code sample will save the output file after removing annotations on the cloud. You can download it using the following code sample:
// construct FileApi
var fileApi = new groupdocs_annotation_cloud.FileApi(configuration);
// create download file request
let request = new groupdocs_annotation_cloud.DownloadFileRequest("Output/output.pdf", myStorage);
let response = await fileApi.downloadFile(request);
// save file in working directory
fs.writeFile("C:\\Files\\output.pdf", response, "binary", function (err) { });
console.log(response);
Try Online
Please try the following free online PDF annotation tool, which is developed using the above API. https://products.groupdocs.app/annotation/pdf
Conclusion
In this article, you have learned how to extract or remove annotations from PDF documents on the cloud using Node.js. You have also learned how to programmatically upload the PDF file on the cloud and then download the updated file from the cloud. You can learn even more about GroupDocs.Annotation 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.