Comments in Rich Text Documents

  • 6 minutes to read

The RichEditControl allows you to add comments to documents. You can add, edit, and remove comments in code and from the UI.

Create a Comment

Use the following API to attach a comment to a document range:

Member Description
Paragraph.Range Retrieves the paragraph’s range.
SubDocument.FindAll Finds all document ranges that contain the expression or text string.
SubDocument.Comments Provides access to the document’s Comment collection.
CommentCollection.Create Creates a new item in the CommentCollection.

The code snippet belows creates an empty comment associated with the following phrase: “an extensive problem in hardware and architecture is the construction of the emulation of checksums”.

DXRichEdit_Comments_EmptyComment

View Example

//The target range is the specific phrase in the introduction's first paragraph
DocumentRange[] foundRanges = richEditControl.Document.FindAll("an extensive problem in hardware and architecture is the construction of the emulation of checksums", SearchOptions.None);
if (foundRanges.Length > 0)
{
    //Create a new comment
    Comment comment = richEditControl.Document.Comments.Create(foundRanges[0], "Johnson Alphonso D", new DateTime(2014, 4, 25));
}

Create a Nested Comment

The code snippet below demonstrates how to create a nested comment. To do that, pass the parent content as the CommentCollection.Create method’s parameter.

DXRichEdit_Comment_NestedComment

View Example

Document document = richEditControl.Document;


if (document.Comments.Count > 0)
{
    //Create a new comment nested to the second comment in the collection
    Comment nestedComment = document.Comments.Create("Brian Zetc", DateTime.Now, document.Comments[1]);

    //Add text to the newly created comment
    SubDocument nestedCommentDocument = nestedComment.BeginUpdate();
    DocumentRange textRange = nestedCommentDocument.InsertText(nestedCommentDocument.CreatePosition(0),
    "Suffix trees are comprehensively reviewed in Wikipedia");
    nestedComment.EndUpdate(nestedCommentDocument);
}

Edit the Comment

Edit the Comment Content

Use the API from the table below to edit the comment’s content.

Member Description
Comment.BeginUpdate Opens the comment for modification.
SubDocument.InsertText Inserts a text string into the specified position.
TableCollection.Create Inserts a table into the specified position.
ShapeCollection.InsertPicture Inserts a floating picture into the specified position.
DocumentImageCollection.Insert Inserts an inline image into the specified position.
ShapeCollection.InsertTextBox Inserts a text box into the specified position.
Comment.EndUpdate Finalizes the comment update.

The following code sample adds text to the empty comment:

DXRichEdit_Comments_ContentInserted

View Example

Document document = richEditControl.Document;

int commentCount = document.Comments.Count;
if (commentCount > 0)
{
    //The target comment is the first one
    Comment comment = document.Comments[0];
    if (comment != null)
    {
        //Open the comment for modification
        SubDocument commentDocument = comment.BeginUpdate();

        //Add text to the comment range
        commentDocument.InsertText(commentDocument.CreatePosition(0),
            "J. Taylor, Enabling Voice - over - IP and RAID with sofa,  in Proceedings of NOSSDAV, Oct. 1994.\r\n" +
            @"R.Tarjan, S.Shenker, J.Gray, A.Einstein, Q.Thomas, and X.Sato, ""Deconstructing operating systems with flanchedripper"", in Proceedings of INFOCOM, Mar. 2000.");

        //End the comment update
        comment.EndUpdate(commentDocument);
    }
}

Edit the Comment Attributes

Use the following members to change the comment parameters (author, date, etc.):

Member Description
Comment.BeginUpdate Opens the comment for editing.
Comment.Author Specifies the comment’s author.
Comment.Date Specifies the comment’s last modified date.
Comment.Name Specifies the comment name.
Comment.EndUpdate Finalizes the comment update.

The code snippet below changes the comment’s name, date and author.

DXRichEdit_Comment_EditAttributes_Before

View Example

Document document = richEditControl.Document;
int commentCount = document.Comments.Count;

if (commentCount > 0)
{

    document.BeginUpdate();
    //The target is the second last comment
    Comment comment = document.Comments[document.Comments.Count - 1];

    //Change the comment's name, author and creation date
    comment.Name = "Reference";
    comment.Date = DateTime.Now;
    comment.Author = "Vicars Annie";
    document.EndUpdate();
}

The image below demonstrates the result of the code execution.

DXRichEdit_Comment_EditAttributes_After

Delete the Comment

To remove a comment from the document, use the CommentCollection.Remove method as shown in the code snippet below.

Document document = richEditControl.Document;
if (document.Comments.Count > 0)
  {
   //Remove the fourth document comment
   document.Comments.Remove(document.Comments[3]);
  }

Comments in the User Interface

Users can use the Review Ribbon tab to insert, modify or remove comments. Refer to the Lesson 5 - Create Separate Ribbon Pages for a Rich Text Editor topic for more information on how to add the ribbon menu to the application.

The Comment group on the Review tab allows users to insert new comments, remove existing comments and navigate through all the comments in the document.

rich-edit-comments-delete-comments

The Tracking group buttons allow users to highlight comments in the document, to filter them by the author, and specify whether to display comments in the document margins or Reviewing Pane.

rich-edit-comments-reviewers

To provide the application with the Reviewing Pane, set the RichEditControl.ShowReviewingPane property to true in XAML.

<dxre:RichEditControl ShowReviewingPane="True"/>

Display Options

The comments are displayed in the Reviewing Pane or in balloons that appear in the document margins. The related ranges are highlighted by a different color for each reviewer.

rich-edit-comment

Use the DXRichEditCommentOptions ‘s class properties to change the comment displaying options, such as range highlighting color or comment visibility.

Tip

Set the RichEditControl’s DocumentCapabilitiesOptions.Comments property to Disabled to disable comments.