C#/VB.NET 操作Word批注(二)——如何插入图片、读取、回复Word批注内容

10阅读 0评论2018-07-10 Mia张
分类:C#/.net


在前面的文章C# 如何插入、修改、删除Word批注一文中介绍了如何操作Word批注的一些方法,在本篇文章中继续介绍操作Word批注的方法。分以下三种情况来介绍:

1. 插入图片到Word批注

2. 读取Word批注

3. 回复Word批注

所需工具


PS:注意在安装完成后,添加引用Spire.Doc.dll到程序里面。


示例代码

1. 插入图片到Word批注

C#


  1. using Spire.Doc;
  2. using Spire.Doc.Documents;
  3. using Spire.Doc.Fields;
  4. using System.Drawing;

  5. namespace InsertImgToComment_Doc
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //实例化Document类,加载文档
  12.             Document doc = new Document();
  13.             doc.LoadFromFile("testfile.docx");

  14.             //获取需要添加批注的段落
  15.             Paragraph paragraph = doc.Sections[0].Paragraphs[2];

  16.             //添加文本批注内容、批注作者
  17.             Comment comment = paragraph.AppendComment("探索黑科技,小米为发烧而生!");
  18.             comment.Format.Author = "Administor";
  19.            
  20.             //实例化DocPicture类,加载图片
  21.             DocPicture docPicture = new DocPicture(doc);
  22.             Image img = Image.FromFile("mi.png");
  23.             docPicture.LoadImage(img);
  24.             //插入图片到批注
  25.             comment.Body.AddParagraph().ChildObjects.Add(docPicture);

  26.             //保存文件并打开文档
  27.             doc.SaveToFile("result.docx", FileFormat.Docx2013);
  28.             System.Diagnostics.Process.Start("result.docx");
  29.         }
  30.     }
  31. }
测试效果:



【VB.NET】

  1. Imports Spire.Doc
  2. Imports Spire.Doc.Documents
  3. Imports Spire.Doc.Fields
  4. Imports System.Drawing

  5. Namespace InsertImgToComment_Doc
  6.     Class Program
  7.         Private Shared Sub Main(ByVal args As String())
  8.             Dim doc As Document = New Document()
  9.             doc.LoadFromFile("testfile.docx")
  10.             Dim paragraph As Paragraph = doc.Sections(0).Paragraphs(2)
  11.             Dim comment As Comment = paragraph.AppendComment("探索黑科技,小米为发烧而生!")
  12.             comment.Format.Author = "Administor"
  13.             Dim docPicture As DocPicture = New DocPicture(doc)
  14.             Dim img As Image = Image.FromFile("mi.png")
  15.             docPicture.LoadImage(img)
  16.             comment.Body.AddParagraph().ChildObjects.Add(docPicture)
  17.             doc.SaveToFile("result.docx", FileFormat.Docx2013)
  18.             System.Diagnostics.Process.Start("result.docx")
  19.         End Sub
  20.     End Class
  21. End Namespace

2.读取Word批注内容

C#


  1. using System.Text;
  2. using System.IO;
  3. using Spire.Doc;
  4. using Spire.Doc.Documents;
  5. using Spire.Doc.Fields;

  6. namespace ExtractComments
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             //创建实例,加载文档
  13.             Document doc = new Document();
  14.             doc.LoadFromFile("test.docx");

  15.             //实例化StringBuilder类
  16.             StringBuilder SB = new StringBuilder();
  17.             //遍历所有word批注,将批注内容写入Txt文档
  18.             foreach (Comment comment in doc.Comments)
  19.             {
  20.                 foreach (Paragraph p in comment.Body.Paragraphs)
  21.                 {
  22.                     SB.AppendLine(p.Text);
  23.                 }
  24.             }
  25.             File.WriteAllText("CommentExtraction.txt", SB.ToString());
  26.             System.Diagnostics.Process.Start("CommentExtraction.txt");
  27.         }
  28.     }
  29. }
读取结果:


【VB.NET】

  1. Imports System.Text
  2. Imports System.IO
  3. Imports Spire.Doc
  4. Imports Spire.Doc.Documents
  5. Imports Spire.Doc.Fields

  6. Namespace ExtractComments
  7.     Class Program
  8.         Private Shared Sub Main(ByVal args As String())
  9.             Dim doc As Document = New Document()
  10.             doc.LoadFromFile("test.docx")
  11.             Dim SB As StringBuilder = New StringBuilder()

  12.             For Each comment As Comment In doc.Comments

  13.                 For Each p As Paragraph In comment.Body.Paragraphs
  14.                     SB.AppendLine(p.Text)
  15.                 Next
  16.             Next

  17.             File.WriteAllText("CommentExtraction.txt", SB.ToString())
  18.             System.Diagnostics.Process.Start("CommentExtraction.txt")
  19.         End Sub
  20.     End Class
  21. End Namespace

3.回复Word批注

C#

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

  3. namespace ReplyComment_Doc
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             //实例化Document类,加载文件
  10.             Document doc = new Document();
  11.             doc.LoadFromFile("test.docx");

  12.             //获取第一个批注
  13.             Comment comment = doc.Comments[0];

  14.             //实例化Comment类,添加批注回复作者以及回复内容
  15.             Comment replyComment = new Comment(doc);
  16.             replyComment.Format.Author = "Adam";
  17.             replyComment.Body.AddParagraph().AppendText("这条批注内容请再丰富一下,内容有些单调");
  18.             comment.ReplyToComment(replyComment);

  19.             //保存文件并打开
  20.             doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013);
  21.             System.Diagnostics.Process.Start("ReplyToComment.docx");
  22.         }
  23.     }
  24. }
测试结果:

【VB.NET】

  1. Imports Spire.Doc
  2. Imports Spire.Doc.Fields

  3. Namespace ReplyComment_Doc
  4.     Class Program
  5.         Private Shared Sub Main(ByVal args As String())
  6.             Dim doc As Document = New Document()
  7.             doc.LoadFromFile("test.docx")
  8.             Dim comment As Comment = doc.Comments(0)
  9.             Dim replyComment As Comment = New Comment(doc)
  10.             replyComment.Format.Author = "Adam"
  11.             replyComment.Body.AddParagraph().AppendText("这条批注内容请再丰富一下,内容有些单调")
  12.             comment.ReplyToComment(replyComment)
  13.             doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013)
  14.             System.Diagnostics.Process.Start("ReplyToComment.docx")
  15.         End Sub
  16.     End Class
  17. End Namespace

以上为本次关于操作Word批注的全部内容。

(本文完)

如需转载,请注明出处。


上一篇:C# 如何合并、拆分Word文档
下一篇:C# 如何复制Word文档(复制整篇文档、复制指定段落、复制页眉页脚)