【Java】使用Apache POI识别PPT中的图片和文字,以及对应的大小、坐标、颜色、字体等

本文介绍如何使用Apache POI识别PPT中的图片和文字,获取图片的数据、大小、尺寸、坐标,以及获取文字的字体、大小、颜色、坐标。

官方文档:https://poi.apache.org/components/slideshow/xslf-cookbook.html

官方文档和网上的资料介绍的很少,很多元素,需要一点点尝试才能获取到。

注意:本篇文章针对.pptx结尾的PPT文件

引入依赖:

xml 复制代码
 		<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.0.0</version>
        </dependency>

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

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

核心代码:

java 复制代码
	private static final String FILE_PATH = "/Temp/PPT/test.pptx";
    private static final String OUTPUT_PATH = "/Temp/PPT/output/";
    private static final String RGBA_TEMPLATE = "rgba(%d,%d,%d,1)";

    public static void main(String[] args) {
        analysisPpt(FILE_PATH);
    }

    public static void analysisPpt(String filePath) {
        try {
            InputStream input = new FileInputStream(filePath);
            XMLSlideShow xss = new XMLSlideShow(input);
            //getSlides(); 返回幻灯片中找到的所有普通幻灯片
            List<XSLFSlide> xslfSlideList = xss.getSlides();
            for (XSLFSlide xslfSlide : xslfSlideList) {
                //HSLFShape表示工作表中包含的所有形状(幻灯片或注释)
                List<XSLFShape> shapes = xslfSlide.getShapes();
                handleShapes(shapes);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void handleShapes(final List<XSLFShape> shapes) throws Exception {
        for (XSLFShape shape : shapes) {
            if (shape instanceof XSLFGroupShape) {
                XSLFGroupShape groupShape = ((XSLFGroupShape) shape);
                // 对XSLFGroupShape进行递归处理
                handleShapes(groupShape.getShapes());
            } else if (shape instanceof XSLFTextShape) {
                XSLFTextShape xslfTextShape = ((XSLFTextShape) shape);
                List<XSLFTextParagraph> textParagraphs = xslfTextShape.getTextParagraphs();
                XSLFTextRun textRun = textParagraphs.get(0).getTextRuns().get(0);
                Rectangle2D anchor = xslfTextShape.getAnchor();
                PaintStyle fontColor = textRun.getFontColor();
                Color color = null;
                if (Objects.nonNull(fontColor)) {
                    if (fontColor instanceof PaintStyle.SolidPaint) {
                        PaintStyle.SolidPaint solidPaint = (PaintStyle.SolidPaint) fontColor;
                        color = solidPaint.getSolidColor().getColor();
                    } else if (fontColor instanceof XSLFTexturePaint) {
                        // 一些大标题是该类型,暂无法获取大标题文字颜色
                        XSLFTexturePaint texturePaint = (XSLFTexturePaint) fontColor;
                        System.out.println("todo: XSLFTexturePaint ");
                    } else {
                        System.out.println("not match: " + fontColor.getClass());
                    }
                }
                String fill = "";
                if (Objects.nonNull(color)) {
                    fill = String.format(RGBA_TEMPLATE, color.getRed(), color.getGreen(), color.getBlue());
                }
                System.out.printf("[text]: %s \n[font]: %s [size]: %s [x,y]: (%s,%s) [color]: %s \n", xslfTextShape.getText(),
                        textRun.getFontFamily(), textRun.getFontSize(), anchor.getX(), anchor.getY(), fill);
                System.out.println("----------------------------");
            } else if (shape instanceof XSLFPictureShape) {
                XSLFPictureShape xslfPictureShape = ((XSLFPictureShape) shape);
                XSLFPictureData pictureData = xslfPictureShape.getPictureData();
                // 图片数据
                byte[] data = pictureData.getData();
                savePicture(data, pictureData.getFileName());
                Dimension dimensionInPixels = pictureData.getImageDimensionInPixels();
                Rectangle2D anchor = xslfPictureShape.getAnchor();
                System.out.printf("[picture name]: %s: [size]: %s * %s [X,Y]: (%s,%s) \n", pictureData.getFileName(), dimensionInPixels.getWidth(),
                        dimensionInPixels.getHeight(), anchor.getX(), anchor.getY());
            } else {
                System.out.println("unknown shape:" + shape.getClass());
            }
        }
    }

    private static void savePicture(final byte[] data, final String fileName) throws IOException {
        FileOutputStream out = new FileOutputStream(OUTPUT_PATH + fileName);
        out.write(data);
        out.close();
    }
相关推荐
华仔啊3 小时前
挖到了 1 个 Java 小特性:var,用完就回不去了
java·后端
SimonKing4 小时前
SpringBoot整合秘笈:让Mybatis用上Calcite,实现统一SQL查询
java·后端·程序员
日月云棠20 小时前
各版本JDK对比:JDK 25 特性详解
java
用户83071968408220 小时前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
JavaGuide21 小时前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
IT探险家21 小时前
Java 基本数据类型:8 种原始类型 + 数组 + 6 个新手必踩的坑
java
花花无缺21 小时前
搞懂new 关键字(构造函数)和 .builder() 模式(建造者模式)创建对象
java
用户9083246027321 小时前
Spring Boot + MyBatis-Plus 多租户实战:从数据隔离到权限控制的完整方案
java·后端
桦说编程1 天前
实战分析 ConcurrentHashMap.computeIfAbsent 的锁冲突问题
java·后端·性能优化
程序员清风1 天前
用了三年AI,我总结出高效使用AI的3个习惯!
java·后端·面试