Process Comments in MS Word document using REST API

comments preview in ms word file

Comments are a great way to share your feedback/input over certain sections/paragraphs within a Word document. Furthermore, in a multi-user environment, it provides an easy and effective way to collaborate and comment on document drafts. Nevertheless, even individual users find the feature handy for adding notes and reminders.

Aspose.Words Cloud is a leading REST API offering features to create as well as manipulate existing MS Word, OpenOffice document formats. It provides complete support for manipulating comments inside an MS word document. This article covers the following features supported by the API

Get comments from DOCX files

You can leverage to read all or specific comments from the document. The GetComments method provides the capabilities to read the user comments from DOCX file. Specified below is a cURL command which you may use to read the comments on the command prompt

cURL command

Please note that in order to read comments, you need to provide the details regarding documents stored on cloud storage, as well as only authorized users, get the permissions to access the APIs. Therefore, you need to first retrieve the JWT authentication token and use it in the command as mentioned above.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=xxxxxxx-1c8e-4ea4-a948-3857547232fa&client_secret=xxxxxx" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accpet: application/json"
curl -X GET "https://api.aspose.cloud/v4.0/words/Volume%201.docx/comments" -H "accept: application/json" -H "Authorization: Bearer <JWT Token>"

Request URL

https://api.aspose.cloud/v4.0/words/Volume%201.docx/comments

C#.NET

In order to read a particular comment from the document, please follow the steps specified below

  • Initialize an instance of WordsApi class
  • Create an object of GetCommentRequest class where we provide the input Volume 1.docx file name and comment index value as input arguments
  • Finally, initialize an object of CommentResponse while passing the value returned from GetComment(..) method.

Get all comments

The API also offers the feature to get all comments from the document (in case we do not want to traverse between individuals). For this requirement, you need to create an instance of GetCommentsRequest, CommentsResponse classes, and call the GetComments(..) method of the WordsApi object.

Insert comments in Word document

The API is also capable of adding/inserting new comment objects in a word document.

cURL command

curl -X POST "https://api.aspose.cloud/v4.0/words/Volume%201.docx/comments" -H "accept: application/json" \
-H "Authorization: Bearer <JWT Token>" \
-H "Content-Type: application/json" \
-d "{\"RangeStart\":{\"Node\":{\"link\":{\"Href\":\"https://api.aspose.cloud/v4.0/words/Volume 1.docx/sections/0/body/tables/0/rows/1/cells/1/paragraphs/1/runs/0\",\"Rel\":\"self\",\"Type\":\"string\",\"Title\":\"Heading 2\"},\"NodeId\":\"0.1.0.1.1.1.1\"},\"Offset\":0},\"RangeEnd\":{\"Node\":{\"link\":{\"Href\":\"https://api.aspose.cloud/v4.0/words/Volume 1.docx/sections/0/body/tables/0/rows/1/cells/1/paragraphs/5/runs/0\",\"Rel\":\"self\",\"Type\":\"string\",\"Title\":\"Heading 2\"},\"NodeId\":\"0.1.0.1.1.1.1\"},\"Offset\":0},\"Author\":\"Nayyer Shahbaz\",\"Initial\":\"MOU\",\"DateTime\":\"2020-12-07T04:10:03.942Z\",\"Text\":\"Comments Inserted using Aspose.Words Cloud API\"}"

C#.NET

Insert comments preview.
Image 1:- preview of newly inserted comment

Update comments in Word file

As well as parsing and addition of comments, the API also supports the feature to update the existing comments inside the word document.

cURL command

curl -X PUT "https://api.aspose.cloud/v4.0/words/Volume%201.docx/comments/1" \
-H "accept: application/json" \
-H "Authorization: Bearer <JWT Token>" \
-H "Content-Type: application/json" \
-d "{\"RangeStart\":{\"Node\":{\"link\":{\"Href\":\"https://api.aspose.cloud/v4.0/words/Volume 1.docx/sections/0/body/tables/0/rows/1/cells/1/paragraphs/1/runs/0\",\"Rel\":\"self\",\"Type\":\"string\",\"Title\":\"Heading 1\"},\"NodeId\":\"0.1.0.1.1.1.1\"},\"Offset\":0},\"RangeEnd\":{\"Node\":{\"link\":{\"Href\":\"https://api.aspose.cloud/v4.0/words/Volume 1.docx/sections/0/body/tables/0/rows/1/cells/1/paragraphs/1/runs/0\",\"Rel\":\"string\",\"Type\":\"string\",\"Title\":\"Heading 1\"},\"NodeId\":\"0.1.0.1.1.1.1\"},\"Offset\":0},\"Author\":\"Nayyer Shahbaz\",\"Initial\":\"Aspose.Words\",\"DateTime\":\"2020-12-07T06:18:31.348Z\",\"Text\":\"Comments updated using Aspose.Words Cloud API\"}"

C#.NET

In order to update comments in word document,

  • Create an instance of WordsApi class
  • Create an object of NodeLink which defines the comment NodeID
  • A new object of DocumentPosition class is created that defines RangeStart and RangeEnd values.
  • Create an object of CommentUpdate which defines, comment Initials, Author name, RangeStart & RangeEnd details by assigning DocumentPosition instance value. and the text to be updated in the comment
Update comments preview
Image 2:- preview of updated comments

The resultant document Volume 1 – updated.docx is also attached.

Delete comments from Word document

You can also remove/delete the comments from the word file using REST cloud API.

cURL command

curl -X DELETE "https://api.aspose.cloud/v4.0/words/Volume%201.docx/comments/0" \
-H "accept: /" \
-H "Authorization: Bearer <JWT Token>"

C#.NET

  • Create an object of WordsApi class
  • Create an instance of DeleteCommentRequest class which takes an input file and index of comment as an argument
  • Finally the DeleteComment(…) method having DeleteCommentRequest object as an argument
Deleted comments preview
Image 3:- preview of deleted comments