日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通过poi读取ppt元素demo

發布時間:2023/12/9 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过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

踩坑

  • maven下載spire.presentation包不穩定,我把它放到了私有依賴庫
  • 因為ppt的頁面大小和網頁的大小可能不一樣,會導致位置不對,所以要先獲取比例
  • 盡量輸出png格式圖片,鏤空圖片還沒解決
  • ppt和pptx是兩套處理邏輯
  • 文字多層樣式多次處理
  • 藝術字默認轉換為圖片
  • ppt格式不支持視頻、音頻
  • 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

    總結

    以上是生活随笔為你收集整理的通过poi读取ppt元素demo的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。