简述
您可以使用 XSLFShape 类的方法 getShapeName() 来计算演示文稿中使用的形状数量。 下面给出的是从演示文稿中读取形状的程序 −
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
public class ReadingShapes {
public static void main(String args[]) throws IOException {
//creating a slideshow
File file = new File("shapes.pptx");
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));
//get slides
List<XSLFSlide> slide = ppt.getSlides();
//getting the shapes in the presentation
System.out.println("Shapes in the presentation:");
for (int i = 0; i < slide.size(); i++){
List<XSLFShape> sh = slide.get(i).getShapes();
for (int j = 0; j < sh.size(); j++){
//name of the shape
System.out.println(sh.get(j).getShapeName());
}
}
FileOutputStream out = new FileOutputStream(file);
ppt.write(out);
out.close();
}
}
将上面的Java代码保存为ReadingShapes.java,然后在命令提示符下编译执行如下 −
$javac ReadingShapes.java
$java ReadingShapes
它将编译并执行以生成以下输出。
Shapes in the presentation:
Rectangle 1
Oval 1
Isosceles Triangle 1
新添加的各种形状的幻灯片如下所示−