/// <summary> /// Iterates over every top-level comment and prints its comment range, contents, and replies. /// </summary> private static void PrintAllCommentInfo(NodeCollection comments) { CommentInfoPrinter commentVisitor = new CommentInfoPrinter(); // Iterate over all top level comments. Unlike reply-type comments, top-level comments have no ancestor. foreach (Comment comment in comments.Where(c => ((Comment)c).Ancestor == null)) { // First, visit the start of the comment range. CommentRangeStart commentRangeStart = (CommentRangeStart)comment.PreviousSibling.PreviousSibling.PreviousSibling; commentRangeStart.Accept(commentVisitor); // Then, visit the comment, and any replies that it may have. comment.Accept(commentVisitor); foreach (Comment reply in comment.Replies) { reply.Accept(commentVisitor); } // Finally, visit the end of the comment range, and then print the visitor's text contents. CommentRangeEnd commentRangeEnd = (CommentRangeEnd)comment.PreviousSibling; commentRangeEnd.Accept(commentVisitor); Console.WriteLine(commentVisitor.GetText()); } }
/// <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()); } }
[Test] //ExSkip public void CommentsToText() { // Open the document that has comments/comment ranges we want to print the info of Document doc = new Document(MyDir + "DocumentVisitor.Destination.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()); }