【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();
    }
相关推荐
非 白4 分钟前
【Java】代理模式
java·开发语言·代理模式
Good Note14 分钟前
Golang的静态强类型、编译型、并发型
java·数据库·redis·后端·mysql·面试·golang
我就是我3521 小时前
记录一次SpringMVC的406错误
java·后端·springmvc
向哆哆1 小时前
Java应用程序的跨平台性能优化研究
java·开发语言·性能优化
ekkcole2 小时前
windows使用命令解压jar包,替换里面的文件。并重新打包成jar包,解决Failed to get nested archive for entry
java·windows·jar
handsomestWei2 小时前
java实现多图合成mp4和视频附件下载
java·开发语言·音视频·wutool·图片合成视频·视频附件下载
全栈若城2 小时前
03 Python字符串与基础操作详解
java·开发语言·python
伯牙碎琴2 小时前
二、Spring Framework基础:IoC(控制反转)和DI(依赖注入)
java·spring·log4j
菲力蒲LY2 小时前
输入搜索、分组展示选项、下拉选取,全局跳转页,el-select 实现 —— 后端数据处理代码,抛砖引玉展思路
java·前端·mybatis
南宫生3 小时前
力扣每日一题【算法学习day.130】
java·学习·算法·leetcode