使用 apache poi 从 pptx 中提取图像 [英] Extracting images from pptx with apache poi

查看:32
本文介绍了使用 apache poi 从 pptx 中提取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Apache POI 从 ppt 文件中提取幻灯片,这没有问题,但现在我打算打开 pptx 文件并执行一样,有人知道怎么做吗??

I'm trying to extract slides from a ppt file with Apache POI, there is no problem in that, but now I intend to open pptx files and do the same, does anyone knows how to??

这是从ppt文件中提取图像的代码:

this is the code to extract images from ppt files:

public ImageIcon display() throws JPresentationException { 

    Background background; 
    background = slides[current].getBackground(); 
    Fill f = background.getFill(); 
    Color color = f.getForegroundColor(); 
    Dimension dimension = ppt.getPageSize(); 
    shapes = slides[current].getShapes(); 
    BufferedImage img = new BufferedImage(dimension.width, dimension.height, BufferedImage.TYPE_INT_RGB); 
    Graphics2D graphics = img.createGraphics(); 
    graphics.setPaint(color); 
    graphics.fill(new Rectangle2D.Float(0, 0, dimension.width, dimension.height)); 
    slides[current].draw(graphics); 
    ImageIcon icon = new ImageIcon(img); 

    return icon; 
}

推荐答案

虽然可以参考一些示例代码 来自 POI 项目本身以下是您应该寻找的内容;希望这会有所帮助;

Although you can refer to some example code from the POI project itself below is what you should be looking for; Hope this helps;

private ImageIcon generateFromPPTX(int index) {
    ImageIcon icon = null;
    XMLSlideShow slideShowPPTX = null;
    FileInputStream pptInputStream = null;
    XSLFSlide [] allSlides = null;
    XSLFSlide singleSlide = null;
    BufferedImage memoryImage = null;
    Graphics2D graphics = null;
    try{
        pptInputStream = new FileInputStream("somepptx-file.pptx");
        slideShowPPTX = new XMLSlideShow(pptInputStream);
        allSlides = slideShowPPTX.getSlides();
        if(allSlides == null || allSlides.length == 0) {
            System.out.println("Empty presentation!");
            return null;
        }

        singleSlide = allSlides [index];
        memoryImage = new BufferedImage(slideShowPPTX.getPageSize().width, slideShowPPTX.getPageSize().height, BufferedImage.TYPE_INT_ARGB);
        graphics = memoryImage.createGraphics();
        // Only few rendering hints set but you can set as many as you need depending on your need
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        graphics.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        singleSlide.draw(graphics);
        icon = new ImageIcon(memoryImage);
    }
    catch(IOException exception){
        System.err.println("Input/output Exception:");
        exception.printStackTrace();
    }
    finally{
        slideShowPPTX = null;
        allSlides = null;
        singleSlide = null;
        memoryImage = null;
        graphics = null;
        if(pptInputStream != null){
            try {
                pptInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            pptInputStream = null;
        }
    }
    return icon;
}

这篇关于使用 apache poi 从 pptx 中提取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆