【问题标题】:Extracting images from pptx with apache poi使用 apache poi 从 pptx 中提取图像
【发布时间】:2011-06-06 18:03:31
【问题描述】:

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

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

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

【问题讨论】:

  • 你的问题听起来很奇怪,但我认为如果你真的是说你可以用一个 ppt 文件来做到这一点是有道理的。我编辑了您的答案,但如果这不正确,则回滚我的更改。
  • 我觉得pptx比较合适,我们现在大部分人都有office 2007了。

标签: java apache-poi powerpoint xslf


【解决方案1】:

虽然你可以参考POI项目本身的一些example code 以下是您应该寻找的内容;希望这会有所帮助;

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

【讨论】:

    【解决方案2】:

    这是在VBS中的方法,也许你可以转换:

    Sub SaveAllPictures()
        Dim ap As Presentation: Set ap = ActivePresentation
        Dim savePath As String
        savePath = "C:\Users\me\Desktop\files\"
        Dim i As Integer
        Dim sl As Slide
        Dim sh As Shape
        For Each sl In ap.Slides
            For Each sh In sl.Shapes
                If sh.Type = msoPicture Then
                    sh.Export PathName:=savePath & sh.Name & CStr(i) & ".png", Filter:=ppShapeFormatPNG
                    i = i + 1
                End If
            Next
        Next
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 2015-03-03
      • 2022-11-09
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      相关资源
      最近更新 更多