POI读取PPT和pptx两种格式的坑

同时读取PPT和pptx的时候低版本会产生依赖冲突,解决了依赖冲突后又发现没了关键包导致读取失败。。。。。。。

解决方法:将两个依赖升到4.12,就行了。

真能折腾

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.1.2</version>
        </dependency>

读取ppt或者pptx的代码

package com.example.demobystudy.util;

import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class PPTXtoImageUtils {

    public static void main(String[] args) {
        String url =
//                "https://img.bseera.com/p/t100000764/202210201120116902W100000394.ppt";
        "https://img.bseera.com/p/t100000764/202210201143547531W100000394.pptx";
        List<String> strings = transPPTXToPic(url,
                "D:\\workPlace\\java\\BeiShi\\测试ppt转图片\\");
        System.out.println(strings);
    }

    /**
     * @Author: RedRush
     * @Date: 2022/6/29 22:32
     * @description: ppt/pptx 转换为图片
     */
    public static List<String> transPPTXToPic(String url,String localPath) {
        try {
            if (url.toUpperCase().endsWith(".PPTX")) {
                return transPPTXToPicByPPTX(url,localPath);
            } else if (url.toUpperCase().endsWith(".PPT")) {
                return transPPTToPicByPPt(url,localPath);
            } else {

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ArrayList<>();
    }

    // PPT输出为图片 hslf解析
    private static List<String> transPPTToPicByPPt(String url,String filePath) {

        List<String> outFileList = new ArrayList<>();
        // 生成输出
        String outRoot = filePath + url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf('.')) + File.separator;
        System.err.printf("图片输出路径为:%s\n", outRoot);
        // 不存在则创建文件夹
        mkdir(outRoot);

        try {
            // 读取ppt
            HSLFSlideShow ppt = new HSLFSlideShow(new URL(url).openStream());
            
            /*
             * 解析PPT基本内容
             * */
            Dimension sheet = ppt.getPageSize();
            int width = sheet.width, height = sheet.height;
            List<HSLFSlide> pages = ppt.getSlides();

            System.err.printf("ppt基本信息: 共%s页,尺寸: %s , %s", pages.size(), width, height);

            BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();
            int i = 1;
            // 逐页遍历
            for (HSLFSlide slide : pages) {
                // 清空画板
                graphics.setPaint(Color.white);
                graphics.fill(new Rectangle2D.Float(0, 0, width, height));
                slide.draw(graphics);
                String outFile = outRoot + i++ + ".png";
                outFileList.add(outFile);
                // 输出为图片
                File f = new File(outFile);
                System.out.printf("输出图片:%s\n", f.getAbsolutePath());
                FileOutputStream fos = new FileOutputStream(f);
                javax.imageio.ImageIO.write(img, "PNG", fos);
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return outFileList;
    }

    // PPTX输出为图片 xmls包解析
    private static List<String> transPPTXToPicByPPTX(String url,String filePath) {
        List<String> outFileList = new ArrayList<>();
        // 生成输出
        String outRoot = filePath + url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf('.')) + File.separator;
        System.err.printf("图片输出路径为:%s\n", outRoot);
        // 不存在则创建文件夹
        mkdir(outRoot);

        try {
            // 读取ppt
            XMLSlideShow ppt = new XMLSlideShow(new URL(url).openStream());


            /*
             * 解析PPT基本内容
             * */
            Dimension sheet = ppt.getPageSize();
            int width = sheet.width, height = sheet.height;
            int count = ppt.getSlides().size();
            System.err.printf("ppt基本信息: 共%s页,尺寸: %s , %s", count, width, height);


            BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();
            int i = 1;
            // 逐页遍历
            for (XSLFSlide shape : ppt.getSlides()) {

                // 清空画板
                graphics.setPaint(Color.white);
                graphics.fill(new Rectangle2D.Float(0, 0, width, height));
                shape.draw(graphics);
                String outFile = outRoot + i++ + ".png";
                outFileList.add(outFile);
                // 输出为图片
                File f = new File(outFile);
                System.out.printf("输出图片:%s\n", f.getAbsolutePath());
                FileOutputStream fos = new FileOutputStream(f);
                javax.imageio.ImageIO.write(img, "PNG", fos);
                fos.close();
            }
        } catch (Exception e) {
            System.err.println("======ppt转换异常");
            e.printStackTrace();
        }
        return outFileList;
    }

    // 生成文件夹
    private static void mkdir(String path) {
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
    }
}

maven查找依赖的地址:http://www.mvnrepository.com/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值