批注是作者或审阅者根据自己想法给文档添加的注释或说明,通过查看批注,可以更加详细地了解某些文字的内含意义。该文将详细介绍如何使用C#为Word文档添加,修改及删除批注。
插入批注
C#
//新建一个word文档对象并加载需要添加批注的word文档
Document document = new Document();
document.LoadFromFile("Test.docx", FileFormat.Docx2010);
//获取第一个section里的第一个段落以插入批注
Section section = document.Sections[0];
Paragraph paragraph = section.Paragraphs[0];
//插入批注
string str = "Spire.Doc功能简介";
Comment comment = paragraph.AppendComment(str);
comment.Format.Author = "E-iceblue";
comment.Format.Initial = "CM";
//保存文档
document.SaveToFile("AddComments.docx", FileFormat.Docx2010);
VB.NET
'新建一个word文档对象并加载需要添加批注的word文档
Dim document As Document = New Document
document.LoadFromFile("Test.docx", FileFormat.Docx2010)
'获取第一个section里的第一个段落以插入批注
Dim section As Section = document.Sections(0)
Dim paragraph As Paragraph = section.Paragraphs(0)
'插入批注
Dim str As String = "Spire.Doc功能简介"
Dim comment As Comment = paragraph.AppendComment(str)
comment.Format.Author = "E-iceblue"
comment.Format.Initial = "CM"
'保存文档
document.SaveToFile("AddComments.docx", FileFormat.Docx2010)
更改批注内容
C#
//新建一个word文档对象并加载需要修改批注的word文档
Document document = new Document();
document.LoadFromFile("AddComments.docx", FileFormat.Docx2010);
//修改批注内容
document.Comments[0].Body.Paragraphs[0].Replace("Spire.Doc功能简介", "修改批注内容", false, false);
//保存文档
document.SaveToFile("ModifiedComment.docx", FileFormat.Docx2010);
VB.NET
'新建一个word文档对象并加载需要修改批注的word文档
Dim document As Document = New Document
document.LoadFromFile("AddComments.docx", FileFormat.Docx2010)
'修改批注内容
document.Comments(0).Body.Paragraphs(0).Replace("Spire.Doc功能简介", "修改批注内容", false, false)
'保存文档
document.SaveToFile("ModifiedComment.docx", FileFormat.Docx2010)
删除批注
C#
Document document = new Document();
document.LoadFromFile("AddComments.docx", FileFormat.Docx2010);
//删除第一个批注
document.Comments.RemoveAt(0);
//删除所有批注
document.Comments.Clear();
document.SaveToFile("RemoveComment.docx", FileFormat.Docx2010);
VB.NET
Dim document As Document = New Document
document.LoadFromFile("AddComments.docx", FileFormat.Docx2010)
'删除第一个批注
document.Comments.RemoveAt(0)
'删除所有批注
document.Comments.Clear
document.SaveToFile("RemoveComment.docx", FileFormat.Docx2010)