2011-01-10 120 views
1

我想用Apache POI從ppt文件中提取幻燈片,這裏沒有問題,但現在我打算打開pptx文件並執行相同的操作,是否有人知道如何??用apache poi從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; 
} 
+0

你的問題聽起來很奇怪,但我認爲這是有道理的,如果你真的認爲你可以用ppt文件做到這一點。我編輯了你的答案,但如果這是不正確的回滾我的更改。 – jzd 2011-02-15 15:50:42

回答

1

這裏是做在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 
0

雖然可以參考一些example code從POI項目本身 下面是你應該找的;希望這可以幫助;

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; 
} 
相關問題