通过poi读取ppt元素demo

poi+Spire.Presentation for Java获取导入ppt元素

    最近有一个导入ppt,识别ppt内所有元素需求,翻了一些资料都没有特别好的demo,官方文档很官方...,所以打算自己写一个demo。
	poi 4.1,因为项目里之前引入的就是4.1所以就不用最新的了,poi不支持动画效果和保存视频,也有可能是我没找好方法。。
	官网:http://poi.apache.org/apidocs/4.1/
	Spire.Presentation for Java 付费插件用到了保存视频和读取动画效果两个功能
	官网:https://www.e-iceblue.cn/spirepresentationforjava/spire-presentation-for-java-program-guide-content-html.html

踩坑

  1. maven下载spire.presentation包不稳定,我把它放到了私有依赖库
  2. 因为ppt的页面大小和网页的大小可能不一样,会导致位置不对,所以要先获取比例
  3. 尽量输出png格式图片,镂空图片还没解决
  4. ppt和pptx是两套处理逻辑
  5. 文字多层样式多次处理
  6. 艺术字默认转换为图片
  7. ppt格式不支持视频、音频
  8. poi 没有视频对象 我是没找到。。 用spire.presentation获取的视频

具体功能

ppt有两种格式,ppt是2003年之前的版本,pptx是2007年之后的版本,两种版本需要分开处理,ppt里面不能放视频和音频,所以ppt只处理了母版元素,文字,图片,图形,组合图形,艺术字,pptx多处理了视频和音频。
2021-09-22 更新了ppt和pptx背景图片和背景颜色的获取方法

部分代码

// An highlighted block
public static void autoShapeProcess(HSLFShape shape,double pageWidthProportion,double pageHeightProportion,int i,Map<String,String> animationMap) {
        System.out.println("-------------图形处理-------------");
        String graphicType = getGraphicType(shape);
        HSLFAutoShape autoShape = (HSLFAutoShape) shape;
        Map<String,String> styleMap = new HashMap();
        //圆
        if("CIRCLE".equals(graphicType)){
            styleMap.put("width",autoShape.getAnchor().getWidth()*2 +"px");
            styleMap.put("height",autoShape.getAnchor().getHeight()*2 +"px");
            styleMap.put("left",autoShape.getAnchor().getMinX()/pageWidthProportion-20 +"px");
            styleMap.put("top",autoShape.getAnchor().getMinY()/pageHeightProportion-20 +"px");
            styleMap.put("cx",autoShape.getAnchor().getWidth() +"px");
            styleMap.put("cy",autoShape.getAnchor().getHeight() +"px");
            styleMap.put("rx",autoShape.getAnchor().getWidth() +"px");
            styleMap.put("ry",autoShape.getAnchor().getHeight() +"px");
        } else {
            styleMap.put("width",autoShape.getAnchor().getWidth()/pageWidthProportion +"px");
            styleMap.put("height",autoShape.getAnchor().getHeight()/pageHeightProportion +"px");
            styleMap.put("left",autoShape.getAnchor().getMinX()/pageWidthProportion +"px");
            styleMap.put("top",autoShape.getAnchor().getMinY()/pageHeightProportion +"px");
        }
        if(autoShape.getFill().getPictureData() != null){
            // 图形里面包含了一个图片 很坑找了半天图片为什么没显示
            // autoShape.getFill().getPictureData().getData() 为byte[] 须转换成file 获取file属性
            ByteArrayInputStream bais = new ByteArrayInputStream(autoShape.getFill().getPictureData().getData());
            BufferedImage bi1 = null;
            try {
                bi1 = ImageIO.read(bais);
                File w2 = new File("data/"+autoShape.getShapeName()+".png");//可以是jpg,png,gif格式
                ImageIO.write(bi1, "png", w2);//不管输出什么格式图片,此处不需改动
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("path");
            }
            Map<String,String> pictureMap = new HashMap();
            pictureMap.put("width",autoShape.getAnchor().getWidth()/pageWidthProportion +"px");
            pictureMap.put("height",autoShape.getAnchor().getHeight()/pageHeightProportion +"px");
            pictureMap.put("left",autoShape.getAnchor().getMinX()/pageWidthProportion +"px");
            pictureMap.put("top",autoShape.getAnchor().getMinY()/pageHeightProportion +"px");
            pictureMap.put("rorateX",(autoShape.getAnchor().getX())  + "px");
            pictureMap.put("rorateY",(autoShape.getAnchor().getY()) + "px");
            String css =JSON.toJSONString(pictureMap);
            System.out.println("图形类型:"+graphicType);
            System.out.println("图形图片样式:"+css);
            System.out.println("图形图片地址:"+"data/"+autoShape.getShapeName()+".png");
            return;
        }
        // 空白图形 标题一不处理返回
        if(autoShape.getFillColor() != null){
            styleMap.put("fill",getColor(autoShape.getFillColor().toString().split(",")));
        } else {
            return;
        }
        styleMap.put("z-index",i+"");
        styleMap.put("strokeWidth",autoShape.getLineWidth() +"px");
        if(autoShape.getLineColor() != null){
            styleMap.put("stroke", getColor(autoShape.getLineColor().toString().split(",")));
        }
        styleMap.put("strokeDasharray",autoShape.getStrokeStyle().getLineDash().toString());
        if(autoShape.getFillColor() != null && autoShape.getFillColor().getTransparency() == 3){
            styleMap.put("opacity",20*autoShape.getFillColor().getAlpha()/51+"");
        } else {
            styleMap.put("opacity",100+"");
        }
        System.out.println("图形类型:"+graphicType);
        System.out.println("图形样式:"+JSON.toJSONString(styleMap));
        System.out.println("图形动画效果:"+animationMap.get(autoShape.getShapeName()));
    }
public static void pictureProcess(HSLFShape shape,double pageWidthProportion,double pageHeightProportion,int i,Map<String,String> animationMap) {
        System.out.println("-------------图片处理-------------");
        HSLFPictureShape hslfPictureShape = (HSLFPictureShape) shape;
        ByteArrayInputStream bais = new ByteArrayInputStream(hslfPictureShape.getPictureData().getData());
        BufferedImage bi1 = null;
        try {
            bi1 = ImageIO.read(bais);
            File w2 = new File("data/"+hslfPictureShape.getShapeName()+".png");//可以是jpg,png,gif格式
            ImageIO.write(bi1, "png", w2);//不管输出什么格式图片,此处不需改动
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("path");
        }
        Map<String,String> pictureMap = new HashMap();
        pictureMap.put("width",hslfPictureShape.getAnchor().getWidth()/pageWidthProportion +"px");
        pictureMap.put("height",hslfPictureShape.getAnchor().getHeight()/pageHeightProportion +"px");
        pictureMap.put("left",hslfPictureShape.getAnchor().getMinX()/pageWidthProportion +"px");
        pictureMap.put("top",hslfPictureShape.getAnchor().getMinY()/pageHeightProportion +"px");
        pictureMap.put("rorateX",(hslfPictureShape.getAnchor().getX())  + "px");
        pictureMap.put("rorateY",(hslfPictureShape.getAnchor().getY()) + "px");
        String css =JSON.toJSONString(pictureMap);
        System.out.println("图片样式:"+css);
        System.out.println("图片地址:"+"data/"+hslfPictureShape.getShapeName()+".png");
        return;
    }
public static void textProcess(HSLFShape shape,double pageWidthProportion,double pageHeightProportion,int i,Map<String,String> animationMap) {
        System.out.println("-------------文字处理-------------");
        HSLFTextBox textBox = (HSLFTextBox) shape;
        List<HSLFTextParagraph> hslfTextParagraphs = textBox.getTextParagraphs();
        Map<String,String> styleMap = new HashMap();
        HSLFTextRun textRuns = hslfTextParagraphs.get(0).getTextRuns().get(0);
        styleMap.put("width",textBox.getAnchor().getWidth()/pageWidthProportion +"px");
        styleMap.put("height",textBox.getAnchor().getHeight()/pageHeightProportion +"px");
        styleMap.put("left",textBox.getAnchor().getMinX()/pageWidthProportion +"px");
        styleMap.put("top",textBox.getAnchor().getMinY()/pageHeightProportion +"px");
        if(textRuns.getFontColor().getSolidColor().getColor() != null){
            styleMap.put("color",getColor(textRuns.getFontColor().getSolidColor().getColor().toString().split(",")));
        }
        styleMap.put("z-index",i+"");
        styleMap.put("border-width",textBox.getLineWidth() +"px");
        if(textBox.getFillColor() != null){
            styleMap.put("background-color",getColor(textBox.getFillColor().toString().split(",")));
        }
        if(textBox.getLineColor() != null){
            styleMap.put("border-color",getColor(textBox.getLineColor().toString().split(",")));
        }
        String content = "";
        String style = "\"";
        if(textRuns.getFontSize() != null){
            style = style+"font-size:" + textRuns.getFontSize()+"px;";
        }
        if(textRuns.getFontColor().getSolidColor().getColor() != null){
            style = style+"color:" + getColor(textRuns.getFontColor().getSolidColor().getColor().toString().split(","))+";";
        }
        if(textRuns.getFontFamily() != null){
            style = style+"font-family:" + textRuns.getFontFamily()+";";
        }
        for(String string:textBox.getText().split("\n")){
            content = content + "<div><span style="+ style +"\">" +string+"</span></div>";
        }
        System.out.println("文字内容:" + textBox.getText());
        System.out.println("文字外部样式:" + JSON.toJSONString(styleMap));
        System.out.println("文字内部样式:" + content);
    }

具体看git

https://github.com/zoudlgit/poi-ppt-demo

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值