DocumentVisitorVisitCommentEnd Method |
Namespace: Aspose.Words
public void CommentsToText() { // Open the document that has comments/comment ranges we want to print the info of Document doc = new Document(MyDir + "DocumentVisitor-compatible features.docx"); // Create an object that inherits from the DocumentVisitor class CommentInfoPrinter visitor = new CommentInfoPrinter(); // Accepting a visitor lets it start traversing the nodes in the document, // starting with the node that accepted it to then recursively visit every child doc.Accept(visitor); // Once the visiting is complete, we can retrieve the result of the operation, // that in this example, has accumulated in the visitor Console.WriteLine(visitor.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.GetText() + "\""); 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]"); 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; }