Working with Comments

Aspose.Words allows users to work with comments – comments in a document in Aspose.Words are represented by the Comment class. Also use the CommentRangeStart and CommentRangeEnd classes to specify the region of text that should be associated with a comment.

Add a Comment

Aspose.Words allows you to add comments in several ways:

  1. Using the Comment class
  2. Using the CommentRangeStart and CommentRangeEnd classes

The following code example shows how to add a comment to a paragraph using the Comment class:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Some text is added.");
Comment comment = new Comment(doc, "Awais Hafeez", "AH", DateTime.Today);
comment.SetText("Comment text.");
builder.CurrentParagraph.AppendChild(comment);
doc.Save(ArtifactsDir + "WorkingWithComments.AddComments.docx");
view raw add-comments.cs hosted with ❤ by GitHub

The following code example shows how to add a comment to a paragraph using a region of text and the CommentRangeStart and CommentRangeEnd classes:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
Paragraph para1 = new Paragraph(doc);
Run run1 = new Run(doc, "Some ");
Run run2 = new Run(doc, "text ");
para1.AppendChild(run1);
para1.AppendChild(run2);
doc.FirstSection.Body.AppendChild(para1);
Paragraph para2 = new Paragraph(doc);
Run run3 = new Run(doc, "is ");
Run run4 = new Run(doc, "added ");
para2.AppendChild(run3);
para2.AppendChild(run4);
doc.FirstSection.Body.AppendChild(para2);
Comment comment = new Comment(doc, "Awais Hafeez", "AH", DateTime.Today);
comment.Paragraphs.Add(new Paragraph(doc));
comment.FirstParagraph.Runs.Add(new Run(doc, "Comment text."));
CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.Id);
CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.Id);
run1.ParentNode.InsertAfter(commentRangeStart, run1);
run3.ParentNode.InsertAfter(commentRangeEnd, run3);
commentRangeEnd.ParentNode.InsertAfter(comment, commentRangeEnd);
doc.Save(ArtifactsDir + "WorkingWithComments.AnchorComment.doc");

Extract or Remove Comments

Using Comments in a Word document (in addition to Track Changes) is a common practice when reviewing documents, particularly when there are multiple reviewers. There can be situations where the only thing you need from a document is the comments. Say you want to generate a list of review findings, or perhaps you have collected all the useful information from the document and you simply want to remove unnecessary comments. You may want to view or remove the comments of a particular reviewer.

In this sample we are going to look at some simple methods for both gathering information from the comments within a document and for removing comments from a document. Specifically we’ll cover how to:

  • Extract all the comments from a document or only the ones made by a particular author
  • Remove all the comments from a document or only from a particular author

How to Extract or Remove Comments

The code in this sample is actually quite simple and all methods are based on the same approach. A comment in a Word document is represented by a Comment object in the Aspose.Words document object model. To collect all the comments in a document use the GetChildNodes method with the first parameter set to NodeType.Comment. Make sure that the second parameter of the GetChildNodes method is set to true: this forces the GetChildNodes to select from all child nodes recursively, rather than only collecting the immediate children.

To illustrate how to extract and remove comments from a document, we will go through the following steps:

  1. Open a Word document using the Document class
  2. Get all comments from the document into a collection
  3. To extract comments:
    1. Go through the collection using the foreach operator
    2. Extract and list the author name, date & time and text of all comments
    3. Extract and list the author name, date & time and text of comments written by a specific author, in this case the author ks
  4. To remove comments:
    1. Go backwards through the collection using the for operator
    2. Remove comments
  5. Save the changes

How to Extract All Comments

The GetChildNodes method is very useful and you can use it every time you need to get a list of document nodes of any type. The resulting collection does not create an immediate overhead because the nodes are selected into this collection only when you enumerate or access items in it.

The following code example shows how to extract the author name, date&time and text of all comments in the document:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
List<string> ExtractComments(Document doc)
{
List<string> collectedComments = new List<string>();
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
foreach (Comment comment in comments)
{
collectedComments.Add(comment.Author + " " + comment.DateTime + " " +
comment.ToString(SaveFormat.Text));
}
return collectedComments;
}

How to Extract Comments of a Specified Author

After you have selected Comment nodes into a collection, all you have to do is extract the information you need. In this sample, author initials, date, time and the plain text of the comment is combined into one string; you could choose to store it in some other ways instead.

The overloaded method that extracts the Comments from a particular author is almost the same, it just checks the author’s name before adding the info into the array.

The following code example shows how to extract the author name, date&time and text of the comments by the specified author:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
List<string> ExtractComments(Document doc, string authorName)
{
List<string> collectedComments = new List<string>();
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
foreach (Comment comment in comments)
{
if (comment.Author == authorName)
collectedComments.Add(comment.Author + " " + comment.DateTime + " " +
comment.ToString(SaveFormat.Text));
}
return collectedComments;
}

How to Remove Comments

If you are removing all comments, there is no need to move through the collection deleting comments one by one. You can remove them by calling the Clear method on the comments collection.

The following code example shows how to remove all comments in the document:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
void RemoveComments(Document doc)
{
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
comments.Clear();
}

When you need to selectively remove comments, the process becomes more similar to the code we used for comment extraction.

The following code example shows how to remove comments by the specified author:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
void RemoveComments(Document doc, string authorName)
{
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
// Look through all comments and remove those written by the authorName.
for (int i = comments.Count - 1; i >= 0; i--)
{
Comment comment = (Comment) comments[i];
if (comment.Author == authorName)
comment.Remove();
}
}

The main point to highlight here is the use of the for operator. Unlike the simple extraction, here you want to delete a comment. A suitable trick is to iterate the collection backwards from the last Comment to the first one. The reason for this if you start from the end and move backwards, the index of the preceding items remains unchanged, and you can work your way back to the first item in the collection.

The following code example shows the methods for the comments extraction and removal:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Comments.docx");
// Extract the information about the comments of all the authors.
foreach (string comment in ExtractComments(doc))
Console.Write(comment);
// Remove comments by the "pm" author.
RemoveComments(doc, "pm");
Console.WriteLine("Comments from \"pm\" are removed!");
// Extract the information about the comments of the "ks" author.
foreach (string comment in ExtractComments(doc, "ks"))
Console.Write(comment);
// Read the comment's reply and resolve them.
CommentResolvedAndReplies(doc);
// Remove all comments.
RemoveComments(doc);
Console.WriteLine("All comments are removed!");
doc.Save(ArtifactsDir + "WorkingWithComments.ProcessComments.docx");

How to Remove a Comment between CommentRangeStart and CommentRangeEnd

Using Aspose.Words you can also remove comments between the CommentRangeStart and CommentRangeEnd nodes.

The following code example shows how to remove text between CommentRangeStart and CommentRangeEnd:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Comments.docx");
CommentRangeStart commentStart = (CommentRangeStart)doc.GetChild(NodeType.CommentRangeStart, 0, true);
CommentRangeEnd commentEnd = (CommentRangeEnd)doc.GetChild(NodeType.CommentRangeEnd, 0, true);
Node currentNode = commentStart;
bool isRemoving = true;
while (currentNode != null && isRemoving)
{
if (currentNode.NodeType == NodeType.CommentRangeEnd)
isRemoving = false;
Node nextNode = currentNode.NextPreOrder(doc);
currentNode.Remove();
currentNode = nextNode;
}
doc.Save(ArtifactsDir + "WorkingWithComments.RemoveRangeText.docx");

Add or Remove Comment’s Reply

The AddReply method adds a reply to this comment. Please note that due to the existing Microsoft Office limitations only 1 level of replies is allowed in the document. An exception of type InvalidOperationException will be raised if this method is called on the existing Reply comment.

You can use the RemoveReply method to remove the specified reply to this comment.

The following code example shows how to add a reply to comment and remove comment’s reply:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Comments.docx");
Comment comment = (Comment) doc.GetChild(NodeType.Comment, 0, true);
comment.RemoveReply(comment.Replies[0]);
comment.AddReply("John Doe", "JD", new DateTime(2017, 9, 25, 12, 15, 0), "New reply");
doc.Save(ArtifactsDir + "WorkingWithComments.AddRemoveCommentReply.docx");

Read Comment’s Reply

The Replies property returns a collection of the Comment objects that are immediate children of the specified comment.

The following code example shows how to iterate through a comment’s replies and resolved them:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
void CommentResolvedAndReplies(Document doc)
{
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
Comment parentComment = (Comment) comments[0];
foreach (Comment childComment in parentComment.Replies)
{
// Get comment parent and status.
Console.WriteLine(childComment.Ancestor.Id);
Console.WriteLine(childComment.Done);
// And update comment Done mark.
childComment.Done = true;
}
}