apachePOI

Create, modify, and display MS-Office files using Java programs

Example Powerpoint

structure

|-- pom.xml
|-- src
|   `-- main
|       `-- java
|           `-- org
|               `-- allowed
|                   `-- bitarus
|                       `-- apache
|                           `-- poi
|                               `-- test
|                                   `-- Main.java
|

pom.xml

   1 <project xmlns="http://maven.apache.org/POM/4.0.0" 
   2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   3    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   4 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   5    <modelVersion>4.0.0</modelVersion>
   6    <groupId>apache-poi-test</groupId>
   7    <artifactId>apache-poi-test</artifactId>
   8    <version>0.0.1-SNAPSHOT</version>
   9    <build>
  10       <sourceDirectory>src</sourceDirectory>
  11       <plugins>
  12          <plugin>
  13             <artifactId>maven-compiler-plugin</artifactId>
  14             <!--<version>3.5.1</version>-->
  15             <configuration>
  16                <source>1.8</source>
  17                <target>1.8</target>
  18             </configuration>
  19          </plugin>
  20          <plugin>
  21              <artifactId>maven-assembly-plugin</artifactId>
  22                  <version>2.4</version>
  23                  <configuration>
  24                          <descriptorRefs>
  25                                  <descriptorRef>jar-with-dependencies</descriptorRef>
  26                          </descriptorRefs>
  27                      <archive>
  28                          <manifest>
  29                          <mainClass>org.allowed.bitarus.apache.poi.test.Main</mainClass>
  30                      </manifest>
  31                  </archive>
  32                  </configuration>
  33              <executions>
  34                   <execution>
  35                       <id>make-assembly</id> 
  36                       <phase>package</phase> 
  37                       <goals>
  38                           <goal>single</goal>
  39                       </goals>
  40                   </execution>
  41               </executions>
  42           </plugin>
  43       </plugins>
  44    </build>
  45    <dependencies>  
  46       <dependency>
  47          <groupId>org.apache.poi</groupId>
  48          <artifactId>poi</artifactId>
  49          <version>4.0.1</version>
  50       </dependency>
  51       <dependency>
  52          <groupId>org.apache.poi</groupId>
  53          <artifactId>poi-ooxml</artifactId>
  54          <version>4.0.1</version>
  55       </dependency>    
  56    </dependencies>
  57 </project>

Main.java

   1 package org.allowed.bitarus.apache.poi.test;
   2 
   3 import java.io.FileOutputStream;
   4 import java.io.FileInputStream;
   5 import java.io.IOException;
   6 import java.io.File;
   7 import org.apache.poi.xslf.usermodel.XMLSlideShow;
   8 import org.apache.poi.xslf.usermodel.XSLFSlide;
   9 import org.apache.poi.xslf.usermodel.XSLFShape;
  10 import java.util.List;
  11 import org.apache.poi.xslf.usermodel.XSLFTextShape;
  12 import org.apache.poi.xslf.usermodel.XSLFPictureShape;
  13 import org.apache.poi.xslf.usermodel.XSLFPictureData;
  14 import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
  15 
  16 public class Main {
  17 
  18    public static void main(String args[]) {
  19       try{
  20 
  21           File file = new File(args[0]);
  22           XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));
  23 
  24           List<XSLFSlide> slides = ppt.getSlides();
  25 
  26           for ( XSLFSlide slide : slides ){
  27               System.out.println(String.format("%s %d", slide.getSlideName() , slide.getSlideNumber() ));
  28               List<XSLFShape> shapes = slide.getShapes();
  29               for (XSLFShape shape: shapes ){
  30                  System.out.println(String.format("Shape name: %s",shape.getShapeName() ) );
  31 
  32                  if( shape instanceof XSLFTextShape){
  33                      System.out.println(String.format("Shape text: %s", ((XSLFTextShape) shape).getText() ) );
  34 
  35                      for(XSLFTextParagraph para: ( (XSLFTextShape) shape).getTextParagraphs() ){
  36                         System.out.println(String.format("Paragraph text: %s bullet: %b indentLevel: %d", para.getText(), 
  37 para.isBullet(), 
  38 para.getIndentLevel() ) );
  39                      }
  40                  }
  41 
  42                  if( shape instanceof XSLFPictureShape ){
  43                      XSLFPictureData pd = (( XSLFPictureShape ) shape).getPictureData();
  44                      System.out.println(String.format("Filename: %s", pd.getFileName() ) );
  45                      System.out.println(String.format("Content type: %s", pd.getContentType() ) );
  46                      System.out.println(String.format("Image dimension (points): %s", pd.getImageDimension().toString() ) );
  47                      System.out.println(String.format("Image dimension (pixels): %s", 
  48 pd.getImageDimensionInPixels().toString() ) );
  49                  }
  50 
  51               }
  52           }
  53           ppt.close();
  54       }
  55       catch(Exception ex){
  56           ex.printStackTrace();
  57       }
  58    }
  59 }

apachePOI (last edited 2022-04-24 12:31:56 by localhost)