C# /VB.NET 插入、修改、删除Word批注

880阅读 0评论2018-06-27 Mia张
分类:C#/.net

批注内容可以是对某段文字或内容的注释,也可以是对文段中心思想的概括提要,或者是对文章内容的评判、疑问,以及在阅读时给自己或他人起到提示作用。本篇文章中将介绍如何在C#中操作Word批注,主要包含以下要点:

使用工具Free Spire.Doc for .NET 6.3(最新社区版)

注:编辑代码前注意添加引用Sprie.Doc.dll(dll文件可在安装路径下的Bin文件夹中获取)

1.插入Word批注

C#


  1. using Spire.Doc;
  2. using Spire.Doc.Documents;
  3. using Spire.Doc.Fields;

  4. namespace InsertComment_Word
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //实例化一个Document类对象,并加载Word文档
  11.             Document document = new Document();
  12.             document.LoadFromFile("sample.docx");

  13.             //获取第一段第一节
  14.             Section section = document.Sections[0];
  15.             Paragraph paragraph = section.Paragraphs[0];

  16.             //添加文本到批注
  17.             string str = "This paragraph describes the origin and the purpose of WEF";
  18.             Comment comment = paragraph.AppendComment(str);
  19.             //添加批注作者
  20.             comment.Format.Author = "E-iceblue";
  21.           
  22.             //保存并打开文档
  23.             document.SaveToFile("Comments.docx", FileFormat.Docx2010);
  24.             System.Diagnostics.Process.Start("Comments.docx");
  25.         }
  26.     }
  27. }

VB.NET

  1. Imports Spire.Doc
  2. Imports Spire.Doc.Documents
  3. Imports Spire.Doc.Fields

  4. Namespace InsertComment_Word
  5.     
  6.     Class Program
  7.         
  8.         Private Shared Sub Main(ByVal args() As String)
  9.             '实例化一个Document类对象,并加载Word文档
  10.             Dim document As Document = New Document
  11.             document.LoadFromFile("sample.docx")
  12.             '获取第一段第一节
  13.             Dim section As Section = document.Sections(0)
  14.             Dim paragraph As Paragraph = section.Paragraphs(0)
  15.             '添加文本到批注
  16.             Dim str As String = "This paragraph describes the origin and the purpose of WEF"
  17.             Dim comment As Comment = paragraph.AppendComment(str)
  18.             '添加批注作者
  19.             comment.Format.Author = "E-iceblue"
  20.             '保存并打开文档
  21.             document.SaveToFile("Comments.docx", FileFormat.Docx2010)
  22.             System.Diagnostics.Process.Start("Comments.docx")
  23.         End Sub
  24.     End Class
  25. End Namespace
测试结果:


2.修改、删除批注

测试文档:


C#

  1. using Spire.Doc;

  2. namespace ReplaceAndRemoveComment_Word
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             //初始化Document类实例,加载带有批注的Word文档
  9.             Document document = new Document();
  10.             document.LoadFromFile("test.docx");

  11.             //修改第一个批注内容
  12.             document.Comments[0].Body.Paragraphs[0].Replace("This paragraph describes the origin and the purpose of WEF", "What is the WEF ?", false, false);

  13.             //移除第二个批注
  14.             document.Comments.RemoveAt(1);

  15.             //保存并打开文档
  16.             document.SaveToFile("RemoveAndReplace.docx", FileFormat.Docx);
  17.             System.Diagnostics.Process.Start("RemoveAndReplace.docx");
  18.         }
  19.     }
  20. }
VB.NET

  1. Imports Spire.Doc

  2. Namespace ReplaceAndRemoveComment_Word
  3.     
  4.     Class Program
  5.         
  6.         Private Shared Sub Main(ByVal args() As String)
  7.             '初始化Document类实例,加载带有批注的Word文档
  8.             Dim document As Document = New Document
  9.             document.LoadFromFile("test.docx")
  10.             '修改第一个批注内容
  11.             document.Comments(0).Body.Paragraphs(0).Replace("This paragraph describes the origin and the purpose of WEF", "What is the WEF ?", false, false)
  12.             '移除第二个批注
  13.             document.Comments.RemoveAt(1)
  14.             '保存并打开文档
  15.             document.SaveToFile("RemoveAndReplace.docx", FileFormat.Docx)
  16.             System.Diagnostics.Process.Start("RemoveAndReplace.docx")
  17.         End Sub
  18.     End Class
  19. End Namespace
测试结果:

以上是本次关于操作Word批注的全部内容。感谢浏览!

文章转载自:http://www.cnblogs.com/Yesi/p/8779294.html

上一篇:C#如何设置Excel文档保护——工作簿、工作表、单元格
下一篇:C# 如何合并、拆分Word文档