本文介绍如何使用Spire.Presentation for Java获取指定幻灯片中的图片形状,并用新图片进行填充。
import com.spire.presentation.*;
import com.spire.presentation.drawing.IImageData;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
public class ReplaceImage {
public static void main(String[] args) throws Exception {
//创建Presentation对象
Presentation presentation= new Presentation();
//加载PowerPoint示例文档
presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");
//添加图片到图片集合
String imagePath = "C:\\Users\\Administrator\\Desktop\\Microsoft-PowerPoint-logo.jpg";
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(imagePath));
IImageData image = presentation.getImages().append(bufferedImage);
//获取第一张幻灯片上形状集合
ShapeCollection shapes = presentation.getSlides().get(0).getShapes();
//遍历所有形状
for (int i = 0; i < shapes.getCount(); i++) {
//判断形状是否是图片
if (shapes.get(i) instanceof SlidePicture) {
//将图片形状用新图片填充
((SlidePicture) shapes.get(i)).getPictureFill().getPicture().setEmbedImage(image);
}
}
//保存文档
presentation.saveToFile("output/ReplaceImage.pptx", FileFormat.PPTX_2013);
}
}