In Aspose.Words, use the Comment class and the Document.GetChildNodes method to get all comments from a document.
The following code example shows how to retrieve comments from a Word Document:
public void RetrieveCommentsFeature()
{
Document doc = new Document(MyDir + "Comments.docx");
ArrayList collectedComments = new ArrayList();
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
// Look through all comments and gather information about them.
foreach (Comment comment in comments)
collectedComments.Add(comment.Author + " " + comment.DateTime + " " + comment.ToString(SaveFormat.Text));
foreach (string collectedComment in collectedComments)
Console.WriteLine(collectedComment);
}
You can also do the same using the Open XML SDK. At the same time, note that it looks somewhat more complicated and more cumbersome.
Following are the namespaces we need to add:
using System;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using NUnit.Framework;
The following code example shows how to retrieve comments from a Word Document:
public static void RetrieveCommentsFeature()
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(MyDir + "Comments.docx", false))
{
WordprocessingCommentsPart commentsPart = wordDoc.MainDocumentPart.WordprocessingCommentsPart;
if (commentsPart?.Comments != null)
foreach (Comment comment in commentsPart.Comments.Elements<Comment>())
Console.WriteLine(comment.InnerText);
}
}