Extract Images from PowerPoint in Java

·

2 min read

A PowerPoint document usually contains many images to help illustrate the text content, and sometimes you may need to extract these images for other purposes. Instead of manually saving images one by one, this article will share how to programmatically extract images from PowerPoint presentations using a free Java library.

  • Extract Images from an Entire PowerPoint Document in Java

  • Extract Images from a Specific Slide in Java

Import Dependency (2 Methods)

● Download the free library(Free Spire.Presentation) and unzip it, and then add the Spire.Presentation.jar file to your project as dependency.

● Directly add the jar dependency to your maven project by adding the following configurations to the pom.xml.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

Extract Images from an Entire PowerPoint Document in Java

Free Spire.Presentation provides the Presentation.getImages().get().getImage() method to get all images in a PowerPoint presentation, and then you can save them in a specified file path using ImageIO.write() method. The complete sample code is shown below.

import com.spire.presentation.Presentation;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class extractImage {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();

        //Load a PPT document
        ppt.loadFromFile("E:\\Files\\food.pptx");

        for (int i = 0; i < ppt.getImages().getCount(); i++) {
            String ImageName = "output/" + String.format("extractImage-%1$s.png", i);
            //Extract image
            BufferedImage image = ppt.getImages().get(i).getImage();
            ImageIO.write(image, "PNG",  new File(ImageName));
        }
    }
}

Extract Images from a Specific Slide in Java

To extract images from a specific slide, you can use the SlidePicture.getPictureFill().getPicture().getEmbedImage().getImage() or PictureShape.getEmbedImage().getImage() method. The complete sample code is shown below.

import com.spire.presentation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class extractImageFromSlide {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();

        //Load a PPT document
        ppt.loadFromFile("E:\\Files\\food.pptx");

        //Traverse all shapes in the third slide
        for (int j =0; j < ppt.getSlides().get(2).getShapes().getCount(); j ++)
        {
            IShape shape = ppt.getSlides().get(2).getShapes().get(j);
            //It is the SlidePicture object
            if (shape instanceof SlidePicture)
            {
                //Save to image
                String ImageName = "SlideImage/"  + j + ".png";
                SlidePicture ps = (SlidePicture)shape ;
                BufferedImage image =  ps.getPictureFill().getPicture().getEmbedImage().getImage();
                ImageIO.write(image, "PNG",  new File(ImageName));
            }
            //It is the PictureShape object
            if (shape instanceof PictureShape)
            {
                //Save to image
                String ImageName = "SlideImage/"  + j + ".png";
                PictureShape ps = (PictureShape)shape;
                BufferedImage image =  ps.getEmbedImage().getImage();
                ImageIO.write(image, "PNG",  new File(ImageName));
            }
        }
    }
}