我正在使用Java和LibreOffice API,我想绘制矩形并设置它们的名称,或者在它们上面添加一些文本字段。绘图形状相对容易,但添加文本真的很难。我没有在文档和论坛中找到任何解决方案。
我正在声明形状和文字:
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);
然后我试图设置XText:
xShapeText.setString("ABC");
这就是出现问题的地方(即使在阅读文档中的说明后,我也不清楚这个例外):
com.sun.star.lang.DisposedException 在com.sun.star.lib.uno.environments.remote.JobQueue.removeJob(JobQueue.java:210) 在com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:330) 在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) 在com.ericsson.stpdiagramgenerator.presentation.core.HelloTextTableShape.manipulateText(HelloTextTableShape.java:265) at com.ericsson.stpdiagramgenerator.presentation.core.HelloTextTableShape.useWriter(HelloTextTableShape.java:65) 在com.ericsson.stpdiagramgenerator.presentation.core.HelloTextTableShape.useDocuments(HelloTextTableShape.java:52) 在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) 在com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 引起:java.io.IOException:com.sun.star.io.IOException:已达到EOF - 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) 在java.io.DataInputStream.readInt(DataInputStream.java:387) 在com.sun.star.lib.uno.protocols.urp.urp.readBlock(urp.java:355) 在com.sun.star.lib.uno.protocols.urp.urp.readMessage(urp.java:92) 在com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge $ MessageDispatcher.run(java_remote_bridge.java:105)
也许您有另一种解决方案,可以使用LibreOffice API在形状上插入文本/文本框/文本字段。
答案 0 :(得分:0)
您的代码在我的计算机上正常运行。我在Windows上的LibreOffice 5.1.0.3中进行了测试。这是我使用的代码:
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);
}
要运行它,我打开了一个新的LibreOffice Draw文件,然后按下"运行项目"在NetBeans中。这就是结果:
看起来异常可能是由连接到文档的问题引起的。你究竟是怎么运行宏的?
相关:此问题也发布在https://forum.openoffice.org/en/forum/viewtopic.php?f=20&p=395334,其中包含Basic的解决方案。