本文介绍如何使用spire.presentation for java将zip文件嵌入powerpoint文档中。
import com.spire.presentation.*;
import com.spire.presentation.drawing.iimagedata;
import javax.imageio.imageio;
import java.awt.geom.rectangle2d;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.fileinputstream;
public class insertzip {
public static void main(string[] args) throws exception {
//创建presentation对象
presentation presentation = new presentation();
presentation.getslidesize().settype(slidesizetype.screen_16_x_9);
//获取第一个幻灯片
islide slide = presentation.getslides().get(0);
//加载一个zip文件并转换为byte[]对象
string filepath = "c:\\users\\administrator\\desktop\\sample.zip";
file zipfile = new file(filepath);
fileinputstream inputstream = new fileinputstream(zipfile);
byte[] data = new byte[(int) zipfile.length()];
inputstream.read(data, 0, data.length);
//加载一个图片作为zip文件的展示图片
file file = new file("c:\\users\\administrator\\desktop\\winrar-icon.png");
bufferedimage image = imageio.read(file);
iimagedata oleimage = presentation.getimages().append(image);
//将zip文件嵌入第一个幻灯片
rectangle2d rect = new rectangle2d.float(60, 60, image.getwidth(), image.getheight());
ioleobject oleobject = slide.getshapes().appendoleobject("zip", data, rect);
oleobject.getsubstituteimagepicturefillformat().getpicture().setembedimage(oleimage);
oleobject.setprogid("package");
//保存文档
presentation.savetofile("output/insertzip.pptx", fileformat.pptx_2013);
}
}