Extract ppt slides as images using Java

Is there a way to programmatically hide slides in .png files using Java? I searched, and most of the answers were either in C #, or the programs mentioned were not open source.

+3
source share
3 answers

For decent quality, use the following code with the Apache POI HSLF library ( http://poi.apache.org/slideshow/how-to-shapes.html ):

        FileInputStream is = new FileInputStream("path_to_your.ppt");
    SlideShow ppt = new SlideShow(is);
    is.close();

    Dimension pgsize = ppt.getPageSize();

    Slide[] slide = ppt.getSlides();
    for (int i = 0; i < slide.length; i++) {

        BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, 1);

        Graphics2D graphics = img.createGraphics();
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                RenderingHints.VALUE_FRACTIONALMETRICS_ON);

        graphics.setColor(Color.white);
        graphics.clearRect(0, 0, pgsize.width, pgsize.height);
        graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

        // render
        slide[i].draw(graphics);

        // save the output
        FileOutputStream out = new FileOutputStream("slide-" + (i + 1) + ".png");
        javax.imageio.ImageIO.write(img, "png", out);
        out.close();
    }
+3
source

You will need to use the Java / COM bridge, for example j-interop (http://www.j-interop.org/), to programmatically control the PowerPoint process and possibly print individual pages in files. You might be better off writing a VBA script.

+2
source

use the following code with Apache POI library

    FileInputStream is = new FileInputStream("D:\\PPT sample.ppt");
    XMLSlideShow ppt = new XMLSlideShow(is);
    is.close();

    Dimension pgsize = ppt.getPageSize();

    XSLFSlide[] slide = ppt.getSlides();
    for (int i = 0; i < slide.length; i++) {

        BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.SCALE_SMOOTH);
        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[i].draw(graphics);

        //save the output
        FileOutputStream out = new FileOutputStream("D:\\slide-"  + (i+1) + ".JPG");
        javax.imageio.ImageIO.write(img, "JPG", out);
        out.close();
0
source

Source: https://habr.com/ru/post/1786014/


All Articles