Sunday, 26 April 2020

Insert, Replace and Extract Images in a PowerPoint document in Java


An image is one of the most important elements in PowerPoint documents, so it’s more than important to understand how to manipulate images. Here I’ll share with you some examples to insert, replace and extract images by using FreeSpire.Presentation for Java.

Add Spire.Presentation.jar as dependency

Method 1: Download Free Spire.Presentation for Java pack, unzip it and you’ll get Spire.Presentation.jar file from the “lib” folder. Import the .jar file in your project as a dependency.
Method 2: If you are creating a Maven project, you can easily add the .jar dependency with simple configurations. Please check the tutorial which will show you how to install it from Maven.

Example 1 Insert Images in a PowerPoint document

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.geom.Rectangle2D;

public class InsetImage {
   
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, 200, 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\\Test1\\Desktop\\Image1.png", 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\\Test1\\Desktop\\Image2.jpg", rect);
        image.getLine().setFillType(FillFormatType.
NONE);

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

    }
}

Example 2 Replace Images

import com.spire.presentation.*;

import com.spire.presentation.drawing.IImageData;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.io.FileInputStream;


public class ReplaceImage {

    public static void main(String[] args) throws Exception {

        //create a Presentation object

        Presentation presentation= new Presentation();


        //load the sample PowerPoint file

        presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\InsertImages.pptx");


        //add an image to the image collection

        String imagePath = "C:\\Users\\Test1\\Desktop\\Image3.png";

        BufferedImage bufferedImage = ImageIO.read(new FileInputStream(imagePath));

        IImageData image = presentation.getImages().append(bufferedImage);


        //get the shape collection from the first slide

        ShapeCollection shapes = presentation.getSlides().get(0).getShapes();


        //loop through the shape collection

        for (int i = 0; i < shapes.getCount(); i++) {


        //determine if a shape is a picture

        if (shapes.get(i) instanceof SlidePicture) {


        //fill the shape with a new image

        ((SlidePicture) shapes.get(i)).getPictureFill().getPicture().setEmbedImage(image);

            }

        }


        //save to file

        presentation.saveToFile("output/ReplaceImage.pptx", FileFormat.PPTX_2013);

    }
 
} 

Example 3 Extract Images

Part 1 Extract All Images from a PowerPoint document
import com.spire.presentation.Presentation;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.io.File;


public class ExtractAllImage {

    public static void main(String[] args) throws Exception {

        //Create a Presentation instance

        Presentation ppt = new Presentation();
      
       //Load the PowerPoint document

        ppt.loadFromFile("C:\\Users\\Test1\\Desktop\\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("output/" + "extractAllImage-%1$s.png", i)));

        }

    }
 
} 
Part 2 Extract Images from a specific slide
import com.spire.presentation.*;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.io.File;

public class ExtractSpecificImage {

    public static void main(String[] args) throws Exception {

        //Create a Presentation instance

        Presentation ppt = new Presentation();

        //Load the PowerPoint document

        ppt.loadFromFile("C:\\Users\\Test1\\Desktop\\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("output/" + "extractSpecificImage-%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("output/" + "extractSpecificImage-%1$s.png", i)));

            }

        }

    }

}


No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...