多语言展示
当前在线:1909今日阅读:84今日分享:32

C# 读取Word中批注中的图片

此经验分享的内容是通过C#编程来读取/提取Word批注中的图片。
工具/原料
1

Free Spire.Doc for .NET (免费版)

2

Visual Studio

dll引用
C#代码示例
1

测试文档中,Word批注里包含两张图片,通过以下代码将图片提取出来。

2

using System.Text;using System.IO;using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using System;namespace ExtractComments{    class Program    {        static void Main(string[] args)        {            //创建实例,加载文档            Document doc = new Document();            doc.LoadFromFile('test.docx');                        int index = 0;                       //遍历批注获取图片            foreach (Comment comment in doc.Comments)            {                foreach (Paragraph p in comment.Body.Paragraphs)                {                    foreach (DocumentObject docObject in p.ChildObjects)                    {                        if (docObject.DocumentObjectType == DocumentObjectType.Picture)                        {                            DocPicture picture = docObject as DocPicture;                            String imageName = String.Format('图片{0}.png', index);                            picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png);                            index++;                        }                    }                }            }        }    }}

3

图片提取结果:

推荐信息