Skip to main content

Comments in Word Processing Documents

  • 4 minutes to read

All the document comments are contained in the CommentCollection. The SubDocument.Comments property obtains the comment collection.

Add Comments

To create a comment, use CommentCollection.Create method. It allows you to create a comment with the specified settings, such as author or creation date.

Comment

View Example

using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit;

static void CreateComment(RichEditDocumentServer wordProcessor) {

    // Load a document from a file.
    wordProcessor.LoadDocument("Documents\\Grimm.docx");

    // Access a document.
    Document document = wordProcessor.Document;

    if (document.Paragraphs.Count > 2) {
        // Access the range of the third paragraph.
        DocumentRange docRange = document.Paragraphs[2].Range;

        // Specify the comment's author name.
        string commentAuthor = "Johnson Alphonso D";

        // Create a comment.
        document.Comments.Create(docRange, commentAuthor, DateTime.Now);
    }
}

To create a nested comment, use the CommentCollection.Create method with the parent comment set as an argument.

View Example

static void CreateNestedComment(RichEditDocumentServer wordProcessor) {
    // Load a document from a file.
    wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);

    // Access a document.
    Document document = wordProcessor.Document;

    if (document.Comments.Count > 1)
    {
        // Create a new comment nested in the parent comment.
        Comment newComment = document.Comments.Create("Vicars Anny", document.Comments[1]);
        newComment.Date = DateTime.Now;
        SubDocument commentDocument = newComment.BeginUpdate();
        commentDocument.InsertText(commentDocument.Range.Start, "I agree");
        newComment.EndUpdate(commentDocument);
    }
}

Edit Comment Content

To edit the comment content, use the Comment.BeginUpdate and Comment.EndUpdate paired methods. The BeginUpdate method returns the SubDocument object. You can use this object’s methods to insert and format comment content. You can add any text and insert additional objects.

The following code snippet inserts simple text and a table to the comment:

View Example

using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit;
//...

static void EditCommentContent(RichEditDocumentServer wordProcessor) {

    // Load a document from a file.
    wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);

    // Access a document.
    Document document = wordProcessor.Document;

    int commentCount = document.Comments.Count;
    if (commentCount > 0) {

        // Access a comment.
        Comment comment = document.Comments[document.Comments.Count - 1];
        if (comment != null) {

            // Start to edit the comment.
            SubDocument commentDocument = comment.BeginUpdate();

            // Insert text to the comment.
            commentDocument.Paragraphs.Insert(commentDocument.Range.Start);
            commentDocument.InsertText(commentDocument.Range.Start, "some text");

            // Insert a table to the comment.
            commentDocument.Tables.Create(commentDocument.Range.End, 5, 4);

            // Finalize to edit the comment.
            comment.EndUpdate(commentDocument);
        }
    }
}

Edit Comment Properties

You can also change the comment settings by changing the corresponding Comment object properties. All the operation should be enclosed with Comment.BeginUpdate and Comment.EndUpdate paired methods as well.

View Example

Document document = server.Document;
document.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);
int commentCount = document.Comments.Count;
if (commentCount > 0)
{
    document.BeginUpdate();
    Comment comment = document.Comments[document.Comments.Count - 1];
    comment.Name = "New Name";
    comment.Date = DateTime.Now;
    comment.Author = "New Author";
    document.EndUpdate();
}

Delete Comments

To delete a comment from the document, delete it from the corresponding collection. To do that, use the CommentCollection.Remove method.

View Example

Document document = server.Document;
document.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);
if (document.Comments.Count > 0)
{
    document.Comments.Remove(document.Comments[0]);
}

Tip

Use the RichEditDocumentServer.Options.Comments property to specify comment options. Set the CommentOptions.Visibility property to Hidden to hide comments when you print or export the document to PDF format.