【问题标题】:OpenOffice,Java,Saving a document as pdfOpenOffice,Java,将文档另存为 pdf
【发布时间】:2017-08-24 05:28:29
【问题描述】:

我在 java 中使用 openoffice API 创建了一个文档。现在我想将该文档另存为 pdf 在我的机器上。 该怎么做?

创建文档的代码是

// Create a document
       XComponent xdocument = xCLoader.loadComponentFromURL("private:factory/swriter", "_blank", 0, new PropertyValue[0]);

       // Get the textdocument
       XTextDocument aTextDocument = ( XTextDocument )UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, xdocument);

       // Get its text
       XText xText = aTextDocument.getText();
       XTextRange xTextRange = xText.createTextCursor();
       ((XTextCursor)xTextRange).gotoEnd(true);

现在我想保存该文档。但我不能这样做。你能帮我解决这个问题吗?

我用来保存的代码是

 //close the document
       XCloseable xcloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, xdocument);
       xcloseable.close(false);

       // the url where the document is to be saved
    String storeUrl = "D:\\OOo_doc.pdf";

    // Save the document
    XStorable xStorable = ( XStorable )UnoRuntime.queryInterface(XStorable.class, xdocument);
     PropertyValue[] storeProps = new PropertyValue[0];
    storeProps = new PropertyValue[1];
storeProps[0] = new PropertyValue();
storeProps[0].Name = "FilterName";
storeProps[0].Value = "writer_pdf_Export";

xStorable.storeToURL(storeUrl, storeProps);

这是我得到的例外

Exception in thread "main" com.sun.star.task.ErrorCodeIOException: 
    at com.sun.star.lib.uno.environments.remote.Job.remoteUnoRequestRaisedException(Job.java:182)
    at com.sun.star.lib.uno.environments.remote.Job.execute(Job.java:148)
    at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:344)
    at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:313)
    at com.sun.star.lib.uno.environments.remote.JavaThreadPool.enter(JavaThreadPool.java:101)
    at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge.sendRequest(java_remote_bridge.java:652)
    at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.request(ProxyFactory.java:154)
    at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.invoke(ProxyFactory.java:136)
    at com.sun.proxy.$Proxy10.storeToURL(Unknown Source)
    at test.oo.main(oo.java:83)

但我不会将它保存在所需的位置。请帮忙

【问题讨论】:

  • 如果你想要 PDF 格式,那么输出格式应该是 .pdf,所以尝试更改 String storeUrl = "D:\\OOo_doc.pdf";
  • 试过了,还是不行
  • 在方法执行之后/之间是否有任何异常?你的机器上安装openoffice服务了吗?
  • 是的,我得到了一个例外。添加了异常
  • 在尝试导出内容之前不应关闭文档。此外,storeToURL 方法需要一个 URL 而不是路径。尝试使用 file:///D:/OOo_doc.pdf 作为您的 URL。

标签: java document openoffice.org uno


【解决方案1】:

您可以尝试通过以下几种方法来诊断问题的原因:

  1. 修复 moggi 评论中提到的 storeUrl。它应该是一个 OpenOffice URL。
  2. 确保您可以编程可以写入 D:\,其中包括文件夹和 OOo_doc.pdf 文件(如果已存在)。如果 OOo_doc.pdf 文件上已经有某些东西被锁定(例如您的程序之前运行失败),那么您可能会遇到 IO 错误。由于 Open Office API 不一定完全退出,您之前失败的运行可能仍在运行。
  3. 使用 OpenOffice GUI 执行相同的转换。相同的源文件到相同的目标文件。如果可行,那么您就知道它特定于您的程序,而不是与 Open Office 安装、权限等有关。
  4. 打印 e.errCode 值并查看它的含义。异常中的错误代码可能会很有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 2014-04-16
    • 2023-03-24
    • 2015-01-09
    • 2015-12-16
    • 1970-01-01
    相关资源
    最近更新 更多