C#/VB.NET操作Word批注(二)——如何插入图片、读取、回复Word批注内容
序
在前面的文章C# 如何插入、修改、删除Word批注一文中介绍了如何操作Word批注的一些方法,在本篇文章中将继续介绍操作Word批注的方法。分以下三种情况来介绍:
创新互联网站建设由有经验的网站设计师、开发人员和项目经理组成的专业建站团队,负责网站视觉设计、用户体验优化、交互设计和前端开发等方面的工作,以确保网站外观精美、网站建设、网站制作易于使用并且具有良好的响应性。
- 插入图片到Word批注
- 读取Word批注
- 回复Word批注
所需工具
- Spire.Doc for .NET 6.3(社区版)
- Visual Stuido
示例代码
1. 插入图片到Word批注
【C#】
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace InsertImgToComment_Doc
{
class Program
{
static void Main(string[] args)
{
//实例化Document类,加载文档
Document doc = new Document();
doc.LoadFromFile("testfile.docx");
//获取需要添加批注的段落
Paragraph paragraph = doc.Sections[0].Paragraphs[2];
//添加文本批注内容、批注作者
Comment comment = paragraph.AppendComment("探索黑科技,小米为发烧而生!");
comment.Format.Author = "Administor";
//实例化DocPicture类,加载图片
DocPicture docPicture = new DocPicture(doc);
Image img = Image.FromFile("mi.png");
docPicture.LoadImage(img);
//插入图片到批注
comment.Body.AddParagraph().ChildObjects.Add(docPicture);
//保存文件并打开文档
doc.SaveToFile("result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
}
}
}
【VB.NET】
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing
Namespace InsertImgToComment_Doc
Class Program
Private Shared Sub Main(ByVal args As String())
Dim doc As Document = New Document()
doc.LoadFromFile("testfile.docx")
Dim paragraph As Paragraph = doc.Sections(0).Paragraphs(2)
Dim comment As Comment = paragraph.AppendComment("探索黑科技,小米为发烧而生!")
comment.Format.Author = "Administor"
Dim docPicture As DocPicture = New DocPicture(doc)
Dim img As Image = Image.FromFile("mi.png")
docPicture.LoadImage(img)
comment.Body.AddParagraph().ChildObjects.Add(docPicture)
doc.SaveToFile("result.docx", FileFormat.Docx2013)
System.Diagnostics.Process.Start("result.docx")
End Sub
End Class
End Namespace
测试下效果:
2.读取Word批注内容
【C#】
using System.Text;
using System.IO;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace ExtractComments
{
class Program
{
static void Main(string[] args)
{
//创建实例,加载文档
Document doc = new Document();
doc.LoadFromFile("test.docx");
//实例化StringBuilder类
StringBuilder SB = new StringBuilder();
//遍历所有word批注,将批注内容写入Txt文档
foreach (Comment comment in doc.Comments)
{
foreach (Paragraph p in comment.Body.Paragraphs)
{
SB.AppendLine(p.Text);
}
}
File.WriteAllText("CommentExtraction.txt", SB.ToString());
System.Diagnostics.Process.Start("CommentExtraction.txt");
}
}
}
【VB.NET】
Imports System.Text
Imports System.IO
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace ExtractComments
Class Program
Private Shared Sub Main(ByVal args As String())
Dim doc As Document = New Document()
doc.LoadFromFile("test.docx")
Dim SB As StringBuilder = New StringBuilder()
For Each comment As Comment In doc.Comments
For Each p As Paragraph In comment.Body.Paragraphs
SB.AppendLine(p.Text)
Next
Next
File.WriteAllText("CommentExtraction.txt", SB.ToString())
System.Diagnostics.Process.Start("CommentExtraction.txt")
End Sub
End Class
End Namespace
读取结果:
3.回复Word批注
【C#】
using Spire.Doc;
using Spire.Doc.Fields;
namespace ReplyComment_Doc
{
class Program
{
static void Main(string[] args)
{
//实例化Document类,加载文件
Document doc = new Document();
doc.LoadFromFile("test.docx");
//获取第一个批注
Comment comment = doc.Comments[0];
//实例化Comment类,添加批注回复作者以及回复内容
Comment replyComment = new Comment(doc);
replyComment.Format.Author = "Adam";
replyComment.Body.AddParagraph().AppendText("这条批注内容请再丰富一下,内容有些单调");
comment.ReplyToComment(replyComment);
//保存文件并打开
doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("ReplyToComment.docx");
}
}
}
【VB.NET】
Imports Spire.Doc
Imports Spire.Doc.Fields
Namespace ReplyComment_Doc
Class Program
Private Shared Sub Main(ByVal args As String())
Dim doc As Document = New Document()
doc.LoadFromFile("test.docx")
Dim comment As Comment = doc.Comments(0)
Dim replyComment As Comment = New Comment(doc)
replyComment.Format.Author = "Adam"
replyComment.Body.AddParagraph().AppendText("这条批注内容请再丰富一下,内容有些单调")
comment.ReplyToComment(replyComment)
doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013)
System.Diagnostics.Process.Start("ReplyToComment.docx")
End Sub
End Class
End Namespace
测试结果:
需要注意的是,在回复Word批注这个功能上,免费版的Spire.Doc暂不支持,须使用商业版。
以上为本次关于操作Word批注的全部内容。
(本文完)
如需转载,请注明出处。
本文名称:C#/VB.NET操作Word批注(二)——如何插入图片、读取、回复Word批注内容
URL分享:http://ybzwz.com/article/iiisih.html