我正在使用 LibreOffice SDK 和 Apache Batik 在 Java 中创建报表打印机。使用蜡染,我绘制了 svg,然后将其插入到 LibreOffice Writer 文档中。为了正确插入图像,我发现的只是使用路径从磁盘加载图像并将其插入到文档中。到目前为止一切顺利,但我必须将文档显式保存到磁盘才能再次将其读入 libreoffice。
我尝试使用数据 url 作为图像路径,但它不起作用。是否有可能从流中读取图像或我可以使用的任何其他内容而不将文件存储到磁盘?
我正在使用 LibreOffice SDK 和 Apache Batik 在 Java 中创建报表打印机。使用蜡染,我绘制了 svg,然后将其插入到 LibreOffice Writer 文档中。为了正确插入图像,我发现的只是使用路径从磁盘加载图像并将其插入到文档中。到目前为止一切顺利,但我必须将文档显式保存到磁盘才能再次将其读入 libreoffice。
我尝试使用数据 url 作为图像路径,但它不起作用。是否有可能从流中读取图像或我可以使用的任何其他内容而不将文件存储到磁盘?
我找到了解决方案。当我意识到我添加的所有图像都只是图像链接时,我意识到该怎么做。所以我不得不嵌入图像。
要使用它,您需要:
编码:
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/