Document Solutions for Word
Features / Comments
In This Topic
    Comments
    In This Topic

    A comment is a note or a type of annotation which is usually added to a document when a user wants to add remarks, notes, reminder, or a feedback to it. For instance, when multiple users work on the same document, it becomes easy for them to leave a note, remarks or feedback for each other’s reference.

    In DsWord, comments are represented by the Comment class. It allows you to modify the content and other properties of the comments, such as, author, date of the comment, etc. DsWord allows you to add or insert comments in a Word document using Add or Insert method of the CommentCollection class respectively. In addition, it lets you add and delete the comment replies as well. You can use Reply method of the Comment class to add a new comment as a reply into the comments collection and the Delete method to delete it.

    Comments in a Word document

    Add Comments

    To add comments in a document:

    1. Access the content object on which you want to add a comment. For example, access the first paragraph.
    2. Access the comment collection using Comments property of the RangeBase class.
    3. Add a comment to the paragraph using Add method of the CommentCollection class.
      C#
      Copy Code
      doc.Load("CommentInWord.docx");
              
      //Access the first paragraph
      Paragraph par = doc.Body.Sections.First.GetRange().Paragraphs[0];
      
      //Add comment
      Comment c=par.GetRange().Comments.Add("CommentByCode","GC");
      
      //Save the document
      doc.Save("AddComment.docx");
    Back to Top

    Modify Comments

    To modify a comment through code:

    1. Access a comment from the comment collection using the Comments property. For example, access the first comment.
    2. Get the property of the comment you want to modify. For example, access Author and Initials properties of the first comment.
    3. Modify the value of the properties. For example, set value of the Author property to DsWordUser and Initials property to G.C.W.
      C#
      Copy Code
      doc.Load("AddComment.docx");
      
      //Access the first paragraph
      Paragraph par = doc.Body.Sections.First.GetRange().Paragraphs[0];
      
      //Modify the first comment's author and initials
      par.GetRange().Comments[0].Author = "DsWordUser";
      par.GetRange().Comments[0].Initials = "D.S.W";
                  
      //Mark the comment reply as closed
      par.GetRange().Comments[0].Done = true;
                 
      doc.Save("ModifiedComment.docx"); 
    Back to Top

    Delete Comments

    To delete a comment:

    1. Access a comment from the comment collection using the Comments property. For example, access the fourth comment in the document.
    2. Delete the comment using Delete method of the Comment class.
      C#
      Copy Code
      doc.Load("CommentInWord.docx");
              
      //Delete the second comment
      doc.Body.Comments[3].Delete();       
      doc.Save("DeleteComment.docx");
    Back to Top

    Add Comment Reply

    To add a reply on a comment:

    1. Access a comment from the comment collection using the Comments property. For example, access the second comment.
    2. Add a new comment in the comment collection as a reply for the accessed comment using Reply method of the Comment class.
      C#
      Copy Code
      doc.Load("CommentInWord.docx");
              
      //Add comment reply for the last comment in the document
      doc.Body.Comments.Last.Reply("Reply Comment", "John Doe");
      
      //Add a comment reply
      Comment cReply = c.Reply("Reply of the added comment", "JD");
      
      //Mark the comment reply as closed
      cReply.Done = true;
              
      //Save the document
      doc.Save("AddComment.docx");
    Back to Top

    Delete Comment Reply

    To delete a reply on the comment:

    1. Access a reply comment using Replies property of the Comment class. For example, access first reply of the first comment.
    2. Delete the comment reply using Delete method of the Comment class.
      C#
      Copy Code
      //Delete first comment's first reply
      doc.Body.Comments.First.Replies[0].Delete();
      
      doc.Save("DelComment.docx");
    Back to Top

    For more information on how implement comments using DsWord, see DsWord sample browser.