8

I am trying to merge some pptx documents programmatically using java. I figured out how to do this in essence using Apache POI but the documents I am trying to merge do not work.

After significant searching and trial and error I figured out that the reason for this is that the pptx documents do not have theme information (i.e., if I click into powerpoint and check the slide master view it's blank). If I goto the themes in the Design Ribbon and select 'office theme' or another theme then save. the files will merge charmingly. Otherwise, I run into the following error:

Exception in thread "main" java.lang.IllegalArgumentException: Failed to fetch default style for otherStyle and level=0
    at org.apache.poi.xslf.usermodel.XSLFTextParagraph.getDefaultMasterStyle(XSLFTextParagraph.java:1005)
    at org.apache.poi.xslf.usermodel.XSLFTextParagraph.fetchParagraphProperty(XSLFTextParagraph.java:1029)
    at org.apache.poi.xslf.usermodel.XSLFTextParagraph.isBullet(XSLFTextParagraph.java:654)
    at org.apache.poi.xslf.usermodel.XSLFTextParagraph.copy(XSLFTextParagraph.java:1044)
    at org.apache.poi.xslf.usermodel.XSLFTextShape.copy(XSLFTextShape.java:631)
    at org.apache.poi.xslf.usermodel.XSLFSheet.appendContent(XSLFSheet.java:358)
    at com.apsiva.main.Snippet.main(Snippet.java:28)

The following is the code I ran:

package com.apsiva.main;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xslf.usermodel.SlideLayout;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFSlideLayout;

public class Snippet {
    /** Merge the pptx files in the array <decks> to the desired destination 
         * chosen in <outputPath> */
        public static void main(String[] args) {
            try {
                FileInputStream empty = new FileInputStream("C:/Users/Alex/workspace/OutputWorker/tmp/base2.pptx");
                XMLSlideShow pptx;

                pptx = new XMLSlideShow(empty);
                XSLFSlideLayout defaultLayout = pptx.getSlideMasters()[0].getLayout(SlideLayout.TITLE_AND_CONTENT);

                FileInputStream is = new FileInputStream("C:/Users/Alex/workspace/OutputWorker/tmp/noWork.pptx");
//              FileInputStream is = new FileInputStream("C:/Users/Alex/workspace/OutputWorker/tmp/works2.pptx");
                XMLSlideShow src = new XMLSlideShow(is);
                is.close();
                for (XSLFSlide srcSlide: src.getSlides()){
                    pptx.createSlide(defaultLayout).appendContent(srcSlide);
                }
                FileOutputStream out = new FileOutputStream("C:/POI-TEST-OUTPUT.pptx");
                pptx.write(out);
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
}

I want to get these files to merge and I believe the solution is to programmatically assign the theme to the files. How can it be done?

Thank you for your consideration!

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
ThinkBonobo
  • 15,487
  • 9
  • 65
  • 80

2 Answers2

0

In some cases when you have generated pptx files (ex. JasperReport exports) then some invalid values might be added for different fields. For example line spacing, which can be percent, and special characters, and the apache poi xslf doesn't know how to handle these values. When opening the file, PowerPoint automatically adjusts these values to valid ones. When using apache poi, you have to individually identify these fields and adjust them manually. I had a similar issue, but with line spacing, and did a workaround, by setting the values for each paragraph like this:

List<XSLFShape> shapes = srcSlide.getShapes();                
for (XSLFShape xslfShape: shapes) {
    if (xslfShape instanceof XSLFTextShape){
    List<XSLFTextParagraph> textParagraphs = ((XSLFTextShape) xslfShape).getTextParagraphs();
        for (XSLFTextParagraph textParagraph: textParagraphs) {                            
            textParagraph.setLineSpacing(10d);
        }
    }
}

This worked like a charm.

A more effective way to do this is to do it directly on the XML object:

 List<CTShape> ctShapes = srcSlide.getXmlObject().getCSld().getSpTree().getSpList();
    for (CTShape ctShape : ctShapes) {
        List<CTTextParagraph> ctTextParagraphs = ctShape.getTxBody().getPList();
        for (CTTextParagraph paragraph : ctTextParagraphs) {
            if (paragraph.getPPr().getLnSpc() != null) {
                paragraph.getPPr().unsetLnSpc();
            }
        }
    }
Dezso Gabos
  • 2,297
  • 5
  • 21
  • 29
-1

/ApachePOI/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextParagraph.java

CTTextParagraphProperties getDefaultMasterStyle()

add

if( o.length == 0 ) {
    return null;
}