【问题标题】:How to insert a data url/binary to replace an image in a libreoffice writer document?如何插入数据 url/二进制文件以替换 libreoffice writer 文档中的图像?
【发布时间】:2017-08-11 14:03:04
【问题描述】:
我正在使用 LibreOffice SDK 和 Apache Batik 在 Java 中创建一个报表打印机。使用蜡染,我绘制了 svg,然后将其插入到 LibreOffice Writer 文档中。为了正确插入图像,我发现的只是使用路径从磁盘加载图像并将其插入到文档中。到目前为止一切顺利,但我必须将文档显式保存到磁盘才能再次将其读入 libreoffice。
我尝试使用数据 url 作为图像路径,但它不起作用。是否有可能从流中读取图像或我可以使用的任何其他内容,而无需将文件存储到磁盘?
【问题讨论】:
标签:
java
libreoffice
uno
libreoffice-writer
【解决方案1】:
我找到了解决方案。当我意识到我添加的所有图像都只是图像链接时,我意识到该怎么做。所以我不得不嵌入图像。
要使用它,您需要:
- 访问 XComponentContext
- 文档中的 TextGraphicObject(请参阅上面的链接)
- 图像作为字节[]或使用另一个流
代码:
Object graphicProviderObject = xComponentContext.getServiceManager().createInstanceWithContext(
"com.sun.star.graphic.GraphicProvider",
xComponentContext);
XGraphicProvider xGraphicProvider = UnoRuntime.queryInterface(
XGraphicProvider.class, graphicProviderObject);
PropertyValue[] v = new PropertyValue[1];
v[0] = new PropertyValue();
v[0].Name = "InputStream";
v[0].Value = new ByteArrayToXInputStreamAdapter(imageAsByteArray);
XGraphic graphic = xGraphicProvider.queryGraphic(v);
if (graphic == null) {
LOGGER.error("Error loading the image");
return;
}
XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, textGraphicObject);
// Set the image
xProps.setPropertyValue("Graphic", graphic);
即使对于我的 svg 图片,这也毫不费力。
来源:https://blog.oio.de/2010/05/14/embed-an-image-into-an-openoffice-org-writer-document/