C# 插入图片到Word批注

2025-10-21 17:22:07

1、在E-iceblue官网或者Nuget网站上下载Free Spire.Doc for .NET的安装包后,注意在编辑代码时,添加引用Spire.Doc.dll到程序。dll文件可在安装路径下的Bin文件夹中获取。

C# 插入图片到Word批注

1、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("img1.png");

            docPicture.LoadImage(img);

            //插入图片到批注

            comment.Body.AddParagraph().ChildObjects.Add(docPicture);

            //保存文件并打开文档

            doc.SaveToFile("result.docx", FileFormat.Docx2013);

            System.Diagnostics.Process.Start("result.docx");

        }

    }

}

2、调试运行程序后,生辰文件,新的文档中,已插入了图片到批注

C# 插入图片到Word批注

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢