图像通常用于表示 Word 文档中的重要信息。在文本旁边包含图像使内容更具吸引力。在某些情况下,您可能需要以编程方式提取嵌入在 Word 文档中的图像。为此,本文介绍了如何使用 Java 从 Word 文档中提取图像。
Aspose.Words for . java 最新下载(qun:761297826)
https://www.evget.com/product/4116/download
Java API 从 Word 文档中提取图像
Aspose.Words for Java是一个功能强大且功能丰富的 API,用于创建、操作和转换 MS Word 文档。因此,我们将使用此 API 从 MS Word DOCX/DOC 文档中提取图像。您可以使用以下 Maven 配置下载API 的 JAR 或将其安装到您的 Java 应用程序中。
AsposeJavaAPI Aspose Java API https://repository.aspose.com/repo/ com.aspose aspose-words 21.11 pom
如何从 Word 文档中提取图像
Word 文档中的图像使用形状对象表示。因此,要检索图像,您必须处理文档中的每个形状。以下是从 Java 中的 Word DOCX 文档中提取图像的步骤。
以下代码示例展示了如何从 Java 中的 DOCX 文档中提取图像。
// Load Word document
Document doc = new Document("Document.docx");
// Get all the shapes
NodeCollection shapes = (NodeCollection) doc.getChildNodes(NodeType.SHAPE, true);
int imageIndex = 0;
// Loop through the shape collection
for (Shape shape : shapes) {
// Check if shape has image
if (shape.hasImage()) {
// Extract and save the image
String imageFileName = String.format(
"Image.ExportImages.{0}_out_{1}", imageIndex, FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType()));
shape.getImageData().save(dataDir + imageFileName);
imageIndex++;
}
}
结论
在本文中,您学习了如何使用 Java 从 Word 文档中提取图像。此外,代码示例还展示了如何从 DOCX 文件中提取图像并将其保存到所需位置。此外,Aspose.Words for Java 提供了广泛的文档操作功能。此外,您可以通过我们的论坛提出您的问题也可以通过在线客服联系我们。