POI for XPages - save Word document as attachment

2019-05-19 04:10发布

问题:

I'm using the OpenNTF POI 4 XPages plugin. This works very well for generating Word Documents. Now, after generating the Word document, I would like to create a new response document and store the word document as an attachment in a rich text field in this response document. Here is my code (in the poi postGenerationProcess property of the POI 4 XPages widget):

var doc:NotesDocument = currentDocument.getDocument();
var rdoc:NotesDocument = database.createDocument();
rdoc.appendItemValue("Form", "frmRespTempl");
rdoc.appendItemValue("Subject", "Embedded Word Document");
var rtitem:NotesRichTextItem = rdoc.createRichTextItem("Body");
rtitem.embedObject(xwpfdocument);
rdoc.makeResponse(doc);
rdoc.save();

Unfortunately, this gives me the error:

com.ibm.jscript.InterpretException: Script interpreter error, line=13, 
col=8: [TypeError] Method NotesRichTextItem.embedObject(org.apache.poi.xwpf.usermodel.XWPFDocument)
not found, or illegal parameters

Any tips how I can achieve this?

Implementing the suggestion offered below by Knut Hermann, I get:

POI 4 XPages -> ERROR

Error : Error during Documentgeneration POI LIB : 1.2.6.201312211419 StackTrace: com.ibm.xsp.exception.EvaluationExceptionEx: Error while executing JavaScript action expression at com.ibm.xsp.binding.javascript.JavaScriptMethodBinding.invoke(JavaScriptMethodBinding.java:126) at biz.webgate.dominoext.poi.component.containers.UIDocument$1.run(UIDocument.java:300) at biz.webgate.dominoext.poi.component.containers.UIDocument$1.run(UIDocument.java:1) at java.security.AccessController.doPrivileged(AccessController.java:251) at biz.webgate.dominoext.poi.component.containers.UIDocument.doPostGenerationProcessPrivileged(UIDocument.java:298) at biz.webgate.dominoext.poi.component.containers.UIDocument.postGenerationProcess(UIDocument.java:290) at biz.webgate.dominoext.poi.component.kernel.DocumentProcessor.processDocument(DocumentProcessor.java:233) at biz.webgate.dominoext.poi.component.kernel.DocumentProcessor.generateNewFile(DocumentProcessor.java:143) at biz.webgate.dominoext.poi.component.containers.UIDocument.processAjaxRequest(UIDocument.java:208) at biz.webgate.dominoext.poi.component.actions.DocumentGenerationServerAction.invoke(DocumentGenerationServerAction.java:48) at com.ibm.xsp.actions.ActionGroup.invoke(ActionGroup.java:135) at com.ibm.xsp.application.ActionListenerImpl.processAction(ActionListenerImpl.java:60) at javax.faces.component.UICommand.broadcast(UICommand.java:324) at com.ibm.xsp.component.UIEventHandler.broadcast(UIEventHandler.java:366) at com.ibm.xsp.component.UIDataPanelBase.broadcast(UIDataPanelBase.java:400) at com.ibm.xsp.component.UIDataPanelBase.broadcast(UIDataPanelBase.java:400) at com.ibm.xsp.component.UIDataPanelBase.broadcast(UIDataPanelBase.java:400) at com.ibm.xsp.extlib.component.layout.UIVarPublisherBase.broadcast(UIVarPublisherBase.java:183) at com.ibm.xsp.component.UIDataPanelBase.broadcast(UIDataPanelBase.java:400) at com.ibm.xsp.component.UIViewRootEx.broadcast(UIViewRootEx.java:1535) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:307) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:428) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:94) at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:210) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:96) at com.ibm.xsp.controller.FacesControllerImpl.execute(FacesControllerImpl.java:250) at com.ibm.xsp.webapp.FacesServlet.serviceView(FacesServlet.java:223) at com.ibm.xsp.webapp.FacesServletEx.serviceView(FacesServletEx.java:204) at com.ibm.xsp.webapp.FacesServlet.service(FacesServlet.java:160) at com.ibm.xsp.webapp.FacesServletEx.service(FacesServletEx.java:138) at com.ibm.xsp.webapp.DesignerFacesServlet.service(DesignerFacesServlet.java:103) at com.ibm.designer.runtime.domino.adapter.ComponentModule.invokeServlet(ComponentModule.java:576) at com.ibm.domino.xsp.module.nsf.NSFComponentModule.invokeServlet(NSFComponentModule.java:1281) at com.ibm.designer.runtime.domino.adapter.ComponentModule$AdapterInvoker.invokeServlet(ComponentModule.java:847) at com.ibm.designer.runtime.domino.adapter.ComponentModule$ServletInvoker.doService(ComponentModule.java:796) at com.ibm.designer.runtime.domino.adapter.ComponentModule.doService(ComponentModule.java:565) at com.ibm.domino.xsp.module.nsf.NSFComponentModule.doService(NSFComponentModule.java:1265) at com.ibm.domino.xsp.module.nsf.NSFService.doServiceInternal(NSFService.java:653) at com.ibm.domino.xsp.module.nsf.NSFService.doService(NSFService.java:476) at com.ibm.designer.runtime.domino.adapter.LCDEnvironment.doService(LCDEnvironment.java:341) at com.ibm.designer.runtime.domino.adapter.LCDEnvironment.service(LCDEnvironment.java:297) at com.ibm.domino.xsp.bridge.http.engine.XspCmdManager.service(XspCmdManager.java:272) Caused by: com.ibm.jscript.InterpretException: Script interpreter error, line=10, col=14: Error calling method 'write(java.io.FileOutputStream)' on java class 'org.apache.poi.xwpf.usermodel.XWPFDocument' at com.ibm.jscript.types.JavaAccessObject.call(JavaAccessObject.java:334) at com.ibm.jscript.types.FBSObject.call(FBSObject.java:161) at com.ibm.jscript.ASTTree.ASTCall.interpret(ASTCall.java:175) at com.ibm.jscript.ASTTree.ASTProgram.interpret(ASTProgram.java:119) at com.ibm.jscript.ASTTree.ASTProgram.interpretEx(ASTProgram.java:139) at com.ibm.jscript.JSExpression._interpretExpression(JSExpression.java:435) at com.ibm.jscript.JSExpression.access$1(JSExpression.java:424) at com.ibm.jscript.JSExpression$2.run(JSExpression.java:414) at java.security.AccessController.doPrivileged(AccessController.java:284) at com.ibm.jscript.JSExpression.interpretExpression(JSExpression.java:410) at com.ibm.jscript.JSExpression.evaluateValue(JSExpression.java:251) at com.ibm.jscript.JSExpression.evaluateValue(JSExpression.java:234) at com.ibm.xsp.javascript.JavaScriptInterpreter.interpret(JavaScriptInterpreter.java:221) at com.ibm.xsp.binding.javascript.JavaScriptMethodBinding.invoke(JavaScriptMethodBinding.java:111) ... 41 more Caused by: org.apache.poi.POIXMLException: java.lang.IllegalStateException: Access denied (java.lang.RuntimePermission getClassLoader) at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:141) at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:177) at sun.reflect.GeneratedMethodAccessor163.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37) at java.lang.reflect.Method.invoke(Method.java:611) at com.ibm.jscript.types.JavaAccessObject.call(JavaAccessObject.java:321) ... 54 more Caused by: java.lang.IllegalStateException: Access denied (java.lang.RuntimePermission getClassLoader) at org.apache.xmlbeans.XmlBeans.getContextTypeLoader(XmlBeans.java:336) at org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument$Factory.parse(Unknown Source) at org.apache.poi.POIXMLProperties.(POIXMLProperties.java:75) at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:139) ... 59 more Caused by: java.security.AccessControlException: Access denied (java.lang.RuntimePermission getClassLoader) at java.security.AccessController.checkPermission(AccessController.java:108) at java.lang.SecurityManager.checkPermission(SecurityManager.java:544) at java.lang.Thread.getContextClassLoader(Thread.java:456) at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderImpl.getContextTypeLoader(SchemaTypeLoaderImpl.java:131) at sun.reflect.GeneratedMethodAccessor128.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37) at java.lang.reflect.Method.invoke(Method.java:611) at org.apache.xmlbeans.XmlBeans.getContextTypeLoader(XmlBeans.java:327) ... 62 more

回答1:

Save your Word document to file system in a temporary folder and then attach it from there with rtitem.embedObject to your RichTextItem:

var temp = java.lang.System.getProperty("java.io.tmpdir");
var file = new java.io.File(temp + "YourFile.docx"); 
var fileOutputStream = new java.io.FileOutputStream(file);
xwpfdocument.write(fileOutputStream);
fileOutputStream.close();

var doc:NotesDocument = currentDocument.getDocument();
var rdoc:NotesDocument = database.createDocument();
rdoc.appendItemValue("Form", "frmRespTempl");
rdoc.appendItemValue("Subject", "Embedded Word Document");
var rtitem:RichTextItem = rdoc.createRichTextItem("Body");
rtitem.embedObject(lotus.domino.local.EmbeddedObject.EMBED_ATTACHMENT, "",
                   file.getAbsolutePath(), null);
rdoc.makeResponse(doc);
rdoc.save();

If you don't want to save the file into file system then create a MIMEEntity instead and stream the Word content direct to Notes document (Java code snippet as a starting point).



回答2:

The Notes API requires a file, not a Java object. I would suggest you use the MIME document approach where you Base64 the Doc as mime part