Java 在PPT中插入ZIP
程序员文章站
2022-03-01 20:34:33
...
本文通过Java代码示例展示如何在PPT幻灯片中插入ZIP压缩文件。此方法是通过将ZIP压缩文件作为OLE对象插入到PPT。下面是具体实现方法和步骤,供参考。
环境配置
- IntelliJ IDEA2018
- Windows10
- JDK1.8.0
- PPT jar包:Free Spire.Presentation for Java 3.9.0
- 测试文档:Power Point 2013 (.pptx)
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 AddZIP { 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[]对象 File zipFile = new File("sample.zip"); FileInputStream inputStream = new FileInputStream(zipFile); byte[] data = new byte[(int) zipFile.length()]; inputStream.read(data, 0, data.length); //加载一个图片作为zip文件的展示图片 File file = new File("logo.png"); BufferedImage image = ImageIO.read(file); IImageData oleImage = presentation.getImages().append(image); //将zip文件作为OLE对象嵌入幻灯片 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("InsertZip.pptx", FileFormat.PPTX_2013); presentation.dispose(); } }
—End—