officefileapi-116863-word-processing-document-api-word-processing-document-comments.md
All the document comments are contained in the CommentCollection. The SubDocument.Comments property obtains the comment collection.
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. When you add a new comment, the CommentOptions.Visibility property is set to Visible.
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit;
static void CreateComment(RichEditDocumentServer wordProcessor) {
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\Grimm.docx");
// Access a document.
Document document = wordProcessor.Document;
if (document.Paragraphs.Count > 2) {
// Access the range of the third paragraph.
DocumentRange docRange = document.Paragraphs[2].Range;
// Specify the comment's author name.
string commentAuthor = "Johnson Alphonso D";
// Create a comment.
document.Comments.Create(docRange, commentAuthor, DateTime.Now);
}
}
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.XtraRichEdit
Shared Sub CreateComment(ByVal wordProcessor As RichEditDocumentServer)
' Load a document from a file.
wordProcessor.LoadDocument("Documents\Grimm.docx")
' Access a document.
Dim document As Document = wordProcessor.Document
If document.Paragraphs.Count > 2 Then
' Access the range of the third paragraph.
Dim docRange As DocumentRange = document.Paragraphs(2).Range
' Specify the comment's author name.
Dim commentAuthor As String = "Johnson Alphonso D"
' Create a comment.
document.Comments.Create(docRange, commentAuthor, Date.Now)
End If
End Sub
To create a nested comment, use the CommentCollection.Create method with the parent comment set as an argument.
static void CreateNestedComment(RichEditDocumentServer wordProcessor) {
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.Docx);
// Access a document.
Document document = wordProcessor.Document;
if (document.Comments.Count > 1)
{
// Create a new comment nested in the parent comment.
Comment newComment = document.Comments.Create("Vicars Anny", document.Comments[1]);
newComment.Date = DateTime.Now;
SubDocument commentDocument = newComment.BeginUpdate();
commentDocument.InsertText(commentDocument.Range.Start, "I agree");
newComment.EndUpdate(commentDocument);
}
}
Shared Sub CreateNestedComment(ByVal wordProcessor As RichEditDocumentServer)
' Load a document from a file.
wordProcessor.LoadDocument("Documents\Grimm.docx", DocumentFormat.Docx)
' Access a document.
Dim document As Document = wordProcessor.Document
If document.Comments.Count > 1 Then
' Create a new comment nested in the parent comment.
Dim newComment As Comment = document.Comments.Create("Vicars Anny", document.Comments(1))
newComment.Date = Date.Now
Dim commentDocument As SubDocument = newComment.BeginUpdate()
commentDocument.InsertText(commentDocument.Range.Start, "I agree")
newComment.EndUpdate(commentDocument)
End If
End Sub
To edit the comment content, use the Comment.BeginUpdate and Comment.EndUpdate paired methods. The BeginUpdate method returns the SubDocument object. You can use this object’s methods to insert and format comment content. You can add any text and insert additional objects.
The following code snippet inserts simple text and a table to the comment:
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit;
//...
static void EditCommentContent(RichEditDocumentServer wordProcessor) {
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.Docx);
// Access a document.
Document document = wordProcessor.Document;
int commentCount = document.Comments.Count;
if (commentCount > 0) {
// Access a comment.
Comment comment = document.Comments[document.Comments.Count - 1];
if (comment != null) {
// Start to edit the comment.
SubDocument commentDocument = comment.BeginUpdate();
// Insert text to the comment.
commentDocument.Paragraphs.Insert(commentDocument.Range.Start);
commentDocument.InsertText(commentDocument.Range.Start, "some text");
// Insert a table to the comment.
commentDocument.Tables.Create(commentDocument.Range.End, 5, 4);
// Finalize to edit the comment.
comment.EndUpdate(commentDocument);
}
}
}
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.XtraRichEdit
'...
Shared Sub EditCommentContent(ByVal wordProcessor As RichEditDocumentServer)
' Load a document from a file.
wordProcessor.LoadDocument("Documents\Grimm.docx", DocumentFormat.Docx)
' Access a document.
Dim document As Document = wordProcessor.Document
Dim commentCount As Integer = document.Comments.Count
If commentCount > 0 Then
' Access a comment.
Dim comment As Comment = document.Comments(document.Comments.Count - 1)
If comment IsNot Nothing Then
' Start to edit the comment.
Dim commentDocument As SubDocument = comment.BeginUpdate()
' Insert text to the comment.
commentDocument.Paragraphs.Insert(commentDocument.Range.Start)
commentDocument.InsertText(commentDocument.Range.Start, "some text")
' Insert a table to the comment.
commentDocument.Tables.Create(commentDocument.Range.End, 5, 4)
' Finalize to edit the comment.
comment.EndUpdate(commentDocument)
End If
End If
End Sub
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.Docx);
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();
}
Dim document As Document = server.Document
document.LoadDocument("Documents\Grimm.docx", DocumentFormat.Docx)
Dim commentCount As Integer = document.Comments.Count
If commentCount > 0 Then
document.BeginUpdate()
Dim comment As Comment = document.Comments(document.Comments.Count - 1)
comment.Name = "New Name"
comment.Date = Date.Now
comment.Author = "New Author"
document.EndUpdate()
End If
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.Docx);
if (document.Comments.Count > 0)
{
document.Comments.Remove(document.Comments[0]);
}
Dim document As Document = server.Document
document.LoadDocument("Documents\Grimm.docx", DocumentFormat.Docx)
If document.Comments.Count > 0 Then
document.Comments.Remove(document.Comments(0))
End If
Tip
Use the RichEditDocumentServer.Options.Comments property to specify comment options. Set the CommentOptions.Visibility property to Hidden to hide comments when you print or export the document to PDF format.