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 Comments
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 below creates an empty comment associated with the following phrase: “an extensive problem in hardware and architecture is the construction of the emulation of checksums”.
// 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 Nested Comments
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.
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 Comments
Edit Comment Content
Use the API from the table below to edit the comment’s content.
Member | Description |
---|---|
Comment.BeginUpdate | Opens the comment for modification. Use the SubDocument object returned by the BeginUpdate method to add and edit comment content. |
SubDocument.InsertText | Inserts a text string into the specified position. |
TableCollection.Create | Inserts a table into a document position. |
ShapeCollection.InsertPicture | Inserts a floating picture into the specified position position. |
DocumentImageCollection.Insert | Inserts an inline image in the specified position. |
ShapeCollection.InsertTextBox | Inserts a text box in the specified position. |
Comment.EndUpdate | Finalizes the comment update. |
The following code sample adds text to the empty comment:
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 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’s name. |
Comment.EndUpdate | Finalizes the comment update. |
The code snippet below changes the comment’s name, date and author.
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();
}
Delete Comments
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 How to: Create the RichEditControl with a Ribbon UI 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.
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.
To add the Reviewing Pane to the RichEditControl, click the RichEditControl’s smart tag in the designer mode and select Create Reviewing Pane.
Tip
Set the RichEditControl’s DocumentCapabilitiesOptions.Comments property to Disabled to disable comments.
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.
Use the CommentOptions‘s class properties to change the comment displaying options, such as range highlighting color or comment visibility. Access these properties via the richEditControl.Options.Annotations.Comments
notation.
Note
When you add a new comment, the CommentOptions.Visibility property set to Hidden changes its value to Visible.