【问题标题】:Get background image from PowerPoint Slide using Java使用 Java 从 PowerPoint 幻灯片中获取背景图像
【发布时间】:2011-02-15 15:39:01
【问题描述】:

我需要使用 java 从 PowerPoint 幻灯片中获取背景图像。我知道 Apache POI 项目。我可以找到从幻灯片中获取文本和形状的材料,但不是实际背景。有人有什么建议吗?

编辑:我使用建议的链接创建了以下代码。这段代码似乎抓取了幻灯片的内容,但不完全是背景。生成的图像是白色的背景。

我用这个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++; } } }

【问题讨论】:

标签: java powerpoint apache-poi


【解决方案1】:

从这个问题看代码: Extracting images from pptx with apache poi

看起来应该是这样的:

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

【讨论】:

  • 谢谢,该链接很有帮助。但是,我无法获取顶部提供的 PowerPoint 示例的背景。好像没有抓住他们?也许它们与背景不同?
  • SlideMaster 可能就是这个名字?
  • @user,SlideMaster 是一个工作表,所以它应该以相同的方式工作。您可以像这样从幻灯片中获得对它的引用:SlideMaster master = slideShow.getSlidesMasters()[0];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
  • 2021-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多