在某些情况下,您可能需要从 PowerPoint 演示文稿中提取图像以及文本。为此,本文介绍了如何以 Java 编程方式从 PowerPoint PPT 或 PPTX 中提取图像。您还将学习如何仅从 PPT 幻灯片中的背景或形状中提取图像。
Java API 从 PowerPoint PPT 中提取图像
Aspose.Slides for Java 是一种流行且功能丰富的 API,可让您无缝地创建和操作 PowerPoint 演示文稿。我们将使用此 API 从 PPT/PPTX 文件中提取图像。您可以 下载 API 的 JAR 或使用以下 Maven 配置安装它。
存储库:
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
依赖:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>22.1</version>
<classifier>jdk16</classifier>
</dependency>
从 Java 中的 PowerPoint PPT 中提取图像
在 PowerPoint 演示文稿中,幻灯片中使用的所有图像都存储在图像集中。可以访问此集合,并且可以将每个图像保存为文件。以下是用 Java 提取 PPT 演示文稿中所有图像的步骤。
- 首先,使用 Presentation 类加载 PPT/PPTX 演示文稿。
- 然后,使用 Presentation.getImages() 方法访问演示文稿中的图像集合。
- 最后,获取每个图像的类型和格式并保存。
以下代码示例显示了如何从 Java 中的 PowerPoint PPTX 文件中提取图像。
// 加载演示文稿
Presentation pres = new Presentation("presentation.pptx");
int imageIndex = 1;
String imageType = "";
String imagePath = "Image_";
// 循环播放图像
for (IPPImage image : pres.getImages()) {
// 获取图片类型
imageType = image.getContentType();
imageType = imageType.substring(imageType.indexOf("/") + 1, imageType.length());
// 保存图片
try {
ImageIO.write(image.getSystemImage(), imageType, new File(imagePath + imageIndex + "." + imageType.toString()));
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
imageIndex++;
}
从 Java 中的 PPTX 形状中提取图像
您也可以仅从 PPT 幻灯片中的形状中提取图像。以下是完成此操作的步骤。
- 首先,使用 Presentation 类加载演示文件。
- 然后,使用 Presentation.getSlides() 方法访问幻灯片集合。
- 对于每张幻灯片,使用 ISlide.getShapes() 方法访问其形状。
- 对集合中的每个形状执行以下步骤:
- 检查形状是否为自动形状并填充图片,然后使用 getImage() 方法提取其图像。
- 检查形状是否为相框,然后使用 getImage() 方法提取其图像。
- 最后,将图像保存为文件。
以下代码示例展示了如何使用 Java 从 PPT 中的形状中提取图像。
// 加载演示文稿
Presentation pres = new Presentation("presentation.pptx");
com.aspose.slides.IPPImage img = null;
int slideIndex = 0;
String imageType = "";
boolean isImageFound = false;
// 循环播放幻灯片
for (int i = 0; i < pres.getSlides().size(); i++) {
slideIndex++;
// 访问幻灯片
ISlide sl = pres.getSlides().get_Item(i);
for (int j = 0; j < sl.getShapes().size(); j++) {
// 访问形状
IShape sh = sl.getShapes().get_Item(j);
// 检查它是否是自动形状
if (sh instanceof IAutoShape) {
IAutoShape ashp = (IAutoShape) sh;
if (ashp.getFillFormat().getFillType() == FillType.Picture) {
img = ashp.getFillFormat().getPictureFillFormat().getPicture().getImage();
imageType = img.getContentType();
imageType = imageType.substring(0, imageType.indexOf("/") + 1);
isImageFound = true;
}
}
// 如果形状是相框
else if (sh instanceof IPictureFrame) {
IPictureFrame pf = (IPictureFrame) sh;
img = pf.getPictureFormat().getPicture().getImage();
imageType = img.getContentType();
imageType = imageType.substring(imageType.indexOf("/") + 1, imageType.length());
isImageFound = true;
}
// 设置所需的图片格式
if (isImageFound) {
try {
ImageIO.write(img.getSystemImage(), imageType,
new File("Slide_" + slideIndex + "_Shape_" + j + "." + imageType));
} catch (IO例外 ex) {
// 例外
}
}
isImageFound = false;
}
}
从 PPT 幻灯片背景中提取 Java 图像
另一种可能的情况是提取仅用作幻灯片背景的图像。以下步骤展示了如何在 Java 中提取幻灯片背景图像。
首先,使用 Presentation 类加载演示文稿。
然后,使用 Presentation.getSlides() 方法循环浏览演示文稿中的幻灯片。
对于每张幻灯片,请执行以下步骤:
使用 ISlide.getBackground().getFillFormat().getFillType() 方法检查幻灯片是否有背景图像。
如果背景有图片,则使用 getImage() 方法提取图像。
使用 ISlide.getLayoutSlide().getBackground().getFillFormat().getFillType() 方法检查布局幻灯片是否有背景图像。
如果布局幻灯片的背景填充了图片,则使用 getImage() 方法将其提取。
最后,将提取的图像保存为文件。
以下代码示例展示了如何从 Java PPT 中的幻灯片背景中提取图像。
// 加载演示文稿
Presentation pres = new Presentation("presentation.pptx");
com.aspose.slides.IPPImage img = null;
com.aspose.slides.IPPImage backImage = null;
String ImagePath = "BackImage_";
int slideIndex = 0;
String imageType = "";
for (int i = 0; i < pres.getSlides().size(); i++) {
slideIndex++;
// 访问幻灯片
ISlide sl = pres.getSlides().get_Item(i);
// 检查背景是否充满图片
if (sl.getBackground().getFillFormat().getFillType() == FillType.Picture) {
// 获取背景图片
backImage = sl.getBackground().getFillFormat().getPictureFillFormat().getPicture().getImage();
// 保存图片
BufferedImage image = backImage.getSystemImage();
imageType = backImage.getContentType();
imageType = imageType.substring(imageType.indexOf("/") + 1, imageType.length());
try {
ImageIO.write(image, imageType,
new File(ImagePath + "Slide_" + slideIndex + "." + imageType.toString()));
} catch (IOException ex) {
// 读取异常消息
}
} else {
// 检查幻灯片布局是否有图像背景
if (sl.getLayoutSlide().getBackground().getFillFormat().getFillType() == FillType.Picture) {
// 获取背景图片
backImage = sl.getLayoutSlide().getBackground().getFillFormat().getPictureFillFormat().getPicture()
.getImage();
// 保存图片
BufferedImage image = backImage.getSystemImage();
imageType = backImage.getContentType();
imageType = imageType.substring(imageType.indexOf("/") + 1, imageType.length());
try {
ImageIO.write(image, imageType,
new File(ImagePath + "LayoutSlide_" + slideIndex + "." + imageType.toString()));
} catch (IOException ex) {
// 读取异常消息
}
}
}
}
Java PowerPoint 图像提取 API - 获得免费许可证
Aspose 提供免费的临时许可证来使用 Aspose.Slides for Java,没有评估限制。你可以为自己买一个。
结论
在本文中,我们演示了如何在 Java 中从 PowerPoint PPT/PPTX 中提取图像。此外,我们还介绍了如何分别从形状或幻灯片背景中提取图像。此外,您可以通过访问 文档 了解更多关于 Aspose.Slides for Java 的信息。此外,您可以将您的查询发布到我们的 论坛。