Insert and Extract Images in PowerPoint Using Java

In most cases, we will insert images to PowerPoint to make the document more attractive and interesting. And this article will demonstrate how to insert and extract images in PowerPoint using a free Java library — Free Spire.Presentation for Java.

Import JAR Dependency (2 Methods)
● Download the Free Spire.Presentation for Java and unzip it. Then add the Spire.Presentation.jar file to your project as dependency.

● You can also 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>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

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, "C:\\Users\\Administrator\\Desktop\\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, "C:\\Users\\Administrator\\Desktop\\b.jpg", rect);
image.getLine().setFillType(FillFormatType.NONE);

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

Extract all 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("extractImage-%1$s.png", i)));
}
}
}

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 second slide
ISlide slide = ppt.getSlides().get(1);

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("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("extractImage-%1$s.png", i)));
}
}
}
}