zoukankan      html  css  js  c++  java
  • C#/VB.NET 操作Word批注(二)——如何插入图片、读取、回复Word批注内容

    在前面的文章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[0].Paragraphs[2];

    步骤 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[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");
            }
        }
    }
    View Code

    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
    View Code

    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");
            }
        }
    }
    View Code

    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
    View Code

    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[0];

    步骤 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[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");
            }
        }
    }
    View Code

    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
    View Code

    以上为本次关于操作Word批注的全部内容。

    (本文完)

    如需转载,请注明出处。

  • 相关阅读:
    docker映射端口无法访问
    Tomcat日志分析
    linux服务器性能测试
    mysql库表结构对比工具
    网络基础知识,不懂看这里
    Python之redis、mysql进程守护
    linux文件查找工具
    nginx解决跨域问题
    redis插入单个较大的键值
    linux系统安全巡检脚本
  • 原文地址:https://www.cnblogs.com/Yesi/p/9289108.html
Copyright © 2011-2022 走看看