使用Apache poi将ppt转换为png

3

您好,我正在尝试使用Apache Poi框架将ppt的每个幻灯片转换为单独的png。问题在于有些幻灯片会变形。例如,有一张幻灯片的背景有彩虹色。而某些幻灯片上的图片在.png文件中根本不出现。

以下是代码:

        FileInputStream is = new FileInputStream(args[0]);

        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,
        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[i].draw(graphics);

        //save the output
        FileOutputStream out = new FileOutputStream("C:\\Users\\Farzad\\Desktop\\slide-" + (i+1) + ".png");
        javax.imageio.ImageIO.write(img, "png", out);
        out.close();
        }

改变 BufferedImage.TYPE_INT_RGB 能提高质量吗? - Wivani
你正在使用哪个版本的POI呢?PPT渲染是随着时间逐步改进的东西,所以如果你还没有尝试过最新的版本,那么值得升级。 - Gagravarr
有人能告诉我运行上述代码需要包含哪个jar文件吗?因为我找不到Dimension和Graphics2D类。 - Panache
1个回答

1

为了使这个工作起效,我们不必使用:

graphics.setPaint(Color.white);

请使用以下内容:
graphics.setPaint(
    slideShow.getSlides()[0].getBackground().getFill().getForegroundColor()
);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接