通过poi读取ppt元素demo
生活随笔
收集整理的這篇文章主要介紹了
通过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踩坑
具體功能
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 物理层、数据链路层、介质访问控制子层
- 下一篇: 常用的开始→运行→输入命令集锦