English 中文(简体)
Apache POI PPT - Images
  • 时间:2024-11-03

Apache POI PPT - Images


Previous Page Next Page  

In this chapter, you will learn how to add an image to a PPT and how to read an image from it.

Adding Image

You can add images to a presentation using the createPicture() method of XSLFSpde. This method accepts image in the form of byte array format. Therefore, you have to create a byte array of the image that is to be added to the presentation.

Follow the given procedure to add an image to a presentation. Create an empty spdeshow using XMLSpdeShow as shown below −


XMLSpdeShow ppt = new XMLSpdeShow();

Create an empty presentation in it using createSpde().


XSLFSpde spde = ppt.createSpde();

Read the image file that is to be added and convert it into byte array using IOUtils.toByteArray() of the IOUtils class as shown below −


//reading an image
File image = new File("C://POIPPT//boy.jpg");

//converting it into a byte array
byte[] picture = IOUtils.toByteArray(new FileInputStream(image));

Add the image to the presentation using addPicture(). This method accepts two variables: byte array format of the image that is to be added and the static variable representing the file format of the image. The usage of the addPicture() method is shown below −


XSLFPictureData idx = ppt.addPicture(picture, XSLFPictureData.PICTURE_TYPE_PNG);

Embed the image to the spde using createPicture() as shown below −


XSLFPictureShape pic = spde.createPicture(idx);

Given below is the complete program to add an image to the spde in a presentation −


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

import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xslf.usermodel.XMLSpdeShow;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSpde;

pubpc class AddingImage {
   pubpc static void main(String args[]) throws IOException {
      //creating a presentation 
      XMLSpdeShow ppt = new XMLSpdeShow();
      
      //creating a spde in it 
      XSLFSpde spde = ppt.createSpde();
      
      //reading an image
      File image = new File("C://POIPPT//boy.jpg");
      
      //converting it into a byte array
      byte[] picture = IOUtils.toByteArray(new FileInputStream(image));
      
      //adding the image to the presentation
      XSLFPictureData idx = ppt.addPicture(picture, PictureType.PNG);
      
      //creating a spde with given picture on it
      XSLFPictureShape pic = spde.createPicture(idx);
      
      //creating a file object 
      File file = new File("addingimage.pptx");
      FileOutputStream out = new FileOutputStream(file);
      
      //saving the changes to a file
      ppt.write(out);
      System.out.println("image added successfully");
      out.close();	
   }
}

Save the above Java code as AddingImage.java, and then compile and execute it from the command prompt as follows −


$javac AddingImage.java
$java AddingImage

It will compile and execute to generate the following output −


reordering of the spdes is done

The presentation with the newly added spde with image appears as follows −

Adding Image

Reading Image

You can get the data of all the pictures using the getPictureData() method of the XMLSpdeShow class. The following program reads the images from a presentation −


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

import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.xslf.usermodel.XMLSpdeShow;
import org.apache.poi.xslf.usermodel.XSLFPictureData;

pubpc class Readingimage {
   pubpc static void main(String args[]) throws IOException {
      //open an existing presentation 
      File file = new File("addingimage.pptx");
      XMLSpdeShow ppt = new XMLSpdeShow(new FileInputStream(file));
      
      //reading all the pictures in the presentation
      for(XSLFPictureData data : ppt.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);
      ppt.write(out);
      out.close();	
   }
}

Save the above Java code as Readingimage.java, and then compile and execute it from the command prompt as follows −


$javac Readingimage.java
$java Readingimage

It will compile and execute to generate the following output −


picture name: image1.png
picture format: 6 
Advertisements