Skip to main content
A newer version of this page is available. .

How to: Create, Edit and Delete Comments

  • 3 minutes to read

The following example illustrates how to add, edit and delete comments in the current document.

All the document comments are contained in the collection represented by the CommentCollection interface. The collection can be accessed through the SubDocument.Comments property.

Add a comment

  • 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

    document.LoadDocument("Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
    DocumentRange docRange = document.Paragraphs[2].Range;
    string commentAuthor = "Johnson Alphonso D";
    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.

    document.LoadDocument("Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
    if (document.Comments.Count > 0) {
        DocumentRange[] resRanges = document.FindAll("trump", SearchOptions.None, document.Comments[1].Range);
        if (resRanges.Length > 0) {
            Comment newComment = document.Comments.Create("Vicars Anny", document.Comments[1]);
            newComment.Date = DateTime.Now;
        }
    }
    

Edit a comment

To edit the comment content, use the Comment.BeginUpdate and Comment.EndUpdate paired methods. You can add any text and insert additional objects to the comment, as illustrated below.

document.LoadDocument("Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
int commentCount = document.Comments.Count;
if (commentCount > 0)
{
    DevExpress.XtraRichEdit.API.Native.Comment comment = document.Comments[document.Comments.Count - 1];
    if (comment != null)
    {
        SubDocument commentDocument = comment.BeginUpdate();
        commentDocument.InsertText(commentDocument.CreatePosition(0), "some text");
        commentDocument.Tables.Create(commentDocument.CreatePosition(9), 5, 4);
        comment.EndUpdate(commentDocument);
    }
}

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.

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

Delete a comment

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

document.LoadDocument("Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
if (document.Comments.Count > 0)
{
    document.Comments.Remove(document.Comments[0]);
}