如何插入数据url / binary来替换libreoffice编写器文档中的图像?

问题描述 投票:0回答:1

我正在使用LibreOffice SDK和Apache Batik在Java中创建报表打印机。使用Batik,我绘制svgs然后插入到LibreOffice Writer文档中。为了正确插入图像,我发现只是使用一个路径从磁盘加载图像并将其插入到文档中。到目前为止一切都很好,但我必须明确地将文档保存到磁盘,以便再次将其读入libreoffice。

我试图使用数据网址作为图像路径但它不起作用。是否有可能从流中读取图像或我可以使用的任何其他内容而无需将文件存储到磁盘?

java libreoffice uno libreoffice-writer
1个回答
0
投票

我找到了解决方案。当我意识到我添加的所有图像都只是图像链接时,我意识到该怎么做。所以我不得不嵌入图像。

要使用它,您需要:

  • 访问XComponentContext
  • 文档中的TextGraphicObject(参见上面的链接)
  • 图像为byte []或使用另一个流

代码:

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/

© www.soinside.com 2019 - 2024. All rights reserved.