PPT到PNG转换与Apache POI(PPT to PNG conversion with Ap

2019-09-20 18:29发布

按照在所提供的例子http://poi.apache.org/slideshow/how-to-shapes.html我开始工作的转换。

FileInputStream is = new FileInputStream("slideshow.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, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = img.createGraphics();

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

然而,生成的图像是非常小的。 如何提高图像输出的大小?

Answer 1:

您必须重新调整图形上下文,并添加一般的转变。

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

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

    Dimension pgsize = ppt.getPageSize();

    Slide[] slide = ppt.getSlides();
    for (int i = 0; i < slide.length; 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();
    }


文章来源: PPT to PNG conversion with Apache POI