本文介绍了使用Apache POI将PPT转换为PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
按照 poi.apache/提供的示例进行操作slideshow/how-to-shapes.html 我可以进行转换了.
By following the example provided at poi.apache/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
发布评论