CommentInitial Property |
Namespace: Aspose.Words
Cannot be null.
Default is empty string.
public void CreateCommentsAndPrintAllInfo() { Document doc = new Document(); doc.RemoveAllChildren(); Section sect = (Section)doc.AppendChild(new Section(doc)); Body body = (Body)sect.AppendChild(new Body(doc)); // Create a commented text with several comment replies for (int i = 0; i <= 10; i++) { Comment newComment = CreateComment(doc, "VDeryushev", "VD", DateTime.Now, "My test comment " + i); Paragraph para = (Paragraph)body.AppendChild(new Paragraph(doc)); para.AppendChild(new CommentRangeStart(doc, newComment.Id)); para.AppendChild(new Run(doc, "Commented text " + i)); para.AppendChild(new CommentRangeEnd(doc, newComment.Id)); para.AppendChild(newComment); for (int y = 0; y <= 2; y++) { newComment.AddReply("John Doe", "JD", DateTime.Now, "New reply " + y); } } // Look at information of our comments PrintAllCommentInfo(ExtractComments(doc)); } /// <summary> /// Create a new comment /// </summary> public static Comment CreateComment(Document doc, string author, string initials, DateTime dateTime, string text) { Comment newComment = new Comment(doc) { Author = author, Initial = initials, DateTime = dateTime }; newComment.SetText(text); return newComment; } /// <summary> /// Extract comments from the document without replies. /// </summary> public static List<Comment> ExtractComments(Document doc) { List<Comment> collectedComments = new List<Comment>(); NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true); foreach (Comment comment in comments) { // All replies have ancestor, so we will add this check if (comment.Ancestor == null) { collectedComments.Add(comment); } } return collectedComments; } /// <summary> /// Use an iterator and a visitor to print info of every comment from within a document. /// </summary> private static void PrintAllCommentInfo(List<Comment> comments) { // Create an object that inherits from the DocumentVisitor class CommentInfoPrinter commentVisitor = new CommentInfoPrinter(); // Get the enumerator from the document's comment collection and iterate over the comments using (IEnumerator<Comment> enumerator = comments.GetEnumerator()) { while (enumerator.MoveNext()) { Comment currentComment = enumerator.Current; // Accept our DocumentVisitor it to print information about our comments if (currentComment != null) { // Get CommentRangeStart from our current comment and construct its information CommentRangeStart commentRangeStart = (CommentRangeStart)currentComment.PreviousSibling.PreviousSibling.PreviousSibling; commentRangeStart.Accept(commentVisitor); // Construct current comment information currentComment.Accept(commentVisitor); // Get CommentRangeEnd from our current comment and construct its information CommentRangeEnd commentRangeEnd = (CommentRangeEnd)currentComment.PreviousSibling; commentRangeEnd.Accept(commentVisitor); } } // Output of all information received Console.WriteLine(commentVisitor.GetText()); } } /// <summary> /// This Visitor implementation prints information about and contents of comments and comment ranges encountered in the document. /// </summary> public class CommentInfoPrinter : DocumentVisitor { public CommentInfoPrinter() { mBuilder = new StringBuilder(); mVisitorIsInsideComment = false; } /// <summary> /// Gets the plain text of the document that was accumulated by the visitor. /// </summary> public string GetText() { return mBuilder.ToString(); } /// <summary> /// Called when a Run node is encountered in the document. /// </summary> public override VisitorAction VisitRun(Run run) { if (mVisitorIsInsideComment) IndentAndAppendLine("[Run] \"" + run.Text + "\""); return VisitorAction.Continue; } /// <summary> /// Called when a CommentRangeStart node is encountered in the document. /// </summary> public override VisitorAction VisitCommentRangeStart(CommentRangeStart commentRangeStart) { IndentAndAppendLine("[Comment range start] ID: " + commentRangeStart.Id); mDocTraversalDepth++; mVisitorIsInsideComment = true; return VisitorAction.Continue; } /// <summary> /// Called when a CommentRangeEnd node is encountered in the document. /// </summary> public override VisitorAction VisitCommentRangeEnd(CommentRangeEnd commentRangeEnd) { mDocTraversalDepth--; IndentAndAppendLine("[Comment range end] ID: " + commentRangeEnd.Id + "\n"); mVisitorIsInsideComment = false; return VisitorAction.Continue; } /// <summary> /// Called when a Comment node is encountered in the document. /// </summary> public override VisitorAction VisitCommentStart(Comment comment) { IndentAndAppendLine( $"[Comment start] For comment range ID {comment.Id}, By {comment.Author} on {comment.DateTime}"); mDocTraversalDepth++; mVisitorIsInsideComment = true; return VisitorAction.Continue; } /// <summary> /// Called when the visiting of a Comment node is ended in the document. /// </summary> public override VisitorAction VisitCommentEnd(Comment comment) { mDocTraversalDepth--; IndentAndAppendLine("[Comment end]"); mVisitorIsInsideComment = false; return VisitorAction.Continue; } /// <summary> /// Append a line to the StringBuilder and indent it depending on how deep the visitor is into the document tree. /// </summary> /// <param name="text"></param> private void IndentAndAppendLine(string text) { for (int i = 0; i < mDocTraversalDepth; i++) { mBuilder.Append("| "); } mBuilder.AppendLine(text); } private bool mVisitorIsInsideComment; private int mDocTraversalDepth; private readonly StringBuilder mBuilder; }