C# /VB.NET操作Word批注(一)—— 插入、修改、删除Word批注

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

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

  • 插入Word批注
  • 修改Word批注
  • 删除Word批注

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

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

1.插入Word批注

C#

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

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

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

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

VB.NET

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace InsertComment_Word
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            '实例化一个Document类对象,并加载Word文档
            Dim document As Document = New Document
            document.LoadFromFile("sample.docx")
            '获取第一段第一节
            Dim section As Section = document.Sections(0)
            Dim paragraph As Paragraph = section.Paragraphs(0)
            '添加文本到批注
            Dim str As String = "This paragraph describes the origin and the purpose of WEF"
            Dim comment As Comment = paragraph.AppendComment(str)
            '添加批注作者
            comment.Format.Author = "E-iceblue"
            '保存并打开文档
            document.SaveToFile("Comments.docx", FileFormat.Docx2010)
            System.Diagnostics.Process.Start("Comments.docx")
        End Sub
    End Class
End Namespace

 

测试结果:

2.修改、删除批注

测试文档:

C#

using Spire.Doc;

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

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

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

            //保存并打开文档
            document.SaveToFile("RemoveAndReplace.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("RemoveAndReplace.docx");
        }
    }
}

 

VB.NET

Imports Spire.Doc

Namespace ReplaceAndRemoveComment_Word
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            '初始化Document类实例,加载带有批注的Word文档
            Dim document As Document = New Document
            document.LoadFromFile("test.docx")
            '修改第一个批注内容
            document.Comments(0).Body.Paragraphs(0).Replace("This paragraph describes the origin and the purpose of WEF", "What is the WEF ?", false, false)
            '移除第二个批注
            document.Comments.RemoveAt(1)
            '保存并打开文档
            document.SaveToFile("RemoveAndReplace.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("RemoveAndReplace.docx")
        End Sub
    End Class
End Namespace

 

测试结果:

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

目录
相关文章
|
15天前
|
数据可视化 网络协议 C#
C#/.NET/.NET Core优秀项目和框架2024年3月简报
公众号每月定期推广和分享的C#/.NET/.NET Core优秀项目和框架(每周至少会推荐两个优秀的项目和框架当然节假日除外),公众号推文中有项目和框架的介绍、功能特点、使用方式以及部分功能截图等(打不开或者打开GitHub很慢的同学可以优先查看公众号推文,文末一定会附带项目和框架源码地址)。注意:排名不分先后,都是十分优秀的开源项目和框架,每周定期更新分享(欢迎关注公众号:追逐时光者,第一时间获取每周精选分享资讯🔔)。
|
29天前
|
C# 开发工具 数据安全/隐私保护
C# 实现 Word 加盖骑缝章效果
C# 实现 Word 加盖骑缝章效果
|
29天前
|
SQL 安全 API
C# 读取Word表格到DataSet
C# 读取Word表格到DataSet
|
1月前
|
SQL 数据库 C#
C# .NET面试系列十一:数据库SQL查询(附建表语句)
#### 第1题 用一条 SQL 语句 查询出每门课都大于80 分的学生姓名 建表语句: ```sql create table tableA ( name varchar(10), kecheng varchar(10), fenshu int(11) ) DEFAULT CHARSET = 'utf8'; ``` 插入数据 ```sql insert into tableA values ('张三', '语文', 81); insert into tableA values ('张三', '数学', 75); insert into tableA values ('李四',
65 2
C# .NET面试系列十一:数据库SQL查询(附建表语句)
|
1月前
|
开发框架 算法 搜索推荐
C# .NET面试系列九:常见的算法
#### 1. 求质数 ```c# // 判断一个数是否为质数的方法 public static bool IsPrime(int number) { if (number < 2) { return false; } for (int i = 2; i <= Math.Sqrt(number); i++) { if (number % i == 0) { return false; } } return true; } class Progr
58 1
|
1月前
|
并行计算 安全 Java
C# .NET面试系列四:多线程
<h2>多线程 #### 1. 根据线程安全的相关知识,分析以下代码,当调用 test 方法时 i > 10 时是否会引起死锁? 并简要说明理由。 ```c# public void test(int i) { lock(this) { if (i > 10) { i--; test(i); } } } ``` 在给定的代码中,不会发生死锁。死锁通常是由于两个或多个线程互相等待对方释放锁而无法继续执行的情况。在这个代码中,只有一个线程持有锁,且没有其他线程参与,因此不
104 3
|
8天前
|
开发框架 前端开发 JavaScript
采用C#.Net +JavaScript 开发的云LIS系统源码 二级医院应用案例有演示
技术架构:Asp.NET CORE 3.1 MVC + SQLserver + Redis等 开发语言:C# 6.0、JavaScript 前端框架:JQuery、EasyUI、Bootstrap 后端框架:MVC、SQLSugar等 数 据 库:SQLserver 2012
|
29天前
|
存储 SQL 数据库
C# 将 Word 转文本存储到数据库并进行管理
C# 将 Word 转文本存储到数据库并进行管理
|
29天前
|
存储 SQL C#
C# 读取二维数组集合输出到Word预设表格
C# 读取二维数组集合输出到Word预设表格
|
29天前
|
SQL C# 数据库
C# 读取多条数据记录导出到 Word 标签模板
C# 读取多条数据记录导出到 Word 标签模板