Apache POI PPT - Reading Shapes



You can get a count of the number of shapes used in a presentation using the method getShapeName() of the XSLFShape class.

Example - Reading Shapes from a Presentation

Given below is the program to read the shapes from a presentation −

ApachePoiPptDemo.java

package com.tutorialspoint;

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 ApachePoiPptDemo {
   public static void main(String args[]) throws IOException {
      //creating a slideshow 
      File file = new File("example1.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();	
   }
}

Output

Run the above code and verify the output −

Shapes in the presentation:
Title 1
Content Placeholder 2

The newly added slide with the various shapes appears as follows −

Reading Shapes
Advertisements