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背景图片和背景颜色的获取方法
部分代码
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 ) {
ByteArrayInputStream bais = new ByteArrayInputStream ( autoShape. getFill ( ) . getPictureData ( ) . getData ( ) ) ;
BufferedImage bi1 = null ;
try {
bi1 = ImageIO. read ( bais) ;
File w2 = new File ( "data/" + autoShape. getShapeName ( ) + ".png" ) ;
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" ) ;
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