用apache poi将ppt转换为png

时间:2015-10-16 03:15:01

标签: java apache-poi powerpoint

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

viewmodel = tvm()

我无法得到正确的PNG。

第一张照片是来自ppt的幻灯片,第二张照片是转换后的结果。

如何获得正确的结果?

slide from the ppt [result after converting with POI 2

我已确认转换图表无法显示。

2 个答案:

答案 0 :(得分:0)

我将POI 3.10用于同一目的,并且我发现PPT文件中嵌入的所有内容都无法转换(包括Cliparts,SmartArt,艺术字,表格和外部文件)。

只有粘贴在文件中的图像才会包含在转换后的文件中。

答案 1 :(得分:0)

TutorialPoint有一个非常简单但很少有调整教程HERE.我修改了在JFrame上显示完整的pptx文档,发现下面的代码:

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