2013-08-17 36 views
3

我正在尝试构建一个将ppt/pptx文件转换为HTML格式的相当粗糙的工具。
我发现不幸的是,apache poi没有提供用于使用电源点文件的统一编程模型,并且必须编写用于解析每种格式的代码。
我觉得pptx文件的支持比ppt支持更有限。 我面临的一个问题是获取有关pptx幻灯片的背景(颜色,图案,背景图像)的信息。

我发现XSLFBackground(pptx api)类远比其对应的Background类(ppt api)有限。
有没有人设法使用apache poi获取有关pptx幻灯片背景的信息?

也可以有人请指点我在这个问题上的一些很好的资源。我发现Apache poi javadoc几乎不可用,poi网站上的示例仅涵盖基本功能。

此致的Sergiu如何从pptx幻灯片使用apache poi获取背景数据

+0

您是否在寻找的背景属性形状或页面主幻灯片中定义的页面的背景? – kiwiwings

+0

我正在寻找个人幻灯片 –

回答

4

背景元素的含量在Office Open Schema描述 - 检查zip-link at the bottom和PML-slide.xsd内部。

随着模式的掌握,您将了解usermodel接口下面的XML bean。

对于首发,这里是读书的背景图像,并导出幻灯片png格式的例子(也许有用你的HTML出口?):

import java.awt.*; 
import java.awt.geom.*; 
import java.awt.image.BufferedImage; 
import java.io.*; 
import java.net.URL; 
import org.apache.poi.xslf.usermodel.*; 
import org.openxmlformats.schemas.presentationml.x2006.main.CTBackground; 

public class PptxBackground { 
    public static void main(String[] args) throws Exception { 
     // sorry for the content, but it was one of the first non-commercial google matches ... 
     URL url = new URL("http://newkilpatrickblog.typepad.com/files/sunday_june_03_2012_trinity_and_majesty_communion.pptx"); 
     InputStream is = url.openStream(); 
     XMLSlideShow ss = new XMLSlideShow(is); 
     is.close(); 

     XSLFSlide sld = ss.getSlides()[0]; 
     XSLFBackground bg = sld.getBackground(); 
     CTBackground xmlBg = (CTBackground)bg.getXmlObject(); 
     String relId = xmlBg.getBgPr().getBlipFill().getBlip().getEmbed(); 

     XSLFPictureData pic = (XSLFPictureData)sld.getRelationById(relId); 
     String filename = pic.getFileName(); 
     byte fileBytes[] = pic.getData(); 


     /***** or convert the slides to images ****/ 

     double zoom = 2; // magnify it by 2 
     AffineTransform at = new AffineTransform(); 
     at.setToScale(zoom, zoom); 

     Dimension pgsize = ss.getPageSize(); 
     XSLFSlide slides[] = ss.getSlides(); 
     for (int i = 0; i < slides.length; i++) { 
      BufferedImage img = new BufferedImage((int)Math.ceil(pgsize.width*zoom), (int)Math.ceil(pgsize.height*zoom), BufferedImage.TYPE_INT_RGB); 
      Graphics2D graphics = img.createGraphics(); 
      graphics.setTransform(at);   

      graphics.setPaint(Color.white); 
      graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height)); 
      slides[i].draw(graphics); 
      FileOutputStream out = new FileOutputStream("slide-" + (i+1) + ".png"); 
      javax.imageio.ImageIO.write(img, "png", out); 
      out.close();   
     } 
    } 
} 
+0

的背景属性,感谢kiwiwings,这确实是一个很好的解决方案。尽管如此,我希望我能像XSLFBackground.draw那样简单一些,将背景保存在单独的图像中。 –