含组合内的文本
jar包用的是POI 3.8
/**
* 读取ppt文字
* @throws Exception
*/
public void ReadPPT() throws Exception {
InputStream is = new FileInputStream("D:\\pac\\ppt.pptx");
XMLSlideShow ppt = new XMLSlideShow(is);
XSLFSlide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
// System.out.println(slide[i].getTitle());
// TextRun[] textRuns = slide[i].getTextRuns();
XSLFShape[] sh = slide[i].getShapes();
for (int j = 0; j < sh.length; j++) {//
// String name = sh[j].getShapeName();
getShapesTxt(sh[j]);
}
}
}
public void getShapesTxt(XSLFShape shape) {
// System.out.println(shape.getShapeName());
if ( shape instanceof XSLFTextShape ){
XSLFTextShape txtshape = (XSLFTextShape)shape ;
String sentence = txtshape.getText();
// String result =
// System.out.println(txtshape.getText());
} else if(shape instanceof XSLFAutoShape){
XSLFAutoShape autoShape = (XSLFAutoShape)shape;
// System.out.println(autoShape.getText());
} else if(shape instanceof XSLFFreeformShape){
XSLFFreeformShape shape2 = (XSLFFreeformShape)shape;
// System.out.println(shape2.getText());
}else if ( shape instanceof XSLFConnectorShape ) {
XSLFConnectorShape shape2 = (XSLFConnectorShape)shape ;
// System.out.println(shape2.getShapeName()) ;
} else if ( shape instanceof XSLFPictureShape ){
XSLFPictureShape shape2 = (XSLFPictureShape)shape ;
// System.out.println(shape2.getShapeName()) ;
} else if ( shape instanceof XSLFGroupShape ){//如果是组合类型,继续下钻直到取到文本
XSLFGroupShape shape2 = (XSLFGroupShape)shape ;
for (XSLFShape xslfShape : shape2) {
getShapesTxt(xslfShape);
}
} else if ( shape instanceof XSLFSimpleShape ){
XSLFSimpleShape shape2 = (XSLFSimpleShape)shape ;
// System.out.println(shape2.getShapeName()) ;
}
}