Created
April 12, 2017 20:43
-
-
Save aautar/b3a7f51438a5e478b2f0672d4117a98b to your computer and use it in GitHub Desktop.
PPTX to PNG image using Apache POI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
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.
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++;
}
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
Hi
How do you convert the .ppt to image , Please share the answer