Skip to main content
All docs
V19.1

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

    Note

    A complete sample project is available at https://github.com/DevExpress-Examples/wpf-richedit-document-api-t213968.

    document.LoadDocument("Documents//Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
    DocumentRange docRange = document.Paragraphs[1].Range;
    string commentAuthor = "Maryland B. Clopton";
    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.

    Note

    A complete sample project is available at https://github.com/DevExpress-Examples/wpf-richedit-document-api-t213968.

    document.LoadDocument("Documents//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.

Note

A complete sample project is available at https://github.com/DevExpress-Examples/wpf-richedit-document-api-t213968.

document.LoadDocument("Documents//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), "comment text");
        commentDocument.Tables.Create(commentDocument.CreatePosition(13), 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.

Note

A complete sample project is available at https://github.com/DevExpress-Examples/wpf-richedit-document-api-t213968.

CommentHelper.CreateComment(document);
int commentCount = document.Comments.Count;
if (document.Comments.Count > 0)
{
    // Uncomment the line below to delete a comment.
    //document.Comments.Remove(document.Comments[0]);
}

Delete a comment

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

Note

A complete sample project is available at https://github.com/DevExpress-Examples/wpf-richedit-document-api-t213968.

CommentHelper.CreateComment(document);
int commentCount = document.Comments.Count;
if (document.Comments.Count > 0)
{
    // Uncomment the line below to delete a comment.
    //document.Comments.Remove(document.Comments[0]);
}