从幻灯片形状中提取图像
Contents
[
Hide
]
图像通常被添加到形状中,并且经常用作幻灯片的背景。图像对象是通过 IImageCollection 添加的,它是 IPPImage 对象的集合。
本文解释了如何提取添加到演示文稿中的图像。
要从演示文稿中提取图像,您必须首先通过遍历每个幻灯片以及每个形状来定位图像。一旦找到或识别图像,您可以提取它并将其保存为新文件。
public void extractImages()
{
Presentation pres = new Presentation(folderPath + "ExtractImages.pptx");
com.aspose.slides.IPPImage img = null;
com.aspose.slides.IPPImage backImage = null;
int slideIndex = 0;
String imageType = "";
boolean ifImageFound = false;
for (int i = 0; i < pres.getSlides().size(); i++)
{
slideIndex++;
//访问第一张幻灯片
ISlide sl = pres.getSlides().get_Item(i);
//访问第一张幻灯片 Slide sl = pres.getSlideByPosition(i);
if (sl.getBackground().getFillFormat().getFillType() == FillType.Picture)
{
//获取背景图像
backImage = sl.getBackground().getFillFormat().getPictureFillFormat().getPicture().getImage();
imageType = getImageTType(backImage);
String imagePath = folderPath + "backImage_" + "Slide_" + slideIndex + "." + imageType;
//保存图像
backImage.getImage().save(imagePath, (int) ImageFormat.getValue(ImageFormat.class, capitalize(imageType)));
} else
{
if (sl.getLayoutSlide().getBackground().getFillFormat().getFillType() == FillType.Picture)
{
//获取背景图像
backImage = sl.getLayoutSlide().getBackground().getFillFormat().getPictureFillFormat().getPicture().getImage();
imageType = getImageTType(backImage);
String imagePath = folderPath + "backImage_" + "LayoutSlide_" + slideIndex + "." + imageType;
//保存图像
backImage.getImage().save(imagePath, (int) ImageFormat.getValue(ImageFormat.class, capitalize(imageType)));
}
}
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 = getImageTType(img);
ifImageFound = true;
}
} else if (sh instanceof IPictureFrame)
{
IPictureFrame pf = (IPictureFrame) sh;
img = pf.getPictureFormat().getPicture().getImage();
imageType = getImageTType(img);
ifImageFound = true;
}
//设置首选图像格式
if (ifImageFound)
{
String imagePath = folderPath + "backImage_" + "Slide_" + slideIndex + "_Shape_" + j + "." + imageType;
//保存图像
img.getImage().save(imagePath, (int) ImageFormat.getValue(ImageFormat.class, capitalize(imageType)));
}
ifImageFound = false;
}
}
}
private String getImageTType(IPPImage image)
{
String imageContentType = image.getContentType();
imageContentType = imageContentType.substring(imageContentType.indexOf("/") + 1);
imageContentType = imageContentType.substring(imageContentType.indexOf("-") + 1);
return imageContentType;
}
private String capitalize(String str)
{
if (str == null || str.length() <= 1) return str;
return str.substring(0, 1).toUpperCase() + str.substring(1);
}