CommentRangeStart
Contents
[
Hide
]CommentRangeStart class
Denotes the start of a region of text that has a comment associated with it.
To learn more, visit the Working with Comments documentation article.
public sealed class CommentRangeStart : Node
Constructors
Name | Description |
---|---|
CommentRangeStart(DocumentBase, int) | Initializes a new instance of this class. |
Properties
Name | Description |
---|---|
CustomNodeId { get; set; } | Specifies custom node identifier. |
virtual Document { get; } | Gets the document to which this node belongs. |
Id { get; set; } | Specifies the identifier of the comment to which this region is linked. |
virtual IsComposite { get; } | Returns true if this node can contain other nodes. |
NextSibling { get; } | Gets the node immediately following this node. |
override NodeType { get; } | Returns CommentRangeStart. |
ParentNode { get; } | Gets the immediate parent of this node. |
PreviousSibling { get; } | Gets the node immediately preceding this node. |
Range { get; } | Returns a Range object that represents the portion of a document that is contained in this node. |
Methods
Name | Description |
---|---|
override Accept(DocumentVisitor) | Accepts a visitor. |
Clone(bool) | Creates a duplicate of the node. |
GetAncestor(NodeType) | Gets the first ancestor of the specified NodeType . |
GetAncestor(Type) | Gets the first ancestor of the specified object type. |
virtual GetText() | Gets the text of this node and of all its children. |
NextPreOrder(Node) | Gets next node according to the pre-order tree traversal algorithm. |
PreviousPreOrder(Node) | Gets the previous node according to the pre-order tree traversal algorithm. |
Remove() | Removes itself from the parent. |
ToString(SaveFormat) | Exports the content of the node into a string in the specified format. |
ToString(SaveOptions) | Exports the content of the node into a string using the specified save options. |
Remarks
To create a comment anchored to a region of text, you need to create a Comment
and then create CommentRangeStart
and CommentRangeEnd
and set their identifiers to the same Id
value.
CommentRangeStart
is an inline-level node and can only be a child of Paragraph
.
Examples
Shows how print the contents of all comments and their comment ranges using a document visitor.
public void CreateCommentsAndPrintAllInfo()
{
Document doc = new Document();
Comment newComment = new Comment(doc)
{
Author = "VDeryushev",
Initial = "VD",
DateTime = DateTime.Now
};
newComment.SetText("Comment regarding text.");
// Add text to the document, warp it in a comment range, and then add your comment.
Paragraph para = doc.FirstSection.Body.FirstParagraph;
para.AppendChild(new CommentRangeStart(doc, newComment.Id));
para.AppendChild(new Run(doc, "Commented text."));
para.AppendChild(new CommentRangeEnd(doc, newComment.Id));
para.AppendChild(newComment);
// Add two replies to the comment.
newComment.AddReply("John Doe", "JD", DateTime.Now, "New reply.");
newComment.AddReply("John Doe", "JD", DateTime.Now, "Another reply.");
PrintAllCommentInfo(doc.GetChildNodes(NodeType.Comment, true));
}
/// <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);
// Visit only start of the comment.
comment.AcceptStart(commentVisitor);
// Visit only end of the comment.
comment.AcceptEnd(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>
/// Prints information and contents of all 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;
}
See Also
- class Node
- namespace Aspose.Words
- assembly Aspose.Words