在Java中设置pptx主题

8
我正在尝试使用Java编程方式合并一些pptx文档。我基本上已经想出了如何使用Apache POI进行操作,但我要合并的文档无法正常工作。
经过大量搜索和试错,我发现问题的原因是pptx文档没有主题信息(即,如果我在PowerPoint中单击并检查幻灯片母版视图,则为空白)。如果我进入“设计”选项卡中的“主题”并选择“Office主题”或其他主题,然后保存,文件将顺利合并。否则,我会遇到以下错误:
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
在某些情况下,当您生成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

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

获取默认的主样式CTTextParagraphProperties()

添加

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接