有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

OpenOffice,Java,将文档另存为pdf

我已经用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)

但我不会把它保存在想要的位置。请帮忙


共 (1) 个答案

  1. # 1 楼答案

    有几种方法可以尝试诊断问题的原因:

    1. 修复moggi评论中提到的storeUrl。它应该是一个OpenOffice URL
    2. 确保您可以将程序写入D:\中,其中包括文件夹和OOU文档。pdf文件(如果已存在)。如果某物已经锁定OO_文档。pdf文件(例如程序的前一次失败运行),则可能会出现IO错误。由于OpenOfficeAPI不一定完全退出,因此您以前失败的运行可能仍在运行
    3. 使用OpenOffice GUI使用OpenOffice执行相同的转换。同一源文件到同一目标文件。如果这样做有效,那么您就知道这是特定于您的程序的,而不是有关OpenOffice安装、权限等的内容
    4. 打印e.errCode值并查找其含义。异常中的错误代码可能非常有用