700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Word控件Spire.Doc 【图像形状】教程(2) ;在 C# VB.NET 中从 Word 中提取图像

Word控件Spire.Doc 【图像形状】教程(2) ;在 C# VB.NET 中从 Word 中提取图像

时间:2024-03-31 23:48:59

相关推荐

Word控件Spire.Doc 【图像形状】教程(2) ;在 C# VB.NET 中从 Word 中提取图像

Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。在 C#、 中从 Word 中提取图像。

Spire.Doc 最新下载/product/3368/download

本指南中的解决方案演示了如何从现有 Word 文档中提取图像,并通过Spire.Doc for .NET将它们保存到 C# 和 中的指定路径。

图像是属于段落项的一种文档对象。Spire.Doc for .NET 提供了一个DocumentObject类来将图像存储在 Document 中。并且还提供了一个DocPicture类来获取和设置文档的图像。下载并安装 Spire.Doc for .NET。按照步骤从 Word 中提取图像。

获取文档中每个部分的每个段落。获取 Paragraph 中 ChildObjects 的每个 DocumentObject。如果获取的 DocumentObjectType 为 Picture,则初始化一个 DocPicture 类实例,并将 DocumentObject 赋值为该实例的值。通过调用 String.Format(String format, object arg0) 初始化 String 类实例以命名提取的图像而不是其原始名称调用 DocPictrue.Image.Save(String, ImageFormat) 方法保存图像。

【C#】

using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using System;namespace ExtractImage{class Program{static void Main(string[] args){//Load documentDocument document = new Document(@"E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx");int index = 0;//Get Each Section of Documentforeach (Section section in document.Sections){//Get Each Paragraph of Sectionforeach (Paragraph paragraph in section.Paragraphs){//Get Each Document Object of Paragraph Itemsforeach (DocumentObject docObject in paragraph.ChildObjects){//If Type of Document Object is Picture, Extract.if (docObject.DocumentObjectType == DocumentObjectType.Picture){DocPicture picture = docObject as DocPicture;//Name ImageString imageName = String.Format(@"images\Image-{0}.png", index);//Save Imagepicture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png);index++;}}}}}}}

【】

Imports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsNamespace ExtractImageClass ProgramPrivate Shared Sub Main(args As String())'Load documentDim document As New Document("E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx")Dim index As Integer = 0'Get Each Section of DocumentFor Each section As Section In document.Sections'Get Each Paragraph of SectionFor Each paragraph As Paragraph In section.Paragraphs'Get Each Document Object of Paragraph ItemsFor Each docObject As DocumentObject In paragraph.ChildObjects'If Type of Document Object is Picture, Extract.If docObject.DocumentObjectType = DocumentObjectType.Picture ThenDim picture As DocPicture = TryCast(docObject, DocPicture)'Name ImageDim imageName As [String] = [String].Format("images\Image-{0}.png", index)'Save Imagepicture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png)index += 1End IfNextNextNextEnd SubEnd ClassEnd Namespace

调试后,所有提取的图像都保存在指定路径中。打开目录,就会找到图片。

Spire.Doc 是一个易于使用的 Word 文档操作组件,允许开发人员在 C# 和 for .NET 中快速生成、编写、编辑和保存 Word(Word 97-、Word 、Word ), Silverlight 和 WPF。

以上便是如何在 Word 中插入图像(C#/),如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。