C#/VB.NET 操作Word批注(二)——如何插入图片、读取、回复Word批注内容
2021-03-29 20:28
标签:col text comment target ocp png from 第一个 docx 在前面的文章C# 如何插入、修改、删除Word批注一文中介绍了如何操作Word批注的一些方法,在本篇文章中继续介绍操作Word批注的方法。分以下三种情况来介绍: 1. 插入图片到Word批注 2. 读取Word批注 3. 回复Word批注 PS:下载安装Free Spire.Doc 后,注意在你的程序中添加引用Spire.Doc.dll(dll文件可以在安装路径下的Bin文件夹中获取) 步骤 1:添加using指令 步骤 2:创建文档,加载测试文件 步骤 3 :获取段落 步骤 4 :添加文本、图片到批注 步骤 5 :保存文件 测试结果: C#全部代码: VB.NET代码: 步骤 1 :添加using指令 步骤 2 :创建实例,加载文档 步骤 3 :将批注内容写入Txt文档 C# 全部代码: VB.NET 代码 步骤 1 :添加using指令 步骤 2 :创建实例 步骤 3 :获取批注 步骤 4 :回复批注 步骤 5 :保存文件 测试结果: C# 全部代码: VB.NET 代码: 以上为本次关于操作Word批注的全部内容。 (本文完) 如需转载,请注明出处。 C#/VB.NET 操作Word批注(二)——如何插入图片、读取、回复Word批注内容 标签:col text comment target ocp png from 第一个 docx 原文地址:https://www.cnblogs.com/Yesi/p/9289108.html序
所需工具
示例代码
1. 插入图片到Word批注
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
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 = 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");
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");
}
}
}
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批注
using System.Text;
using System.IO;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
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");
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");
}
}
}
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批注内容
using Spire.Doc;
using Spire.Doc.Fields;
Document doc = new Document();
doc.LoadFromFile("test.docx");
Comment comment = doc.Comments[0];
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");
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");
}
}
}
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
文章标题:C#/VB.NET 操作Word批注(二)——如何插入图片、读取、回复Word批注内容
文章链接:http://soscw.com/index.php/essay/69679.html