The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

How to: Create, Edit and Delete Comments

  • 3 min 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 CommentCollection. Access the collection using 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 document = server.Document;
server.LoadDocument("Documents\\Grimm.docx", 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 document = server.Document;
document.LoadDocument("Documents\\Grimm.docx", 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 document = server.Document;
document.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);
int commentCount = document.Comments.Count;
if (commentCount > 0)
{
    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 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 a comment

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

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 DevExpress.XtraRichEdit.CommentOptions class properties to specify comment options. Use the RichEditDocumentServer.Options.Comments property to access the comment options. Set the CommentOptions.Visibility property to RichEditCommentVisibility.Hidden to hide comments when printing or exporting the document to the PDF format.