I am trying to build a rather crude tool that converts a ppt/pptx file to a HTML format.
I have found out that, unfortunately, apache poi does not provide a unified programming model for working with power point files and code has to be written for parsing each format.
I feel that the pptx file support is much more limited than the ppt support.
One problem I'm facing is getting information regarding the background (color, pattern, background image) of a pptx slide.
I find the XSLFBackground (pptx api) class to be much more limited than its corresponding Background class (ppt api).
Has anyone managed to get get information regarding the background of a pptx slide using apache poi ?
Also can someone please point me to some good resources on this subject. I find the apache poi javadoc almost unusable and the examples on the poi website only cover basic functionality.
Best regards, Sergiu
3
votes
Are you searching for the background properties of a shape or the background of a page defined in the page master slide?
– kiwiwings
I am searching for the background properties of individual slides
– Sergiu Repede
1 Answers
4
votes
The content of the background element is described in the Office Open Schema - check the zip-link at the bottom and the pml-slide.xsd inside.
With the schema in the hand you will understand the XML beans below the usermodel interface.
For a starter, here is an example of reading a background image and also of exporting the slides to pngs (maybe useful for your html export?):
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();
}
}
}