I want to perform some minor manipulations to a docx document and I want to insert a page break of type= "page" on the place of a certain variable. The document xml is prepared correctly and I have the variable in the ${pagebreak} format.
My script looks like this:
- Code: Select all
File templateZip = new File("resources/doctest.docx")
WordprocessingMLPackage template = WordprocessingMLPackage.load(templateZip)
def map = ["pagebreak": "<w:r><w:br w:type=\"page\"/></w:r>"]
template.mainDocumentPart.variableReplace(map)
At the last line however, I receive the following exception:
- Code: Select all
javax.xml.bind.JAXBException: Preprocessing exception
- with linked exception:
[org.docx4j.openpackaging.exceptions.Docx4JException: Cannot perform the transformation]
at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:574)
at org.docx4j.XmlUtils.unmarshallFromTemplate(XmlUtils.java:636)
at org.docx4j.openpackaging.parts.JaxbXmlPart.variableReplace(JaxbXmlPart.java:303)
at org.docx4j.openpackaging.parts.JaxbXmlPart$variableReplace.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:139)
at ideaGroovyConsole.run(ideaGroovyConsole.groovy:12)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:254)
at groovy.lang.GroovyShell.run(GroovyShell.java:360)
at groovy.lang.GroovyShell.run(GroovyShell.java:339)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite.doInvoke(PlainObjectMetaMethodSite.java:43)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSite.invoke(PogoMetaMethodSite.java:170)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:73)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:148)
at console.run(console.groovy:11)
at groovy.ui.GroovyMain.processReader(GroovyMain.java:631)
at groovy.ui.GroovyMain.processFiles(GroovyMain.java:552)
at groovy.ui.GroovyMain.run(GroovyMain.java:396)
at groovy.ui.GroovyMain.access$1400(GroovyMain.java:68)
at groovy.ui.GroovyMain$GroovyCommand.process(GroovyMain.java:322)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:142)
at groovy.ui.GroovyMain.main(GroovyMain.java:115)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:111)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:129)
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Cannot perform the transformation
at org.docx4j.XmlUtils.transform(XmlUtils.java:1368)
at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:569)
... 32 more
Caused by: javax.xml.transform.TransformerException: com.sun.istack.SAXParseException2; unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"r"). Expected elements are <{ }text>
I saw some other topics related to issues with page breaks in here, but most of them involve pdf generation. I guess it always comes down to the unmarshalling process, but still I can't figure out how to correct this.
Any ideas or hints will be greatly appreciated.