使用Java将ppt幻灯片作为图像提取

时间:2011-01-17 22:43:17

标签: java powerpoint

有没有办法以Java编程方式将幻灯片拆分成.png文件?我已经四处搜索,大多数答案都是在C#中提到的,或者提到的程序都不是开源的

3 个答案:

答案 0 :(得分:3)

为了获得不错的质量,请使用以下代码与Apache POI HSLF库(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();
    }

答案 1 :(得分:2)

您将需要使用j-interop(http://www.j-interop.org/)之类的Java / COM桥来以编程方式控制PowerPoint进程,然后可能将各个页面打印到文件中。你可能最好只编写一个VBA脚本。

答案 2 :(得分:0)

将以下代码与Apache POI库一起使用

    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();