In our earlier blog post, we explored the Apache POI XSLF API for creating and updating a Microsoft PowerPoint PPTX file with Java. Apache POI for Java lets you work with PPT and PPTX files from within your Java applications without getting into the internal file format details of these. In this article, we will go through the details of how Apache POI API can be used to insert an image in a PPTX with Java. We will also have a look at how to read image information from a PPTX file using Java.

System Requirements

Before you begin, make sure that your system meets the following requirements.

  • JDK – Java SE 2 JDK 1.5 or above
  • Memory – 1 GB Ram
  • Operating System – Windows/ Linux/ Mac OS

Installing Apache POI for Java

You’ll need a Java development environment such as Eclipse, IntelliJ IDEA, or any other IDE you’re comfortable with to work with the Apache POI library in your application. Next is to add Apache POI Maven dependency in your application’s pom.xml file as shown below.

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.0.0</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.23.0</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.7</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.3</version>
</dependency>

Java Library to Add Picture to a PPTX File

At this stage, we assume that your development environment is ready and we can start with writing the code for adding images to a PowerPoint presentation file. This is achieved by using the createPicture() method of the XSLFSlide class. Inserting an image to a slide in PowerPoint includes the following steps.

  • Create an instance of the XMLSlideShow class
  • Add a slide to the presentation by calling the createSlide method on the presentation instance from step 1
  • Load the image from file as byte array and add it to XSLFPictureData instance
  • Call the createPicture method on slide’s instance
  • Save the presentation to the output

The following code sample demonstrates these steps.

//creating a presentation 
XMLSlideShow pptx = new XMLSlideShow();
  
//creating a slide in it 
XSLFSlide slide = pptx.createSlide();
  
//reading an image
File image = new File("fileformat.png");
  
//converting it into a byte array
byte[] picture = IOUtils.toByteArray(new FileInputStream(image));
  
//adding the image to the presentation
XSLFPictureData idx = pptx.addPicture(picture, PictureType.PNG);
  
//creating a slide with given picture on it
XSLFPictureShape pic = slide.createPicture(idx);
  
//creating a file object 
File file = new File("imagetopresentation.pptx");
FileOutputStream out = new FileOutputStream(file);
  
//saving the changes to a file
pptx.write(out);
out.close();

Read Image Information from a PPTX File

With Apache POI API for Java, you can read the data of all the images from within a PPTX file using the getPictureData() method of the XMLSlideShow class. The following sample code demonstrates this functionality by reading images from a presentation.

//open an existing presentation 
File file = new File("imagetopresentation.pptx");
XMLSlideShow pptx = new XMLSlideShow(new FileInputStream(file));
  
//reading all the images from the presentation
for(XSLFPictureData data : pptx.getPictureData()){
     byte[] bytes = data.getData();
     String fileName = data.getFileName();
     PictureType pictureFormat = data.getType();
     System.out.println("picture name: " + fileName);
     System.out.println("picture format: " + pictureFormat);
 }

 //saving the changes to a file
 FileOutputStream out = new FileOutputStream(file);
 pptx.write(out);
 out.close();

Conclusion

Apache POI components for working with PowerPoint presentation files let you insert images in a PowerPoint PPT and PPTX files from within your Java application. It also lets you read image information from images on slides. In our upcoming blogs, we’ll further demonstrate the usage of Apache POI Java components for working with PowerPoint presentations. So, stay tuned.