Search code examples
javalibreofficelibreoffice-draw

Java LibreOffice Draw - Set text of a shape


I'm using Java and the LibreOffice API, and I'd like to draw rectangles and set their names, or put some text fields on them. Drawing shapes was relatively easy, but adding text is really hard. I didn't find any solution, neither in documentation nor at forums.

I am declaring the shape and text like this:

Object drawShape = xDrawFactory.createInstance("com.sun.star.drawing.RectangleShape");
XShape xDrawShape = UnoRuntime.queryInterface(XShape.class, drawShape);
xDrawShape.setSize(new Size(10000, 20000));
xDrawShape.setPosition(new Point(5000, 5000));
xDrawPage.add(xDrawShape);

XText xShapeText = UnoRuntime.queryInterface(XText.class, drawShape);
XPropertySet xShapeProps = UnoRuntime.queryInterface(XPropertySet.class, drawShape);

And then I am trying to set XText:

xShapeText.setString("ABC");

And this is where the problem appears (this exception is not clear for me even after reading the explanation from documentation):

com.sun.star.lang.DisposedException at com.sun.star.lib.uno.environments.remote.JobQueue.removeJob(JobQueue.java:210) at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:330) at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:303) at com.sun.star.lib.uno.environments.remote.JavaThreadPool.enter(JavaThreadPool.java:87) at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge.sendRequest(java_remote_bridge.java:636) at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.request(ProxyFactory.java:146) at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.invoke(ProxyFactory.java:128) at com.sun.proxy.$Proxy6.setString(Unknown Source) at com.ericsson.stpdiagramgenerator.presentation.core.HelloTextTableShape.manipulateText(HelloTextTableShape.java:265) at com.ericsson.stpdiagramgenerator.presentation.core.HelloTextTableShape.useWriter(HelloTextTableShape.java:65) at com.ericsson.stpdiagramgenerator.presentation.core.HelloTextTableShape.useDocuments(HelloTextTableShape.java:52) at com.ericsson.stpdiagramgenerator.presentation.core.HelloTextTableShape.main(HelloTextTableShape.java:42) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) Caused by: java.io.IOException: com.sun.star.io.IOException: EOF reached - socket,host=localhost,port=8100,localHost=localhost.localdomain,localPort=34456,peerHost=localhost,peerPort=8100 at com.sun.star.lib.uno.bridges.java_remote.XConnectionInputStream_Adapter.read(XConnectionInputStream_Adapter.java:55) at java.io.DataInputStream.readInt(DataInputStream.java:387) at com.sun.star.lib.uno.protocols.urp.urp.readBlock(urp.java:355) at com.sun.star.lib.uno.protocols.urp.urp.readMessage(urp.java:92) at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge$MessageDispatcher.run(java_remote_bridge.java:105)

Maybe you have another solution for inserting text/textbox/textfield on a shape with the LibreOffice API.


Solution

  • Your code works fine on my machine. I tested it in LibreOffice 5.1.0.3 on Windows. Here is the code I used:

    com.sun.star.frame.XDesktop xDesktop = null;
    // getDesktop() is from
    // https://wiki.openoffice.org/wiki/API/Samples/Java/Writer/BookmarkInsertion
    xDesktop = getDesktop();
    com.sun.star.lang.XComponent xComponent = null;
    try {
        xComponent = xDesktop.getCurrentComponent();
        XDrawPagesSupplier xDrawPagesSupplier = 
            (XDrawPagesSupplier)UnoRuntime.queryInterface(
                XDrawPagesSupplier.class, xComponent);
        Object drawPages = xDrawPagesSupplier.getDrawPages();
        XIndexAccess xIndexedDrawPages = (XIndexAccess)
            UnoRuntime.queryInterface(
            XIndexAccess.class, drawPages);
        Object drawPage = xIndexedDrawPages.getByIndex(0);
        XMultiServiceFactory xDrawFactory = 
            (XMultiServiceFactory)UnoRuntime.queryInterface(
                XMultiServiceFactory.class, xComponent);
        Object drawShape = xDrawFactory.createInstance(
            "com.sun.star.drawing.RectangleShape");
        XDrawPage xDrawPage = (XDrawPage)UnoRuntime.queryInterface(
            XDrawPage.class, drawPage);
        XShape xDrawShape = UnoRuntime.queryInterface(XShape.class, drawShape);
        xDrawShape.setSize(new Size(10000, 20000));
        xDrawShape.setPosition(new Point(5000, 5000));
        xDrawPage.add(xDrawShape);
    
        XText xShapeText = UnoRuntime.queryInterface(XText.class, drawShape);
        XPropertySet xShapeProps = UnoRuntime.queryInterface(
            XPropertySet.class, drawShape);
        xShapeText.setString("DEF");
    } catch( Exception e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
    

    To run it, I opened a new LibreOffice Draw file, then pressed "Run Project" in NetBeans. This was the result:

    enter image description here

    It looks like the exception may be caused by a problem with connecting to the document. How exactly are you running the macro?

    Related: This question is also posted at https://forum.openoffice.org/en/forum/viewtopic.php?f=20&p=395334, which contains a solution in Basic.