How to Add Comments to Paragraphs in Word Documents using Node.js

Cloudmersive
2 min readDec 20, 2023

--

Once we’ve loaded our DOCX files into web viewers in our applications, we can begin to make programmatic edits to those files with a few simple API calls.

Using the below code, we can take advantage of a free API that allows us to insert new comments next to a specific paragraph in a DOCX file. To structure our input request, we’ll need to know our paragraph path, and we’ll need to set various details about our comment, including the author's name & initials, comment text, and more:

{
"InputFileBytes": "string",
"InputFileUrl": "string",
"ParagraphPath": "string",
"CommentToInsert": {
"Path": "string",
"Author": "string",
"AuthorInitials": "string",
"CommentText": "string",
"CommentDate": "2023-12-20T16:19:34.524Z",
"IsTopLevel": true,
"IsReply": true,
"ParentCommentPath": "string",
"Done": true
}
}

To structure our API call, we can begin by installing the SDK. We can do that either by 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 we can add the below code into our file and pass in our input reqeust. We’ll also need to use a free-tier API key to authorize our request, which will allow a limit of 800 API calls per month & no commitments:

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.EditDocumentApi();

var reqConfig = new CloudmersiveConvertApiClient.DocxInsertCommentOnParagraphRequest(); // DocxInsertCommentOnParagraphRequest | Document input request


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

That will input our comment(s), but we’re not quite done yet. The API response will return a temporary editing document URL, and in order to convert that URL into finalized DOCX encoding, we’ll need to call another specialized API:

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.EditDocumentApi();

var reqConfig = new CloudmersiveConvertApiClient.FinishEditingRequest(); // FinishEditingRequest | Cloudmersive Document URL to complete editing on


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

Now we can easily add comments to DOCX files and download updated documents from our Node.js applications.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet