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

1. 插入图片到Word批注

2. 读取Word批注

3. 回复Word批注

所需工具

PS:下载安装Free Spire.Doc 后,注意在你的程序中添加引用Spire.Doc.dll(dll文件可以在安装路径下的Bin文件夹中获取)

示例代码

1. 插入图片到Word批注

步骤 1:添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

步骤 2:创建文档,加载测试文件

Document doc = new Document();
doc.LoadFromFile("testfile.docx");

步骤 3 :获取段落

Paragraph paragraph = doc.Sections[].Paragraphs[];

步骤 4 :添加文本、图片到批注

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);

步骤 5 :保存文件

doc.SaveToFile("result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");

测试结果:

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[].Paragraphs[]; //添加文本批注内容、批注作者
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().Paragraphs()
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批注

步骤 1 :添加using指令

using System.Text;
using System.IO;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

步骤 2 :创建实例,加载文档

Document doc = new Document();
doc.LoadFromFile("test.docx");

步骤 3 :将批注内容写入Txt文档

//实例化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");

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批注内容

步骤 1 :添加using指令

using Spire.Doc;
using Spire.Doc.Fields;

步骤 2 :创建实例

Document doc = new Document();
doc.LoadFromFile("test.docx");

步骤 3 :获取批注

Comment comment = doc.Comments[];

步骤 4 :回复批注

Comment replyComment = new Comment(doc);
replyComment.Format.Author = "Adam";
replyComment.Body.AddParagraph().AppendText("这条批注内容请再丰富一下,内容有些单调");
comment.ReplyToComment(replyComment);

步骤 5 :保存文件

doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("ReplyToComment.docx");

测试结果:

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[]; //实例化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()
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批注的全部内容。

(本文完)

如需转载,请注明出处。

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

  1. 自定义word快捷键,设置插入图片快捷键

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 自定义word快捷键,设置插入图片快捷键 文件→选项→自定义功能区 选择键盘快捷方式 自 ...

  2. C# 操作Word书签(二)——插入图片、表格到书签;读取、替换书签

    概要 书签的设置可以帮助我们快速的定位某段文字,使用起来很方便,也很节省时间.在前一篇文章“C# 如何添加/删除Word书签”中介绍了插入.删除书签的方法,本篇文章将对C# 操作Word书签的功能做进 ...

  3. 利用POI操作不同版本号word文档中的图片以及创建word文档

    我们都知道要想利用java对office操作最经常使用的技术就应该是POI了,在这里本人就不多说到底POI是什么和怎么用了. 先说本人遇到的问题,不同于利用POI去向word文档以及excel文档去写 ...

  4. 富文本编辑器UEditor自定义工具栏(二、插入图片、音频、视频个性化功能按钮和弹层及自定义分页符)

    导读:本篇将简单探讨插入图片.音频.视频的功能按钮实现方式 传送门:富文本编辑器UEditor自定义工具栏(一.基础配置与字体.背景色.行间距.超链接实现) 一.效果图 1.UEditor自定义工具栏 ...

  5. C#中按模板操作Word —— 如何向Word中插入图片

    一.Word对象模型的重叠性分析 本文主要介绍通过书签Bookmark向Word文档中插入图片的方法.在此之前我们先简单讨论下Word对象模型的重叠性.如果你对Word对象模型还不熟悉,请参考本专栏第 ...

  6. Java导出Word利用freemarker(含图片)

    制作Word模版 建议使用高版本的office做,尽量不要用WPS做,生成xml会出现乱码 编码要统一,推荐UTF-8 建好模板,将模板另存为xml格式,建议原来模板不要删,xml的如果后期打不开,还 ...

  7. word插入图片显示不完整的解决的方法

    有时在编写word文档,插入图片时,会出现图不完整的情况. 解决方法是:选中图片,段落格式设置为单位行距(不是22磅),图片格式为嵌入式.问题解决.

  8. C#&.Net干货分享-构造QRCoderHelper生成二维码图片

    不想说废话,直接源码拿去用... /// <summary>    /// 二维码管理    /// </summary>    public class QRCoderHel ...

  9. c# word 插入图片问题

    情景描述: 在之前文本框中加标签,代码直接addPicture出现了意外.不起作用,怀疑是文档模板的问题,因为生成的PDF和word格式总时不时有差异,左右捣鼓下,更换文本框,更换图片形式,形状形式, ...

随机推荐

  1. Java spring boot 2.0连接mysql异常:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone

    解决办法:application.yml提示信息表明数据库驱动com.mysql.jdbc.Driver'已经被弃用了.应当使用新的驱动com.mysql.cj.jdbc.Driver' com.my ...

  2. CF F. Shovels Shop(前缀和预处理+贪心+dp)

    F. Shovels Shop time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  3. sql 随机获取数据

    SQL Server: SELECT TOP 10 * FROM T_USER ORDER BY NEWID() ORACLE: SELECT * FROM (SELECT * FROM T_USER ...

  4. APP研发录笔记

    一.消灭全局变量 在内存不足时,系统会回收一部分闲置的资源,由于App被切换到后台,所以之前存放的全局变量很容易被回收,这时再切换到前台继续使用,会报空指针崩溃.想彻底解决这个问题,就要使用序列化. ...

  5. C++或C#调用外部exe的分析

    假如有个外部程序名为A.exe,放在目录E:\temp\下,然后我们用C++或者C#写一个程序调用这个A.exe的话(假设这个调用者所在的路径在D:\invoke),通常会采用下面的代码: // C# ...

  6. HTTP协议概念与特点,HTTP的状态码,HTTPS是什么?

    很多人在打开网页的时候,在浏览器地址栏里都会看到http  ,在Java WEB里,HTTP也是个重点内容,今天我们就来详细了解和学习HTTP . HTTP是Hyper Text Transfer P ...

  7. [Swift]LeetCode459. 重复的子字符串 | Repeated Substring Pattern

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  8. [Swift]LeetCode529. 扫雷游戏 | Minesweeper

    Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...

  9. [Swift]LeetCode798. 得分最高的最小轮调 | Smallest Rotation with Highest Score

    Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1 ...

  10. [Swift]LeetCode1008. 先序遍历构造二叉树 | Construct Binary Search Tree from Preorder Traversal

    Return the root node of a binary search tree that matches the given preorder traversal. (Recall that ...