Search code examples
javaapache-poipowerpoint

Adding existing extracted images to slides


I am looking for a way to take existing images that were extracted and add them to a new PowerPoint Project. The program should take the existing images, save them to a folder or location then the remaining code should take them and add them to a PPTX formatted on to each slide correctly.

Please see my Java code below:

// Converting the slides of a PPT into Images using Java
import java.util.List;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFSlide;

import javax.imageio.ImageIO;

public class Array {

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

        // create an empty presentation
        File file = new File("PowerPoint Location");
        XMLSlideShow ppt
            = new XMLSlideShow(new FileInputStream(file));

        // get the dimension and size of the slide
        Dimension pgsize = ppt.getPageSize();
        List < XSLFSlide > slide = ppt.getSlides();
        BufferedImage img = null;

        System.out.println(slide.size());

        for (int i = 0; i < slide.size(); i++) {
            img = new BufferedImage(
                pgsize.width, pgsize.height,
                BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();

            // clear area
            graphics.setPaint(Color.white);
            graphics.fill(new Rectangle2D.Float(
                0, 0, pgsize.width, pgsize.height));

            // draw the images
            slide.get(i).draw(graphics);
            FileOutputStream out = new FileOutputStream(
                "ppt_image" + i + ".png");
            ImageIO.write(img, "png", out);
            ppt.write(out);
            out.close();
            System.out.println(i);
        }
        System.out.println("Images successfully created");

        //creating a presentation
        //creating a slide in it

        //reading an image
        File image = new File("Take in existing images to add to new project/presentation");

        //converting it into a byte array
        byte[] picture = IOUtils.toByteArray(new FileInputStream(image));

        //adding the image to the presentation
        XSLFPictureData idx = ppt.addPicture(picture, PictureData.PictureType.PNG);

        //creating a slide with given picture on it
        XSLFPictureShape pic = slide.createPicture(idx);

        //creating a file object
        File fileTwo = new File("Create new presentation with slides formatted on each slide");
        FileOutputStream out = new FileOutputStream(file);

        //saving the changes to a file
        ppt.write(out);

        System.out.println("image added successfully");
        out.close();

    }
}

Solution

  • It is not really clear from your question what exactly you are trying to do. As of the code it seems you want extract all slides from an input slideshow as pictures and create a new output slide show having a slide for each extracted picture. The simplest code to do so would be like this:

    import java.util.List;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics2D;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.BufferedImage;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ByteArrayOutputStream;
    
    import org.apache.poi.sl.usermodel.PictureData;
    import org.apache.poi.xslf.usermodel.*;
    
    import javax.imageio.ImageIO;
    
    public class PowerPointExtractSliderImagesToNewPPTX {
    
     public static void main(String[] args) throws Exception {
    
      String filePathIn = "./PowerPoint.pptx"; // input PPTX
      String filePathOut = "./PowerPointNew.pptx"; // output PPTX
    
      FileInputStream in = new FileInputStream(filePathIn);
      XMLSlideShow pptIn = new XMLSlideShow(in); // input slideshow
      XMLSlideShow pptOut = new XMLSlideShow(); // output slideshow
    
      // get the dimension of the slide in input PPTX
      Dimension pgsize = pptIn.getPageSize();
    
      // get the slides from input slideshow
      List <XSLFSlide> slides = pptIn.getSlides();
    
      System.out.println(slides.size());
    
      // get all slides from input slideshow as pictures and create a slide in output slideshow for each picture
      for (int i = 0; i < slides.size(); i++) {
    
       //buffered image in page size
       BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
    
       // clear area
       Graphics2D graphics = img.createGraphics();
       graphics.setPaint(Color.white);
       graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
    
       // draw the slide as image into a byte[]
       slides.get(i).draw(graphics);
       ByteArrayOutputStream pictureBytesOut = new ByteArrayOutputStream();
       ImageIO.write(img, "png", pictureBytesOut);
       byte[] pictureBytes = pictureBytesOut.toByteArray();
    
       //adding the image to the output slideshow
       XSLFPictureData idx = pptOut.addPicture(pictureBytes, PictureData.PictureType.PNG);
    
       //creating a slide with given picture on it in output slideshow
       XSLFSlide slide = pptOut.createSlide();
       XSLFPictureShape pic = slide.createPicture(idx);
    
      }
    
      pptIn.close();
    
      // write the output slideshow 
      FileOutputStream out = new FileOutputStream(filePathOut);
      pptOut.write(out);
      out.close();
      pptOut.close();
     }
    }