i have a problem with Apahe POI using XSLF to convert presentation slides to images. The issue is that sometimes there are missing elements on pictures, most of the time background template images or some template shapes.
For conversion im using following code:
package com.itpharma.videocall.poi;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
public class PPTX2PNG {
public static void main(String[] args) {
String prezName = "presentation";
try {
File fin = new File(prezName + ".pptx");
FileInputStream fis = new FileInputStream(fin);
XMLSlideShow ppt = new XMLSlideShow(fis);
Dimension pptSize = ppt.getPageSize();
System.out.println("Presentation size: " + pptSize.width + " x " + pptSize.height);
ppt.getSlides().forEach(slide -> {
System.out.println(slide.getSlideNumber() + ": " + slide.getTitle());
BufferedImage img = new BufferedImage(pptSize.width, pptSize.height, 1);
Graphics2D gfx = img.createGraphics();
gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gfx.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
gfx.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
gfx.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
gfx.setColor(Color.WHITE);
gfx.clearRect(0, 0, pptSize.width, pptSize.height);
gfx.fill(new Rectangle2D.Float(0, 0, pptSize.width, pptSize.height));
slide.draw(gfx);
try {
File fout = new File(prezName + "-slide-" + slide.getSlideNumber() + ".png");
FileOutputStream fos = new FileOutputStream(fout);
javax.imageio.ImageIO.write(img, "png", fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
});
ppt.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Im using POI 3.15 version (latest stable), Eclipse Oxygen on MacOS 10.12.2 for development (production would be on ubuntu VPS) and i dont know what is going wrong, if its my code or my system or POI library or my imports or i dont know.
I attached few images where i used the same presentation and only changed design templates. In upper part of image is PowerPoin screenshot and in lower part are generated images with missing parts. Third template did produce an error an no images.
First template:
Second template:
Third template:
With last template i get following error:
Exception in thread "main" java.lang.IllegalArgumentException: Relationship null doesn't start with this part /ppt/slides/slide1.xml
at org.apache.poi.openxml4j.opc.PackagePart.getRelatedPart(PackagePart.java:469)
at org.apache.poi.xslf.usermodel.XSLFShape$2.getPart(XSLFShape.java:392)
at org.apache.poi.xslf.usermodel.XSLFShape$2.getImageData(XSLFShape.java:400)
at org.apache.poi.sl.draw.DrawPaint.getTexturePaint(DrawPaint.java:135)
at org.apache.poi.sl.draw.DrawPaint.getPaint(DrawPaint.java:112)
at org.apache.poi.sl.draw.DrawTextParagraph.getAttributedString(DrawTextParagraph.java:530)
at org.apache.poi.sl.draw.DrawTextParagraph.breakText(DrawTextParagraph.java:235)
at org.apache.poi.sl.draw.DrawTextShape.drawParagraphs(DrawTextShape.java:158)
at org.apache.poi.sl.draw.DrawTextShape.getTextHeight(DrawTextShape.java:219)
at org.apache.poi.sl.draw.DrawTextShape.drawContent(DrawTextShape.java:102)
at org.apache.poi.sl.draw.DrawSimpleShape.draw(DrawSimpleShape.java:93)
at org.apache.poi.sl.draw.DrawSheet.draw(DrawSheet.java:67)
at org.apache.poi.sl.draw.DrawSlide.draw(DrawSlide.java:39)
at org.apache.poi.xslf.usermodel.XSLFSlide.draw(XSLFSlide.java:301)
at com.itpharma.videocall.poi.PPTX2PNG.lambda$0(PPTX2PNG.java:48)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at com.itpharma.videocall.poi.PPTX2PNG.main(PPTX2PNG.java:32)
I have also tested with other templates and with some custom made presentations and there are almost every time missing some shapes or background.