1

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:

First template

Second template:

Second template

Third 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.

BoonZ
  • 443
  • 1
  • 6
  • 16
  • 1
    For your error with last template, see http://stackoverflow.com/questions/41877957/error-caused-by-java-lang-illegalargumentexception-relationship-null-doesnt/41888174#41888174. There one of the pictures used as texture fill in your template is not an embedded picture but a linked picture. – Axel Richter Feb 02 '17 at 10:38
  • Even in the development build there are still a lot of missing features (e.g. chart support is completely missing). I'm currently reworking a few of the minor rendering issues, like slide numbers and slide layout / master slide defaults. You can always open a bug in our bugzilla or if the file is bigger than 1mB, send it to my apache email ... but I can't guarantee you asap fixes ... – kiwiwings Feb 02 '17 at 12:24
  • Thx for reply, too bad as the POI lib is much faster and with more control than headless libreoffice. I guess we will have to figure out something with open xml sdk, thus we tried to avoid using microsoft :) – BoonZ Feb 07 '17 at 08:44

0 Answers0