Microsoft Word 中的注释功能为人们提供了一种极好的方式,可以将他们的见解或意见添加到 Word 文档中,而无需更改或中断文档的内容。如果有人对文档发表了评论,即使他们没有同时查看文档,文档作者或其他用户也可以回复评论与他进行讨论。本文将演示如何使用 Spire.Doc for .NET 在 C# 和 VB.NET 中添加、回复或删除 Word 中的注释。
安装 Spire.Doc for .NET
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过 NuGet 安装。
PM> Install-Package Spire.Doc
为 Word 中的段落添加注释
Spire.Doc for .NET 提供了 Paragraph.AppendComment() 方法,用于向特定段落添加注释。以下是详细步骤:
- 初始化 Document 类的实例。
- 使用 Document.LoadFromFile() 方法加载 Word 文档。
- 通过 Document.Sections[int] 属性通过其索引访问文档中的特定节。
- 通过 Section.Paragraphs[int] 属性通过索引访问节中的特定段落。
- 使用 Paragraph.AppendComment() 方法向段落添加注释。
- 使用 Document.SaveToFile() 方法保存结果文档。
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace AddComments
{
internal class Program
{
static void Main(string[] args)
{
//初始化Document类的实例
Document document = new Document();
//加载Word文档
document.LoadFromFile(@"示例文档.docx");
//获取文档的第一节
Section section = document.Sections[0];
//获取章节中的第一段
Paragraph paragraph = section.Paragraphs[0];
//向段落添加注释
Comment comment = paragraph.AppendComment("此注释是使用Spire.Doc for.NET添加的。");
//设置注释作者
comment.Format.Author = "冰蓝科技";
comment.Format.Initial = "CM";
//保存结果文档
document.SaveToFile("将注释添加到段落.docx", FileFormat.Docx2013);
document.Close();
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace AddComments
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'初始化Document类的实例 Dim document As Document = New Document() '加载Word文档
document.LoadFromFile("示例文档.docx")
'获取文档的第一节 Dim section As Section = document.Sections(0) '获取章节中的第一段
Dim paragraph As Paragraph = section.Paragraphs(0)
'向段落添加注释 Dim comment As Comment = paragraph.AppendComment("此注释是使用Spire.Doc for.NET添加的。") '设置注释作者
comment.Format.Author = "冰蓝科技"
comment.Format.Initial = "CM"
'保存结果文档 document.SaveToFile("将注释添加到段落.docx",FileFormat.Docx2013) document.Close() End Sub End Class End Namespace
为 Word 文档中的第一个查找结果添加注释
Paragraph.AppendComment()方法用于向整个段落添加注释。默认情况下,注释标记将放置在段落末尾。如果要向特定文本添加注释,可以使用 Document.FindString()方法搜索文本,该方法会返回第一个查找对象。当您设置好注释的内容后,再将注释标记放置在文本的开头和结尾。以下是详细步骤:
- 初始化 Document类的实例。
- 使用 Document.LoadFromFile()方法加载 Word 文档。
- 使用 Document.FindString()方法查找文档中的特定文本,并返回第一个匹配项。
- 创建一个注释开始标记和一个注释结束标记。
- 初始化 Comment类的实例以创建新注释,然后设置注释的内容和作者。
- 获取找到的第一个匹配文本的所有者段落,然后将注释作为子对象添加到该段落中。
- 在该匹配文本的范围之前插入注释开始标记,在其后插入注释结束标记。
- 使用 Document.SaveToFile()方法保存结果文档。
- C#
- VB.NET
using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;namespace AddCommentsToText{internal class Program{static void Main(string[] args){//初始化Document类的实例 Document document = new Document();//加载 Word 文档 document.LoadFromFile("示例文档.docx");//查找特定字符串 TextSelection find = document.FindString("Microsoft Office",false,true);//创建注释开始标记和注释结束标记 CommentMark commentmarkStart = new CommentMark(document);commentmarkStart.Type = CommentMarkType.CommentStart;CommentMark commentmarkEnd = new CommentMark(document);commentmarkEnd.Type = CommentMarkType.CommentEnd;//创建批注并设置其内容和作者 Comment comment = new Comment(document);comment.Body.AddParagraph().Text = "由Microsoft开发.";comment.Format.Author = "张兰";//将找到的文本作为单个文本范围获取 TextRange range = find.GetAsOneRange();//获取文本范围的所有者段落 Paragraph para = range.OwnerParagraph;//在段落中添加注释 para.ChildObjects.Add(comment);//获取段落中文本范围的索引 int index = para.ChildObjects.IndexOf(range);// 为注释开始标记和注释结束标记设置注释id commentmarkStart.CommentId = comment.Format.CommentId;commentmarkEnd.CommentId = comment.Format.CommentId;//在文本范围前插入注释开始标记 para.ChildObjects.Insert(index,commentmarkStart);//在文本范围后插入注释结束标记 para.ChildObjects.Insert(index + 2,commentmarkEnd);//保存结果文档 document.SaveToFile("添加文本注释.docx",FileFormat.Docx2013);document.Close();}}}
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace AddCommentsToText Friend Class Program Private Shared Sub Main(ByVal args As String()) '初始化Document类的实例
Dim document As Document = New Document()
'加载 Word 文档 document.LoadFromFile("示例文档.docx") '查找特定字符串
Dim find As TextSelection = document.FindString("Microsoft Office", False, True)
'创建注释开始标记和注释结束标记 Dim commentmarkStart As CommentMark = New CommentMark(document) commentmarkStart.Type = CommentMarkType.CommentStart Dim commentmarkEnd As CommentMark = New CommentMark(document) commentmarkEnd.Type = CommentMarkType.CommentEnd '创建批注并设置其内容和作者
Dim comment As Comment = New Comment(document)
comment.Body.AddParagraph().Text = "由Microsoft开发."
comment.Format.Author = "张兰"
'将找到的文本作为单个文本范围获取 Dim range As TextRange = find.GetAsOneRange() '获取文本范围的所有者段落
Dim para As Paragraph = range.OwnerParagraph
'在段落中添加注释 para.ChildObjects.Add(comment) '获取段落中文本范围的索引
Dim index As Integer = para.ChildObjects.IndexOf(range)
'在文本范围前插入注释开始标记 para.ChildObjects.Insert(index,commentmarkStart) '在文本范围后插入注释结束标记
para.ChildObjects.Insert(index + 2, commentmarkEnd)
'保存结果文档 document.SaveToFile("添加文本注释.docx",FileFormat.Docx2013) document.Close() End Sub End Class End Namespace
给 Word 文档中所有查找结果添加注释
在 Word 文档中,有时需要为多个相同的关键词或短语添加批注,以提供额外说明或标注重要信息。本章节将介绍如何使用 Document.FindAllString()方法批量查找指定文本,并通过 Paragraph.ChildObjects.Insert()方法为所有匹配项添加注释,从而提高文档的可读性和标注效率。
- 创建 Document类的实例。
- 通过 Document.LoadFromFile()方法加载 Word 文档。
- 使用 Document.FindString()方法查找所有匹配的特定文本。
- 遍历所有匹配的文本,为每个匹配项:
- 创建注释开始标记和结束标记。
- 初始化 Comment实例,设置注释的内容和作者。
- 获取匹配文本的 TextRange及其所在段落,并确定其索引。
- 通过 Paragraph.ChildObjects.Insert()方法插入注释的起始、结束标记和注释对象。
- 使用 Document.SaveToFile()方法保存修改后的文档。
- C#
using Spire.Doc.Documents;using Spire.Doc.Fields;using Spire.Doc;namespace AddCommentsToAllText{internal class Program{static void Main(string[] args){// 初始化 Document 类的实例 Document doc = new Document();// 加载 Word 源文档 doc.LoadFromFile("/示例文档.docx");// 设置需要查找的文本 TextSelection[] finds = doc.FindAllString("Microsoft Word",false,true);for (int i = 0;i <finds.Length;i++){// 创建注释起始标记 CommentMark commentmarkStart = new CommentMark(doc);commentmarkStart.CommentId = i;commentmarkStart.Type = CommentMarkType.CommentStart;// 创建注释结束标记 CommentMark commentmarkEnd = new CommentMark(doc);commentmarkEnd.CommentId = i;commentmarkEnd.Type = CommentMarkType.CommentEnd;// 创建注释对象并设置ID Comment comment = new Comment(doc);comment.Format.CommentId = i;// 添加注释内容 comment.Body.AddParagraph().Text = "测试批注_" + i;// 添加注释的作者信息 comment.Format.Author = "冰蓝科技";// 获取查找到文本范围 TextRange range = finds[i].GetAsOneRange();// 获取包含该文本的段落 Paragraph paragraph = range.OwnerParagraph;// 获取文本范围在段落中的的索引 int index = paragraph.ChildObjects.IndexOf(range);// 插入注释起始和结束标记,以及注释的对象 paragraph.ChildObjects.Insert(index,commentmarkStart);paragraph.ChildObjects.Insert(index + 2,commentmarkEnd);paragraph.ChildObjects.Insert(index + 3,comment);}// 保存修改后的文档 doc.SaveToFile("/result.docx");}}}
回复 Word 中的注释
要向现有评论添加回复,可以使用 Comment.ReplyToComment()方法。以下是详细步骤:
- 初始化 Document类的实例。
- 使用 Document.LoadFromFile()方法加载 Word 文档。
- 通过 Document.Comments[int]属性获取文档中的特定注释。
- 初始化 Comment类的实例以创建新注释,然后设置评论的内容和作者。
- 使用 Comment.ReplyToComment()方法添加新注释作为对特定注释的回复。
- 使用 Document.SaveToFile()方法保存结果文档。
- C#
- VB.NET
using Spire.Doc;using Spire.Doc.Fields;namespace ReplyToComments{internal class Program{static void Main(string[] args){//初始化Document类的实例 Document document = new Document();//加载Word文档 document.LoadFromFile("将注释添加到段落.docx");//获取文档中的第一条注释 Comment comment1 = document.Comments[0];//创建新注释并指定其作者和内容 Comment replyComment1 = new Comment(document);replyComment1.Format.Author = "杨舒婷";replyComment1.Body.AddParagraph().AppendText("Spire.Doc是一个很棒的Word库.");//添加批注作为对第一条批注的回复 comment1.ReplyToComment(replyComment1);//保存结果文档 document.SaveToFile("回复注释.docx",FileFormat.Docx2013);document.Close();}}}
Imports Spire.Doc Imports Spire.Doc.Fields Namespace ReplyToComments Friend Class Program Private Shared Sub Main(ByVal args As String()) '初始化Document类的实例
Dim document As Document = New Document()
'加载Word文档 document.LoadFromFile("将注释添加到段落.docx") '获取文档中的第一条注释
Dim comment1 As Comment = document.Comments(0)
'创建新注释并指定其作者和内容 Dim replyComment1 As Comment = New Comment(document) replyComment1.Format.Author = "杨舒婷" replyComment1.Body.AddParagraph().AppendText("Spire.Doc是一个很棒的Word库.") '添加批注作为对第一条批注的回复
comment1.ReplyToComment(replyComment1)
'保存结果文档 document.SaveToFile("回复注释.docx",FileFormat.Docx2013) document.Close() End Sub End Class End Namespace
删除 Word 中的注释
Spire.Doc for .NET 提供 Document.Comments.RemoveAt(int)方法从 Word 文档中删除特定注释,以及 Document.Commons.Clear()方法从 Word 文档中删除所有注释。以下是详细步骤:
- 初始化 Document类的实例。
- 使用 Document.LoadFromFile()方法加载 Word 文档。
- 使用 Document.Comments.RemoveAt(int)方法或 Document.Comments.Clear()方法删除文档中的特定注释或所有注释。
- 使用 Document.SaveToFile()方法保存结果文档。
- C#
- VB.NET
using Spire.Doc;namespace DeleteComments{internal class Program{static void Main(string[] args){//初始化Document类的实例 Document document = new Document();//加载Word文档 document.LoadFromFile(@"将注释添加到段落.docx");//删除文档中的第一条注释 document.Comments.RemoveAt(0);//删除文档中的所有注释 //document.Comments.Clear();//保存结果文档 document.SaveToFile("删除注释.docx",FileFormat.Docx2013);document.Close();}}}
Imports Spire.Doc Namespace DeleteComments Friend Class Program Private Shared Sub Main(ByVal args As String()) '初始化Document类的实例
Dim document As Document = New Document()
'加载Word文档 document.LoadFromFile("将注释添加到段落.docx") '删除文档中的第一条注释
document.Comments.RemoveAt(0)
'删除文档中的所有注释 'document.Comments.Clear();
'保存结果文档 document.SaveToFile("删除注释.docx",FileFormat.Docx2013) document.Close() End Sub End Class End Namespace
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。