如何在pptx4j中获取slideLayout的名称?

时间:2015-03-24 16:58:29

标签: java powerpoint docx4j

我使用pptx4j加载现有的演示文稿,我需要每个布局的名称。使用以下代码,它应该在我看来工作:

    // loading layout
    SlideLayoutPart layoutPart = null;
    try {
        PartName partName = new PartName("/ppt/slideLayouts/slideLayout1.xml");
        layoutPart = (SlideLayoutPart) presentationMLPackage.getParts().getParts().get(partName);
        // get name
        String layoutName = layoutPart.getContents().getCSld().getName();
        log.debug("LayoutName: " + layoutName);
    } catch (InvalidFormatException e) {
        log.error("Error - Could not load layout: " + e);
    } catch (Docx4JException e) {
        log.error("Error - Could not get layoutName: " + e);
    }

但我得到了layoutPart.getJaxbElement()的NullPointerException。这是错误的方式还是我为什么会收到此错误?

整个例外:

java.lang.IllegalArgumentException: Unknown type of result: class javax.xml.transform.dom.DOMResult 
        at net.sf.saxon.event.SerializerFactory.getReceiver(SerializerFactory.java:154)
        at net.sf.saxon.IdentityTransformerHandler.startDocument(IdentityTransformerHandler.java:94)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.DomLoader$State.<init>(DomLoader.java:67)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.DomLoader.startElement(DomLoader.java:102)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ProxyLoader.startElement(ProxyLoader.java:45)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:486)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:60)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135)
        at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:229)
        at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:266)
        at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:235)
        at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:266)
        at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:235)
        at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:266)
        at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:235)
        at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:266)
        at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:235)
        at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:112)
        at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:95)
        at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:88)
        at com.sun.xml.internal.bind.v2.runtime.BinderImpl.associativeUnmarshal(BinderImpl.java:146)
        at com.sun.xml.internal.bind.v2.runtime.BinderImpl.unmarshal(BinderImpl.java:117)
        at org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware.unmarshal(JaxbXmlPartXPathAware.java:316)
        at org.docx4j.openpackaging.parts.JaxbXmlPart.getJaxbElement(JaxbXmlPart.java:130)
        at pptx4j.PPTXConverter.create(Converter.java:83)
        at Starter.main(Starter.java:17)
Exception in thread "main" java.lang.NullPointerException
        at pptx4j.PPTXConverter.create(PPTXConverter.java:83)
        at Starter.main(Starter.java:17)

由于内容中存在slideLayout1.xml,我可以使用它创建新幻灯片。

我希望有人可以给我一个提示。如果需要其他信息,请询问。

更新

我使用的是3.0.1版本,因此我更新到3.2.1并稍微更改了我的代码。现在NullPointerException已经消失,但我仍然得到layoutPart.getContents().getCSld()的IllegalArgumentException:

INFO  JaxbXmlPart - Lazily unmarshalling /ppt/slideLayouts/slideLayout1.xml
INFO  JaxbXmlPartXPathAware - For org.docx4j.openpackaging.parts.PresentationML.SlideLayoutPart, unmarshall via binder
DEBUG JaxbXmlPartXPathAware - info: com.sun.xml.internal.bind.v2.runtime.BinderImpl
ERROR JaxbXmlPartXPathAware - Unknown type of result: class javax.xml.transform.dom.DOMResult
java.lang.IllegalArgumentException: Unknown type of result: class javax.xml.transform.dom.DOMResult

2 个答案:

答案 0 :(得分:1)

问题是我使用的库之间存在某些依赖关系。不幸的是,我不能说哪个库。只有saxondocx4j现在才有效。如果我可以重现我的问题,我会在这里添加它。

@JasonPlutext感谢您的回答和提示!

答案 1 :(得分:0)

看起来您正在正确访问零件集合。

假设layoutPart不为null,但layoutPart.getJaxbElement()为null,则问题是您尚未向布局部件添加任何内容。

做类似的事情:

layoutPart.setJaxbElement(new SldLayout() )

请注意,getCSld()也将返回null,除非您先给它一个值。

作为旁注,您始终可以将pptx上传到http://webapp.docx4java.org/OnlineDemo/PartsList.html,以便以交互方式检查其内容。