2011-02-15 91 views
2

我需要從使用Java的PowerPoint幻燈片獲取背景圖像。我知道Apache POI項目。我可以從幻燈片中找到用於獲取文本和形狀的材料,但不能找到實際背景。有沒有人有什麼建議?使用Java從PowerPoint幻燈片獲取背景圖像

編輯:我用建議的鏈接吱吱下面的代碼。此代碼似乎抓住幻燈片的內容,但不完全是背景。由此產生的圖像背景爲白色。

我這個PowerPoint

 

package PowerPointProcessing; 

import Logging.LogRunner; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics2D; 
import java.awt.geom.Rectangle2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 

import org.apache.poi.hslf.HSLFSlideShow; 
import org.apache.poi.hslf.model.Background; 
import org.apache.poi.hslf.model.Fill; 
import org.apache.poi.hslf.model.Shape; 
import org.apache.poi.hslf.model.Slide; 
import org.apache.poi.hslf.usermodel.SlideShow; 


/** 
* 
* @author dvargo 
*/ 
public class PPI 
{ 

    Dimension pageSize; 
    public Slide[] theSlides; 

    public PPI(String powerPointFilePath) 
    { 
     SlideShow ppt = null; 
     //open the presentation 
     try 
     { 
      ppt = new SlideShow(new HSLFSlideShow(powerPointFilePath)); 
     } 
     catch(Exception e) 
     { 
      LogRunner.getLogger().severe("Could not open the powerpoint presentation"); 
      return; 

     } 


     //get all the slides 
     theSlides = ppt.getSlides(); 
     //see how many slides there are 
     int numberOfSlides = theSlides.length; 
     pageSize = ppt.getPageSize(); 


    } 

    public BufferedImage getBackground(Slide theSlide) 
    { 
     Background background; 
     background = theSlide.getBackground(); 
     Fill f = background.getFill(); 
     Color color = f.getForegroundColor(); 
     Shape[] theShapes = theSlide.getShapes(); 

     BufferedImage img = new BufferedImage(pageSize.width, pageSize.height, BufferedImage.TYPE_INT_RGB); 
     Graphics2D graphics = img.createGraphics(); 
     graphics.setPaint(color); 
     graphics.fill(new Rectangle2D.Float(0, 0, pageSize.width, pageSize.height)); 
     theSlide.draw(graphics); 
     return img; 
    } 

    public static void main(String[] args) { 
     PPI ppi = new PPI("C:\\Documents and Settings\\dvargo\\Desktop\\Cludder\\a.ppt"); 
     int count= 0; 
     for (Slide currSlide : ppi.theSlides) 
     { 
      BufferedImage img = ppi.getBackground(currSlide); 
      try 
      { 
       ImageIO.write(img, "jpeg", new File("C:\\Documents and Settings\\dvargo\\Desktop\\ppt\\" + count + ".jpeg")); 
      } 
      catch (IOException ex) 
      { 
       Logger.getLogger(PPI.class.getName()).log(Level.SEVERE, null, ex); 
      } 
      count++; 
     } 
    } 

} 

 
+0

可能重複[提取圖片來自pptx與apache poi](http://stackoverflow.com/questions/4650785/extracting-images-from-pptx-with-apache-poi) – OscarRyz 2011-02-15 15:55:01

回答

1

試圖從這個問題看代碼: Extracting images from pptx with apache poi

看起來應該是這樣的:的

Background background = slides[current].getBackground(); 
Fill f = background.getFill(); 
Color color = f.getForegroundColor(); 
+0

謝謝,那個鏈接很有幫助。然而,我無法抓住我在頂部提供的PowerPoint例子的背景。它好像沒有抓住他們?也許它們與背景不同? – user489041 2011-02-15 16:24:11