簡體   English   中英

OpenOffice,Java,將文檔另存為pdf

[英]OpenOffice,Java,Saving a document as 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. 按照moggi的評論中所述修復您的storeUrl。 它應該是一個OpenOffice URL。
  2. 確保您可以編程將其寫入D:\\,其中包括文件夾和OOo_doc.pdf文件(如果已存在)。 如果某些東西已經在OOo_doc.pdf文件上鎖定(例如,程序先前運行失敗),則可能會遇到IO錯誤。 由於Open Office API不一定會干凈退出,因此以前的失敗運行可能仍在運行。
  3. 使用Open Office GUI使用OpenOffice進行相同的轉換。 相同的源文件與相同的目標文件。 如果可行,那么您就知道它特定於您的程序,而與Open Office安裝,權限等無關。
  4. 打印e.errCode值並查找其含義。 異常中的錯誤代碼可能會很有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM