首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Apache POI将PPT转换为PNG

使用Apache POI将PPT转换为PNG
EN

Stack Overflow用户
提问于 2012-08-04 00:49:15
回答 1查看 3.7K关注 0票数 2

通过遵循http://poi.apache.org/slideshow/how-to-shapes.html提供的示例,我实现了转换。

代码语言:javascript
复制
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();
}

然而,生成的图像非常小。如何增加图像输出的大小?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-28 18:05:21

您需要调整图形上下文的大小并添加常规转换。

代码语言:javascript
复制
    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();
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11799972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档