Convert ppt to png with apache poi

梦想的初衷 提交于 2020-01-07 09:05:26

问题


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");
}

And I can't get the correct PNG.

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

How can I get the correct result?

[

2

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


回答1:


I use POI 3.10 for the same kind of purpose, and I've found out that all things embedded in the PPT file cannot be converted (including Cliparts, SmartArt, WordArt, Tables, and external Files).

Only images pasted in the file will be included in the converted file.




回答2:


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


来源:https://stackoverflow.com/questions/33161979/convert-ppt-to-png-with-apache-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!