Monday, August 11, 2014

Converting PPT to Png

public class PptToPng {

    public static void main(String[] args) throws Exception {
        FileInputStream is = new FileInputStream("example.pptx");
        XMLSlideShow ppt = new XMLSlideShow(is);
        is.close();

        double zoom = 2; // magnify it by 2
        AffineTransform at = new AffineTransform();
        at.setToScale(zoom, zoom);

        Dimension pgsize = ppt.getPageSize();

        XSLFSlide[] slide = ppt.getSlides();
        for (int i = 0; i < 2; i++) {
          BufferedImage img = new BufferedImage((int) Math.ceil(pgsize.width * zoom), 
                    (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
          Graphics2D graphics = img.createGraphics();
          graphics.setTransform(at);

          graphics.setPaint(Color.white);
          graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
          slide[i].draw(graphics);
          FileOutputStream out = new FileOutputStream("slide-" + (i + 1) + ".png");
          javax.imageio.ImageIO.write(img, "png", out);
          out.close();
        }
    }
}

No comments: