Aspose.Word从word文档里提取图片

更新时间:2023-09-24 18:59:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Aspose.Word从word文档里提取图片

Aspose.Word是一款功能强大的word控件,可以对word文件进行创建、编辑、读取、修改、转换,合并,插入等操作,并且不需要安装任何第三方插件,下面我们简单介绍,Aspose.Word如果从word文件中提取图片并保存。控件中国网是Aspose.Word在中国地区的核心代理商 具体查看下面的代码: C#

public void ExtractImagesToFiles() {

Document doc = new Document(MyDir + \ NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true); int imageIndex = 0; foreach (Shape shape in shapes) {

if (shape.HasImage) {

string imageFileName = string.Format(

\

FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)); shape.ImageData.Save(MyDir + imageFileName); imageIndex++; } }

// Newer Microsoft Word documents (such as DOCX) may contain a different type of image container called DrawingML.

// Repeat the process to extract these if they are present in the loaded document. NodeCollection dmlShapes = doc.GetChildNodes(NodeType.DrawingML, true); foreach (DrawingML dml in dmlShapes) {

if (dml.HasImage) {

string imageFileName = string.Format(

\

FileFormatUtil.ImageTypeToExtension(dml.ImageData.ImageType)); dml.ImageData.Save(MyDir + imageFileName); imageIndex++; } } }

Visual Basic

Public Sub ExtractImagesToFiles()

Dim doc As New Document(MyDir & \

Dim shapes As NodeCollection = doc.GetChildNodes(NodeType.Shape, True) Dim imageIndex As Integer = 0 For Each shape As Shape In shapes If shape.HasImage Then

Dim imageFileName As String = String.Format(\imageIndex, FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)) shape.ImageData.Save(MyDir & imageFileName) imageIndex += 1 End If Next shape

' Newer Microsoft Word documents (such as DOCX) may contain a different type of image container called DrawingML.

' Repeat the process to extract these if they are present in the loaded document. Dim dmlShapes As NodeCollection = doc.GetChildNodes(NodeType.DrawingML, True) For Each dml As DrawingML In dmlShapes If dml.HasImage Then

Dim imageFileName As String = String.Format(\imageIndex, FileFormatUtil.ImageTypeToExtension(dml.ImageData.ImageType)) dml.ImageData.Save(MyDir & imageFileName) imageIndex += 1 End If Next dml End Sub

本文来源:https://www.bwwdw.com/article/2tsd.html

Top