codecamp

Apache POI PPT - 读取形状

您可以使用 XSLFShape 类的方法 getShapeName()来计算演示文稿中使用的形状数量。 下面给出的是从演示中读取形状的程序:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

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 
      XSLFSlide[] slide = ppt.getSlides();
      
      //getting the shapes in the presentation
      System.out.println("Shapes in the presentation:");
      for (int i = 0; i < slide.length; i++){
         
         XSLFShape[] sh = slide[i].getShapes();
         for (int j = 0; j < sh.length; j++){
            
            //name of the shape
            System.out.println(sh[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

新添加的具有各种形状的幻灯片显示如下:


Apache POI PPT - 创建超链接
Apache POI PPT - 格式化文本
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }