2017-08-24 128 views
0

我已经在java中使用openoffice API创建了一个文档。现在我想在我的机器上将该文档保存为pdf。 如何做到这一点?OpenOffice,Java,将文档另存为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) 

例外,但我不把它保存在所需位置。请帮助

+0

如果你想要PDF格式,那么输出格式应该是.pdf,所以试着改变那个String storeUrl =“D:\\ OOo_doc.pdf”; –

+0

尝试过但它不起作用 –

+0

在方法执行之后/之间是否收到任何异常?你在你的机器上安装了openoffice服务吗? –

回答

0

有几件事情,你可以尝试诊断问题的原因:

  1. 解决您的storeUrl在由莫吉的评论中提及。它应该是一个OpenOffice URL。
  2. 请确保您的程序可以写入D:\,其中包含文件夹和OOo_doc.pdf文件(如果它已存在)。如果某个东西已经对OOo_doc.pdf文件有锁定(例如之前的程序运行失败),那么您会发现IO错误。由于Open Office API不一定完全退出,您以前的失败运行可能仍在运行。
  3. 使用OpenOffice GUI使用OpenOffice进行相同的转换。相同的源文件到相同的目标文件。如果这样做,那么你知道它是特定于你的程序,而不是关于Open Office安装,权限等。
  4. 打印e.errCode值并查找它的含义。来自异常的错误代码可能相当有帮助。
相关问题