Apache POI PPT - 讀取形狀

2018-12-27 15:50 更新

您可以使用 XSLFShape 類的方法 getShapeName()來(lái)計(jì)算演示文稿中使用的形狀數(shù)量。 下面給出的是從演示中讀取形狀的程序:

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 ,然后從命令提示符處編譯并執(zhí)行,如下所示:

$javac ReadingShapes.java
$java ReadingShapes

它將編譯并執(zhí)行以生成以下輸出。

Shapes in the presentation: 
Rectangle 1
Oval 1
Isosceles Triangle 1

新添加的具有各種形狀的幻燈片顯示如下:


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)