Skip to content

Instantly share code, notes, and snippets.

@aautar
Created April 12, 2017 20:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aautar/b3a7f51438a5e478b2f0672d4117a98b to your computer and use it in GitHub Desktop.
Save aautar/b3a7f51438a5e478b2f0672d4117a98b to your computer and use it in GitHub Desktop.
PPTX to PNG image using Apache POI
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import java.awt.*;
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;
public class Transcoder {
public static void main(String args[]) throws IOException{
//creating an empty presentation
File file=new File("sample-ppt-for-converter.pptx");
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));
//getting the dimensions and size of the slide
Dimension pgsize = ppt.getPageSize();
java.util.List<XSLFSlide> slide = ppt.getSlides();
for (int i = 0; i < slide.size(); i++) {
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
//clear the drawing area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
//render
slide.get(i).draw(graphics);
//creating an image file as output
FileOutputStream out = new FileOutputStream("ppt_image_" + i + ".png");
javax.imageio.ImageIO.write(img, "png", out);
System.out.println("Image successfully created");
out.close();
}
}
}
@jaluDadhich
Copy link

Hi
How do you convert the .ppt to image , Please share the answer

@aautar
Copy link
Author

aautar commented Aug 4, 2019

You can try the code above, but I remember encountering a number of issues with Apache POI rendering slides.. but things may have changed since 2017 🤷‍♂️

We ended up using the Zamzar API (https://developers.zamzar.com/) which worked well for us.

@zexho994
Copy link

Hi
How do you convert the .ppt to image , Please share the answer

FileInputStream is = new FileInputStream("slideshow.ppt");
HSLFSlideShow ppt = new HSLFSlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
int idx = 1;
for (HSLFSlide slide : ppt.getSlides()) {
    BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = img.createGraphics();
    // clear the drawing area
    graphics.setPaint(Color.white);
    graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
    // render
    slide.draw(graphics);
    // save the output
    FileOutputStream out = new FileOutputStream("slide-" + idx + ".png");
    javax.imageio.ImageIO.write(img, "png", out);
    out.close();
    idx++;
}

@archine
Copy link

archine commented Dec 5, 2022

How do I use poi to operate pptx smart art

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment