Skip to content
Related Articles

Related Articles

Adding Images to a Slide in a PPT using Java
  • Last Updated : 28 Dec, 2020

In Java, using createPicture() method from XSLFSlide, images can be added to the PPT. Now the computer doesn’t understand, what’s there in the picture, so internally, this method accepts the image in a byte array format —> (a series of non-understandable random numbers, letters, symbols, etc.) something like this, AGETRDYDC5545#$^NHVGCFFSFSFGSDF#@@?.0976*).

The syntax for an empty slideshow

XMLSlideShow ppt = new XMLSlideShow();

The syntax for creating slide

XSLFSlide slide = ppt.createSlide();

The next task is to read the image file, which we wish to insert, and then convert it into a byte array using IOUtils.toByteArray() —> IOUtils class.

Add image into the presentation, using addPicture():

  • Byte array format of the picture which is to be added(written in the code as byte[] photo)
  • A static variable, which represents the file format of the image.


int idx = ppt.addPicture(photo, XSLFPictureData.PICTURE_TYPE_PNG);

Insert the image to a slide, using createPicture().

XSLFPictureShape pic = slide.createPicture(idx);



// Adding Images to a slide in a PPT using java
import org.apache.poi.util.IOUtils;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
public class InsertingImage {
    public static void main(String args[])
        throws IOException
        XMLSlideShow ppt = new XMLSlideShow();
        XSLFSlide slide = ppt.createSlide();
        File image
            = new File("C://Folder//codingisfun.png");
        byte[] photo = IOUtils.toByteArray(
            new FileInputStream(image));
        int idx = ppt.addPicture(
            photo, XSLFPictureData.PICTURE_TYPE_PNG);
        XSLFPictureShape pic = slide.createPicture(idx);
        // we are creating a file object
        File file = new File("insertingimg.pptx");
        FileOutputStream out = new FileOutputStream(file);
        // saving the changes to the file we created
        System.out.println("image is inserted");

Commands for Compilation and Execution:

$java InsertingImage


Reordering of the slides is done.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :