Sarthak Jain
Sarthak Jain

Reputation: 3

OpenOffice,Java,Saving a document as pdf

I have created a document using openoffice API in java. Now I want to save that document as pdf on my machine. How to do that?

Code for creation of document is

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

Now I want to save that document. But i am not able to do so. Can you help me with that?

Code which I am using for saving is

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

This is the exception i am getting

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)

But I am not to get it saved at the desired location. Please help

Upvotes: 0

Views: 1289

Answers (1)

Paul Jowett
Paul Jowett

Reputation: 6581

There are several things you can try to diagnose the cause of the problem:

  1. Fix your storeUrl as mentioned in the comment by moggi. It should be an OpenOffice URL.
  2. make sure you can program can write to D:\ which includes the folder and the OOo_doc.pdf file if it already exists. If something already has a lock on the OOo_doc.pdf file (such as a previous failed run of your program) then you would expect an IO error. Since the Open Office API doesn't necessarily exit cleanly your previous failed runs might still be running.
  3. Do the same conversion using OpenOffice using the Open Office GUI. Same source file to same destination file. If that works then you know it is specific to your program and not something about the Open Office install, permissions etc.
  4. Print e.errCode value and look up what it means. The error code from the exception might be quite helpful.

Upvotes: 0

Related Questions