【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();
    }
相关推荐
花落人散处38 分钟前
SpringAI——接入高德MCP服务
java·后端
超浪的晨38 分钟前
Java 代理机制详解:从静态代理到动态代理,彻底掌握代理模式的原理与实战
java·开发语言·后端·学习·代理模式·个人开发
天天摸鱼的java工程师40 分钟前
🧠 MySQL 索引结构有哪些?优缺点是什么?【原理 + 场景实战】
java·后端·面试
java叶新东老师1 小时前
idea提交时忽略.class、.iml文件和文件夹或目录的方法
java·开发语言
飞翔的佩奇1 小时前
Java项目:基于SSM框架实现的社区团购管理系统【ssm+B/S架构+源码+数据库+毕业论文+答辩PPT+远程部署】
java·数据库·vue.js·毕业设计·mybatis·答辩ppt·社区团购
TDengine (老段)1 小时前
TDengine 转化函数 TO_TIMESTAMP 用户手册
java·大数据·数据库·物联网·时序数据库·tdengine·涛思数据
Warren981 小时前
Java Collections工具类
java·开发语言·笔记·python·学习·oracle·硬件工程
java叶新东老师2 小时前
CMakelists.txt 实现多级目录编译
java·服务器·数据库
_风不会停息2 小时前
JDK1.8升级 JDK21 实践踩坑
java
SimonKing2 小时前
一文搞定:SpringBoot集成语音识别模型FunASR
java·人工智能·后端