Insert and Extract Images from a PowerPoint Document in Java

In this article, we're going to demonstrate how to insert and extract images from a PowerPoint document using Spire.Presentation for Java.

Insert images into a PowerPoint document

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

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

            Rectangle2D rect = new Rectangle2D.Double(ppt.getSlideSize().getSize().getWidth() / 2 - 280, 140, 120, 120);

            //Get the first slide
            ISlide slide = ppt.getSlides().get(0);
            //Insert an image into the slide
            IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, "a.jpg", rect);
            image.getLine().setFillType(FillFormatType.NONE);

            //Append a new slide
            slide = ppt.getSlides().append();
            //Insert an image into the slide
            image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, "b.jpg", rect);
            image.getLine().setFillType(FillFormatType.NONE);

            //Save the document
            ppt.saveToFile("InsertImages.pptx", FileFormat.PPTX_2013);
    }
}

Insert and Extract Images from a PowerPoint Document in Java

Extract images from a PowerPoint document

Spire.Presentation for Java supports extracting all of the images in a PowerPoint document along with extracting images from a specific slide in a PowerPoint document.

The following code example shows how to extract all of the images in a PowerPoint document:

import com.spire.presentation.Presentation;

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

public class ExtractImages {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load the PowerPoint document
        ppt.loadFromFile("InsertImages.pptx");

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

Insert and Extract Images from a PowerPoint Document in Java

The following code example shows how to extract images from a specific slide in a PowerPoint document:

import com.spire.presentation.*;

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

public class ExtractImages {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load the PowerPoint document
        ppt.loadFromFile("InsertImages.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        for(int i = 0; i< slide.getShapes().getCount(); i++)
        {
            IShape shape = slide.getShapes().get(i);
            //Extract images from the slide
            if(shape instanceof SlidePicture)
            {
                SlidePicture pic = (SlidePicture) shape;
                BufferedImage image = pic.getPictureFill().getPicture().getEmbedImage().getImage();
                ImageIO.write(image, "PNG",  new File(String.format("slide/" + "extractImage-%1$s.png", i)));
            }
            if(shape instanceof PictureShape)
            {
                PictureShape ps = (PictureShape) shape;
                BufferedImage image = ps.getEmbedImage().getImage();
                ImageIO.write(image, "PNG",  new File(String.format("slide/" + "extractImage-%1$s.png", i)));
            }
        }
    }
}

Insert and Extract Images from a PowerPoint Document in Java