在Java中设置pptx主题

时间:2013-10-11 20:51:15

标签: java apache-poi powerpoint xslf

我正在尝试使用java以编程方式合并一些pptx文档。我想出了如何使用Apache POI实质上做到这一点,但我试图合并的文档不起作用。

经过大量搜索和反复试验后,我发现原因是pptx文档没有主题信息(例如,如果我点击powerpoint并检查幻灯片主视图是否为空白)。如果我转到设计功能区中的主题并选择“办公室主题”或其他主题,则保存。文件将迷人地合并。否则,我遇到以下错误:

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)

以下是我运行的代码:

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();
            }
        }
}

我想让这些文件合并,我相信解决方案是以编程方式将主题分配给文件。怎么办呢?

感谢您的考虑!

2 个答案:

答案 0 :(得分:0)

在某些情况下,当您生成pptx文件(例如 JasperReport导出)时,可能会为不同的字段添加一些无效的值。例如,行距(可以是百分比)和特殊字符,而apache poi xslf不知道如何处理这些值。打开文件时,PowerPoint会自动将这些值调整为有效值。使用apache poi时,您必须分别标识这些字段并手动进行调整。 我有一个类似的问题,但是行距很短,并且通过如下设置每个段落的值来进行了解决:

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);
        }
    }
}

这就像一种魅力。

一种更有效的方法是直接在XML对象上进行操作:

 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();
            }
        }
    }

答案 1 :(得分:-1)

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

CTTextParagraphProperties getDefaultMasterStyle()

添加

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