成都网站建设设计

将想法与焦点和您一起共享

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

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

创新互联建站技术团队十载来致力于为客户提供成都做网站、成都网站制作、高端网站设计全网营销推广、搜索引擎SEO优化等服务。经过多年发展,公司拥有经验丰富的技术团队,先后服务、推广了近千家网站,包括各类中小企业、企事单位、高校等机构单位。
  • 插入Word批注
  • 修改Word批注
  • 删除Word批注
    使用工具:Free Spire.Doc for .NET 6.3(最新社区版)
    :编辑代码前注意添加引用Sprie.Doc.dll(dll文件可在安装路径下的Bin文件夹中获取)

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

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

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

2.修改、删除Word批注

测试文档:
C# /VB.NET 操作Word (一)——插入、修改、删除Word批注
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)         
            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

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

以上是本次关于C#、VB.NET操作word批注的全部内容,感谢阅读!
欢迎转载,转载请注明出处。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享标题:C#/VB.NET操作Word(一)——插入、修改、删除Word批注-创新互联
本文路径:http://chengdu.cdxwcx.cn/article/gddci.html