用Apache POI将PPT转换为PNG

编程入门 行业动态 更新时间:2024-10-10 09:21:03
本文介绍了用Apache POI将PPT转换为PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我使用以下代码将ppt转换为png时:

When I convert ppt to png using follow code:

public static void main(String[] args) throws FileNotFoundException, IOException { final String PPT_TEMPLATE = "data/test.pptx"; float scale = 1; XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(PPT_TEMPLATE)); Dimension pgsize = ppt.getPageSize(); int width = (int) (pgsize.width * scale); int height = (int) (pgsize.height * scale); XSLFSlide slide = ppt.getSlides()[5]; BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = img.createGraphics(); // default rendering options graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); graphics.setColor(Color.white); graphics.clearRect(0, 0, width, height); graphics.scale(scale, scale); // draw stuff slide.draw(graphics); // save the result FileOutputStream out = new FileOutputStream(new File("D:/test.png")); try { ImageIO.write(img, "png", out); } finally { out.close(); } System.out.println("Job Done"); }

我无法获得正确的PNG.

And I can't get the correct PNG.

第一张图片是ppt的幻灯片,第二张图片是转换后的结果.

first picture is a slide from ppt and second picture is the result after converting.

如何获得正确的结果?

[ 2

并且我已经确认无法显示转化图表.

And I have confirmed that conversion chart can't display.

推荐答案

TutorialPoint非常简单,但是几乎没有进行任何调整的教程即可做到这里.我修改为在JFrame上显示完整的pptx文档,发现以下代码:

TutorialPoint has a very straight forward but with little tweaking tutorial to do that HERE. I modified to display a full pptx document on JFrame found the code below:

try { outFile = new File("pages/" + fileName); //fileName here is the name of the folder to save all the slides if (!outFile.exists()) { outFile.mkdir(); //creating an empty presentation XMLSlideShow ppt = new XMLSlideShow(is); //getting the dimensions and size of the slide Dimension pgsize = ppt.getPageSize(); XSLFSlide[] slide = ppt.getSlides(); BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB); for (int i = 0; i < slide.length; i++) { 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); //creating an image file as output System.out.println(outFile.getName() + "/" + i + ".png"); OutputStream out = new FileOutputStream("pages/" + fileName + "/" + i + ".png"); javax.imageio.ImageIO.write(img, "png", out); out.close(); } } JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); if (outFile.exists() && outFile.isDirectory()) { //Always a good practice to check to avoid errors final FilenameFilter IMAGE_FILTER = new FilenameFilter() { @Override public boolean accept(final File dir, final String name) { if (name.endsWith(".png")) //make sure only png are valid return (true); else return (false); } }; for (final File f : outFile.listFiles(IMAGE_FILTER)) {//fetch all png files within the folder panel.add(new JLabel(new ImageIcon(f.getAbsolutePath()))); } } JScrollPane labelScrollPane = new JScrollPane(panel); //Wrapped in scrollpane just in case the slides are much add(labelScrollPane); //Add to frame or panel

更多推荐

用Apache POI将PPT转换为PNG

本文发布于:2023-10-31 10:38:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545832.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   POI   Apache   PNG   PPT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!