Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Manage Comments

    • 6 minutes to read

    A comment is a note attached to a document range. A comment contains the author name, date, and content (paragraphs and text). A comment can have replies.

    Use the WordDocument.Comments property to access comments in a document. The CommentCollection contains Comment objects. Replies (Reply objects) are not in the collection. Use the Comment.Replies property to access replies to a comment.

    Comment Settings

    Comment and Reply classes include the following properties:

    Property Description
    CommentBase.Author Specifies the author name.
    CommentBase.Date Specifies the date and time when the author created the comment or reply.
    CommentBase.Content Contains comment or reply paragraphs and text.
    MarkedRangeBase.Range Specifies the commented document range.
    MarkedRangeBase.DocumentFragment Identifies the document fragment that contains the commented range (for example, the document body).
    Comment.Replies Contains replies to the comment.
    Reply.ParentComment Identifies the comment that the reply answers.

    Add Comments and Replies

    To add a comment, create a CommentDefinition object and pass it to the CommentCollection.Create(range, commentDefinition) method with the commented range. CommentDefinition.Author and CommentDefinition.Date properties specify the comment author and date.

    To specify the commented range, create a DocumentRange object, or call the DocumentFragment.GetRange(element) method to get the range of a document element (for example, a paragraph or a text fragment).

    A new comment contains an empty paragraph. Comment content is a DocumentFragment object. You can add text, paragraphs, and other elements to comment content in the same way as you do to the document body. For example, call the DocumentFragment.AddText(text) method to add text to the last paragraph.

    Call the Comment.CreateReply(commentDefinition) method to add a reply to a comment.

    The following code snippet adds a comment to a date in document text and then adds a reply to this comment:

    using System.IO;
    using DevExpress.Docs.Word;
    
    using var document = new WordDocument();
    document.Body.AddText("The contract takes effect on ");
    var date = document.Body.AddText("January 1, 2027");
    document.Body.AddText(".");
    
    // Add a comment to the date.
    var range = document.Body.GetRange(date);
    var comment = document.Comments.Create(range, new CommentDefinition { Author = "John Smith" });
    comment.Content.AddText("Please confirm the effective date.");
    
    // Reply to the comment.
    var reply = comment.CreateReply(new CommentDefinition { Author = "Jane Doe" });
    reply.Content.AddText("The date is confirmed.");
    
    Directory.CreateDirectory("output");
    using var stream = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(stream, DocumentFormat.Docx);
    

    Read Comments

    Iterate through WordDocument.Comments and Comment.Replies collections to read comments and replies. To get the commented text, pass the Range property value to the DocumentFragment.GetText(range) method of the fragment specified by the DocumentFragment property. Call the DocumentFragment.GetText() method for the Content fragment to get the comment text.

    The following code snippet outputs the author, date, commented text, and text of each comment and reply to the console:

    using System;
    using System.IO;
    using DevExpress.Docs.Word;
    
    using var document = new WordDocument(File.ReadAllBytes("Document.docx"));
    foreach (var comment in document.Comments)
    {
        var commentedText = comment.DocumentFragment.GetText(comment.Range);
        Console.WriteLine($"{comment.Author} ({comment.Date:g}) on \"{commentedText}\": {comment.Content.GetText()}");
        foreach (var reply in comment.Replies)
            Console.WriteLine($"    {reply.Author} ({reply.Date:g}): {reply.Content.GetText()}");
    }
    

    Modify Comments

    Use Author and Date properties to modify the author name and date of a comment or reply.

    To modify comment text, edit the Content fragment. For example, call the DocumentFragment.Clear() method to remove current content, and then add new paragraphs.

    To attach a comment to another text range, assign a DocumentRange object to the Range property. The new range must be in the document fragment that contains the comment.

    Remove Comments and Replies

    Call CommentCollection.Remove(comment) or CommentCollection.RemoveAt(index) methods to remove a comment. These methods also remove replies to that comment. The commented text stays in the document.

    Call the Comment.RemoveReply(reply) method to remove a reply.

    The following code snippet removes comments and replies by Jane Doe:

    using System.IO;
    using DevExpress.Docs.Word;
    
    using var document = new WordDocument(File.ReadAllBytes("Document.docx"));
    var comments = document.Comments;
    for (var i = comments.Count - 1; i >= 0; i--)
    {
        var comment = comments[i];
        if (comment.Author == "Jane Doe")
        {
            // Remove the comment and replies to the comment.
            comments.RemoveAt(i);
            continue;
        }
        for (var j = comment.Replies.Count - 1; j >= 0; j--)
        {
            if (comment.Replies[j].Author == "Jane Doe")
                comment.RemoveReply(comment.Replies[j]);
        }
    }
    
    Directory.CreateDirectory("output");
    using var stream = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(stream, DocumentFormat.Docx);
    

    When you remove a comment or a reply, the MarkedRangeBase.Invalidated property of the removed object gets true. If you access other members of a removed comment or reply, the API throws an InvalidOperationException.

    Export, Protect, and Sanitize Documents with Comments

    PDF export
    Set the PdfExportOptions.ExportComments property to false to exclude comments from the PDF document. Refer to the following help topic for additional information: Export Documents to PDF.
    Document protection
    To allow users to add comments to a protected document in Microsoft Word and prevent other modifications, set the DocumentProtectionOptions.ProtectionType property to DocumentProtectionType.AllowComments and pass the options to the WordDocument.Protect(options) method. Refer to the following help topic for additional information: Manage Document Security.
    Document inspection and sanitization
    Call the WordDocument.Inspect(options) method with the DocumentInspectOptions.Comments flag to check whether a document contains comments. To remove all comments before you share a document, call the WordDocument.Sanitize(options) method. The SanitizeOptions.RemoveComments property specifies whether the Sanitize method removes comments. Refer to the following help topic for additional information: Safer Document Processing.

    Next Steps