我使用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 ist消失了,但是我仍然得到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

最佳答案

问题是我使用的库之间存在某些依赖关系。不幸的是,我不能说是哪个图书馆。现在仅适用于saxondocx4j。如果我可以重现我的问题,请在这里添加。

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

关于java - 如何在pptx4j中获取slideLayout的名称?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29238596/

10-11 18:50