本文介绍如何使用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);
}
}