使用 Apache POI 将 PPT 转换为 PNG [英] PPT to PNG conversion with Apache POI
本文介绍了使用 Apache POI 将 PPT 转换为 PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
按照 http://poi.apache.org/中提供的示例进行操作幻灯片/how-to-shapes.html 我得到了转换工作.
By following the example provided at http://poi.apache.org/slideshow/how-to-shapes.html i got the conversion to work.
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();
}
但是生成的图像非常小.如何增加图像输出的大小?
However the generated images are very small. How do increase the size of the image output?
推荐答案
您需要调整图形上下文的大小并添加常规转换.
You need to resize the graphics context and add a general transformation.
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();
}
这篇关于使用 Apache POI 将 PPT 转换为 PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文