【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();
    }
相关推荐
路在脚下@1 小时前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql
森屿Serien1 小时前
Spring Boot常用注解
java·spring boot·后端
苹果醋32 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
Hello.Reader2 小时前
深入解析 Apache APISIX
java·apache
菠萝蚊鸭3 小时前
Dhatim FastExcel 读写 Excel 文件
java·excel·fastexcel
旭东怪3 小时前
EasyPoi 使用$fe:模板语法生成Word动态行
java·前端·word
007php0073 小时前
Go语言zero项目部署后启动失败问题分析与解决
java·服务器·网络·python·golang·php·ai编程
∝请叫*我简单先生3 小时前
java如何使用poi-tl在word模板里渲染多张图片
java·后端·poi-tl
ssr——ssss3 小时前
SSM-期末项目 - 基于SSM的宠物信息管理系统
java·ssm
一棵星3 小时前
Java模拟Mqtt客户端连接Mqtt Broker
java·开发语言