使用评论
在线尝试
您可以使用我们的 免费在线删除注释 尝试此功能。
Aspose.Words 允许用户使用注释 - Aspose.Words 文档中的注释由 Comment 类表示。还可以使用 CommentRangeStart 和 CommentRangeEnd 类来指定应与注释关联的文本区域。
添加评论
Aspose.Words 允许您通过多种方式添加注释:
1.使用Comment类 2.使用CommentRangeStart和CommentRangeEnd类
以下代码示例演示如何使用 Comment 类向段落添加注释:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithComments(); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Write("Some text is added."); | |
Comment comment = new Comment(doc, "Awais Hafeez", "AH", DateTime.Today); | |
builder.CurrentParagraph.AppendChild(comment); | |
comment.Paragraphs.Add(new Paragraph(doc)); | |
comment.FirstParagraph.Runs.Add(new Run(doc, "Comment text.")); | |
dataDir = dataDir + "Comments_out.doc"; | |
// Save the document. | |
doc.Save(dataDir); |
以下代码示例演示如何使用文本区域以及 CommentRangeStart 和 CommentRangeEnd 类向段落添加注释:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithComments(); | |
Document doc = new Document(); | |
Paragraph para1 = new Paragraph(doc); | |
Run run1 = new Run(doc, "Some "); | |
Run run2 = new Run(doc, "text "); | |
para1.AppendChild(run1); | |
para1.AppendChild(run2); | |
doc.FirstSection.Body.AppendChild(para1); | |
Paragraph para2 = new Paragraph(doc); | |
Run run3 = new Run(doc, "is "); | |
Run run4 = new Run(doc, "added "); | |
para2.AppendChild(run3); | |
para2.AppendChild(run4); | |
doc.FirstSection.Body.AppendChild(para2); | |
Comment comment = new Comment(doc, "Awais Hafeez", "AH", DateTime.Today); | |
comment.Paragraphs.Add(new Paragraph(doc)); | |
comment.FirstParagraph.Runs.Add(new Run(doc, "Comment text.")); | |
CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.Id); | |
CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.Id); | |
run1.ParentNode.InsertAfter(commentRangeStart, run1); | |
run3.ParentNode.InsertAfter(commentRangeEnd, run3); | |
commentRangeEnd.ParentNode.InsertAfter(comment, commentRangeEnd); | |
dataDir = dataDir + "Anchor.Comment_out.doc"; | |
// Save the document. | |
doc.Save(dataDir); |
提取或删除评论
在 Word 文档中使用注释(除了跟踪更改之外)是审阅文档时的常见做法,特别是当有多个审阅者时。在某些情况下,您从文档中需要的唯一内容可能就是注释。假设您想要生成审核结果列表,或者您可能已经从文档中收集了所有有用的信息,并且您只想删除不必要的注释。您可能想要查看或删除特定审阅者的评论。
在此示例中,我们将了解一些简单的方法,用于从文档中的注释中收集信息以及从文档中删除注释。具体来说,我们将介绍如何:
- 提取文档中的所有评论或仅提取特定作者的评论
- 删除文档中的所有评论或仅删除特定作者的评论
如何提取或删除评论
该示例中的代码实际上非常简单,并且所有方法都基于相同的方法。 Word 文档中的注释由 Aspose.Words 文档对象模型中的 Comment 对象表示。要收集文档中的所有注释,请使用 GetChildNodes 方法,并将第一个参数设置为 NodeType.Comment。确保 GetChildNodes 方法的第二个参数设置为 true:这会强制 GetChildNodes 递归地从所有子节点中进行选择,而不是仅收集直接子节点。
为了说明如何从文档中提取和删除注释,我们将执行以下步骤:
1.使用Document类打开Word文档 2.将文档中的所有评论获取到一个集合中 3. 提取注释:
- 使用 foreach 运算符遍历集合
- 提取并列出所有评论的作者姓名、日期和时间以及文本
- 提取并列出作者姓名、日期和时间以及特定作者撰写的评论文本,在本例中为作者"ks"
- 删除评论:
- 使用 for 运算符向后遍历集合 2.删除评论
- 保存更改
如何提取所有评论
GetChildNodes 方法非常有用,每次需要获取任何类型的文档节点列表时都可以使用它。生成的集合不会立即产生开销,因为仅当您枚举或访问其中的项目时,才会将节点选择到该集合中。
以下代码示例展示了如何提取文档中所有评论的作者姓名、日期和时间以及文本:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
static ArrayList ExtractComments(Document doc) | |
{ | |
ArrayList collectedComments = new ArrayList(); | |
// Collect all comments in the document | |
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)); | |
} | |
return collectedComments; | |
} |
如何提取指定作者的评论
将 Comment 节点选择到集合中后,您所要做的就是提取所需的信息。在此示例中,作者姓名首字母、日期、时间和评论的纯文本组合成一个字符串;您可以选择以其他方式存储它。
从特定作者提取评论的重载方法几乎相同,它只是在将信息添加到数组之前检查作者的姓名。
以下代码示例显示如何提取指定作者的评论的作者姓名、日期和时间以及文本:
如何删除评论
如果要删除所有评论,则无需在集合中逐一删除评论。您可以通过在评论集合上调用 Clear 方法来删除它们。
以下代码示例展示了如何删除文档中的所有注释:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
static void RemoveComments(Document doc) | |
{ | |
// Collect all comments in the document | |
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true); | |
// Remove all comments. | |
comments.Clear(); | |
} |
当您需要有选择地删除评论时,该过程变得与我们用于评论提取的代码更加相似。
以下代码示例展示了如何删除指定作者的评论:
这里要强调的要点是 for 运算符的使用。与简单的提取不同,这里要删除一条评论。一个合适的技巧是从最后一个 Comment 向后迭代集合到第一个 Comment。这样做的原因是,如果从末尾开始向后移动,前面的项目的索引保持不变,并且您可以返回到集合中的第一项。
以下代码示例展示了注释提取和删除的方法:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithComments(); | |
string fileName = "TestFile.doc"; | |
// Open the document. | |
Document doc = new Document(dataDir + fileName); | |
// Extract the information about the comments of all the authors. | |
foreach (string comment in ExtractComments(doc)) | |
Console.Write(comment); | |
// Remove comments by the "pm" author. | |
RemoveComments(doc, "pm"); | |
Console.WriteLine("Comments from \"pm\" are removed!"); | |
// Extract the information about the comments of the "ks" author. | |
foreach (string comment in ExtractComments(doc, "ks")) | |
Console.Write(comment); | |
//Read the comment's reply and resolve them. | |
CommentResolvedandReplies(doc); | |
// Remove all comments. | |
RemoveComments(doc); | |
Console.WriteLine("All comments are removed!"); | |
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName); | |
// Save the document. | |
doc.Save(dataDir); |
如何删除 CommentRangeStart 和 CommentRangeEnd 之间的注释
使用 Aspose.Words,您还可以删除 CommentRangeStart 和 CommentRangeEnd 节点之间的注释。
以下代码示例显示如何删除 CommentRangeStart 和 CommentRangeEnd 之间的文本:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithComments(); | |
string fileName = "TestFile.doc"; | |
// Open the document. | |
Document doc = new Document(dataDir + fileName); | |
CommentRangeStart commentStart = (CommentRangeStart)doc.GetChild(NodeType.CommentRangeStart, 0, true); | |
CommentRangeEnd commentEnd = (CommentRangeEnd)doc.GetChild(NodeType.CommentRangeEnd, 0, true); | |
Node currentNode = commentStart; | |
Boolean isRemoving = true; | |
while (currentNode != null && isRemoving) | |
{ | |
if (currentNode.NodeType == NodeType.CommentRangeEnd) | |
isRemoving = false; | |
Node nextNode = currentNode.NextPreOrder(doc); | |
currentNode.Remove(); | |
currentNode = nextNode; | |
} | |
dataDir = dataDir + "RemoveRegionText_out.doc"; | |
// Save the document. | |
doc.Save(dataDir); |
添加或删除评论的回复
AddReply 方法添加对此评论的回复。请注意,由于现有的 Microsoft Office 限制,文档中仅允许 1 级回复。如果在现有 Reply 注释上调用此方法,将引发 InvalidOperationException 类型的异常。
您可以使用 RemoveReply 方法删除对此评论的指定回复。
以下代码示例展示了如何添加评论回复和删除评论回复:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
Comment comment = (Comment)doc.GetChild(NodeType.Comment, 0, true); | |
//Remove the reply | |
comment.RemoveReply(comment.Replies[0]); | |
//Add a reply to comment | |
comment.AddReply("John Doe", "JD", new DateTime(2017, 9, 25, 12, 15, 0), "New reply"); | |
dataDir = dataDir + "TestFile_Out.doc"; | |
// Save the document to disk. | |
doc.Save(dataDir); |
阅读评论的回复
Replies 属性返回 Comment 对象的集合,这些对象是指定注释的直接子级。
以下代码示例展示了如何迭代评论的回复并解决它们:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
static void CommentResolvedandReplies(Document doc) | |
{ | |
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true); | |
Comment parentComment = (Comment)comments[0]; | |
foreach (Comment childComment in parentComment.Replies) | |
{ | |
// Get comment parent and status. | |
Console.WriteLine(childComment.Ancestor.Id); | |
Console.WriteLine(childComment.Done); | |
// And update comment Done mark. | |
childComment.Done = true; | |
} | |
} |