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