为了账号安全,请及时绑定邮箱和手机立即绑定

Java 在PPT中插入ZIP

标签:
Java

本文通过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)

 ——————————————————————————————

导入jar包

    #1.手动导入

按照如下步骤将本地文件夹下的jar文件导入Java程序:

https://img2.sycdn.imooc.com/62e22e220001b7ff09710565.jpg

https://img2.sycdn.imooc.com/62e22e2c0001fb6b08830359.jpg

https://img1.sycdn.imooc.com/62e22e3a0001947506540541.jpg

      #2. Maven仓库下载导入

可参考配置如下pom.xml文件:

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories><dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.presentation.free</artifactId>
        <version>3.9.0</version>
    </dependency></dependencies>

—————————————————————————————

【Java】

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();
    }
}

执行代码,生成PPT幻灯片文档。在幻灯片中可查看插入的ZIP,效果如图:

https://img2.sycdn.imooc.com/62e22f140001d30b10120541.jpg


—END—



点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
9
获赞与收藏
48

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消