本文介绍了XPage 的 POI - 在富文本字段中将 Word 文档另存为附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 OpenNTF POI 4 XPage 插件.这对于生成 Word 文档非常有效.现在,在生成 Word 文档后,我想创建一个新的响应文档并将该 Word 文档作为附件存储在此响应文档的富文本字段中.这是我的代码(在 POI 4 XPages 小部件的 poi postGenerationProcess 属性中):

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?

实施 Knut Hermann 提供的以下建议后,我得到:

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

错误:文档生成过程中出错兴趣点库:1.2.6.201312211419堆栈跟踪:com.ibm.xsp.exception.EvaluationExceptionEx:执行 JavaScript 操作表达式时出错在 com.ibm.xsp.binding.javascript.JavaScriptMethodBinding.invoke(JavaScriptMethodBinding.java:126)在 biz.webgate.dominoext.poi.component.containers.UIDocument$1.run(UIDocument.java:300)在 biz.webgate.dominoext.poi.component.containers.UIDocument$1.run(UIDocument.java:1)在 java.security.AccessController.doPrivileged(AccessController.java:251)在 biz.webgate.dominoext.poi.component.containers.UIDocument.doPostGenerationProcessPrivileged(UIDocument.java:298)在 biz.webgate.dominoext.poi.component.containers.UIDocument.postGenerationProcess(UIDocument.java:290)在 biz.webgate.dominoext.poi.component.kernel.DocumentProcessor.processDocument(DocumentProcessor.java:233)在 biz.webgate.dominoext.poi.component.kernel.DocumentProcessor.generateNewFile(DocumentProcessor.java:143)在 biz.webgate.dominoext.poi.component.containers.UIDocument.processAjaxRequest(UIDocument.java:208)在 biz.webgate.dominoext.poi.component.actions.DocumentGenerationServerAction.invoke(DocumentGenerationServerAction.java:48)在 com.ibm.xsp.actions.ActionGroup.invoke(ActionGroup.java:135)在 com.ibm.xsp.application.ActionListenerImpl.processAction(ActionListenerImpl.java:60)在 javax.faces.component.UICommand.broadcast(UICommand.java:324)在 com.ibm.xsp.component.UIEventHandler.broadcast(UIEventHandler.java:366)在 com.ibm.xsp.component.UIDataPanelBase.broadcast(UIDataPanelBase.java:400)在 com.ibm.xsp.component.UIDataPanelBase.broadcast(UIDataPanelBase.java:400)在 com.ibm.xsp.component.UIDataPanelBase.broadcast(UIDataPanelBase.java:400)在 com.ibm.xsp.extlib.component.layout.UIVarPublisherBase.broadcast(UIVarPublisherBase.java:183)在 com.ibm.xsp.component.UIDataPanelBase.broadcast(UIDataPanelBase.java:400)在 com.ibm.xsp.component.UIViewRootEx.broadcast(UIViewRootEx.java:1535)在 javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:307)在 javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:428)在 com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:94)在 com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:210)在 com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:96)在 com.ibm.xsp.controller.FacesControllerImpl.execute(FacesControllerImpl.java:250)在 com.ibm.xsp.webapp.FacesServlet.serviceView(FacesServlet.java:223)在 com.ibm.xsp.webapp.FacesServletEx.serviceView(FacesServletEx.java:204)在 com.ibm.xsp.webapp.FacesServlet.service(FacesServlet.java:160)在 com.ibm.xsp.webapp.FacesServletEx.service(FacesServletEx.java:138)在 com.ibm.xsp.webapp.DesignerFacesServlet.service(DesignerFacesServlet.java:103)在 com.ibm.designer.runtime.domino.adapter.ComponentModule.invokeServlet(ComponentModule.java:576)在 com.ibm.domino.xsp.module.nsf.NSFComponentModule.invokeServlet(NSFComponentModule.java:1281)在 com.ibm.designer.runtime.domino.adapter.ComponentModule$AdapterInvoker.invokeServlet(ComponentModule.java:847)在 com.ibm.designer.runtime.domino.adapter.ComponentModule$ServletInvoker.doService(ComponentModule.java:796)在 com.ibm.designer.runtime.domino.adapter.ComponentModule.doService(ComponentModule.java:565)在 com.ibm.domino.xsp.module.nsf.NSFComponentModule.doService(NSFComponentModule.java:1265)在 com.ibm.domino.xsp.module.nsf.NSFService.doServiceInternal(NSFService.java:653)在 com.ibm.domino.xsp.module.nsf.NSFService.doService(NSFService.java:476)在 com.ibm.designer.runtime.domino.adapter.LCDEnvironment.doService(LCDEnvironment.java:341)在 com.ibm.designer.runtime.domino.adapter.LCDEnvironment.service(LCDEnvironment.java:297)在 com.ibm.domino.xsp.bridge.http.engine.XspCmdManager.service(XspCmdManager.java:272)引起:com.ibm.jscript.InterpretException:脚本解释器错误,line=10,col=14:在 java 类org.apache.poi.xwpf.usermodel"上调用方法write(java.io.FileOutputStream)"时出错.XWPF文档'在 com.ibm.jscript.types.JavaAccessObject.call(JavaAccessObject.java:334)在 com.ibm.jscript.types.FBSObject.call(FBSObject.java:161)在 com.ibm.jscript.ASTTree.ASTCall.interpret(ASTCall.java:175)在 com.ibm.jscript.ASTTree.ASTProgram.interpret(ASTProgram.java:119)在 com.ibm.jscript.ASTTree.ASTProgram.interpretEx(ASTProgram.java:139)在 com.ibm.jscript.JSExpression._interpretExpression(JSExpression.java:435)在 com.ibm.jscript.JSExpression.access$1(JSExpression.java:424)在 com.ibm.jscript.JSExpression$2.run(JSExpression.java:414)在 java.security.AccessController.doPrivileged(AccessController.java:284)在 com.ibm.jscript.JSExpression.interpretExpression(JSExpression.java:410)在 com.ibm.jscript.JSExpression.evaluateValue(JSExpression.java:251)在 com.ibm.jscript.JSExpression.evaluateValue(JSExpression.java:234)在 com.ibm.xsp.javascript.JavaScriptInterpreter.interpret(JavaScriptInterpreter.java:221)在 com.ibm.xsp.binding.javascript.JavaScriptMethodBinding.invoke(JavaScriptMethodBinding.java:111)……还有 41 个引起:org.apache.poi.POIXMLException: java.lang.IllegalStateException: Access denied (java.lang.RuntimePermission getClassLoader)在 org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:141)在 org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:177)在 sun.reflect.GeneratedMethodAccessor163.invoke(未知来源)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)在 java.lang.reflect.Method.invoke(Method.java:611)在 com.ibm.jscript.types.JavaAccessObject.call(JavaAccessObject.java:321)... 54 更多引起:java.lang.IllegalStateException:访问被拒绝(java.lang.RuntimePermission getClassLoader)在 org.apache.xmlbeans.XmlBeans.getContextTypeLoader(XmlBeans.java:336)在 org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument$Factory.parse(来源不明)在 org.apache.poi.POIXMLProperties.(POIXMLProperties.java:75)在 org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:139)... 59 更多引起:java.security.AccessControlException: Access denied (java.lang.RuntimePermission getClassLoader)在 java.security.AccessController.checkPermission(AccessController.java:108)在 java.lang.SecurityManager.checkPermission(SecurityManager.java:544)在 java.lang.Thread.getContextClassLoader(Thread.java:456)在 org.apache.xmlbeans.impl.schema.SchemaTypeLoaderImpl.getContextTypeLoader(SchemaTypeLoaderImpl.java:131)在 sun.reflect.GeneratedMethodAccessor128.invoke(未知来源)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)在 java.lang.reflect.Method.invoke(Method.java:611)在 org.apache.xmlbeans.XmlBeans.getContextTypeLoader(XmlBeans.java:327)... 62 更多

Error : Error during DocumentgenerationPOI LIB : 1.2.6.201312211419StackTrace: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 moreCaused 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 moreCaused 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 moreCaused 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

推荐答案

将 Word 文档保存到文件系统的临时文件夹中,然后使用 rtitem.embedObject 将其附加到 RichTextItem:

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();

如果您不想将文件保存到文件系统中,则创建一个 MIMEEntity 并将 Word 内容直接流式传输到 Notes 文档 (Java 代码片段作为起点).

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).

这篇关于XPage 的 POI - 在富文本字段中将 Word 文档另存为附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:28