2017-08-11 130 views
0

我使用LibreOffice SDK和Apache Batik在Java中创建报表打印机。使用蜡染,我画svgs,然后插入LibreOffice Writer文档。为了正确地插入图像,我找到的所有内容都是使用路径从磁盘加载图像并将其插入到文档中。到目前为止这么好,但我必须明确地将文档保存到磁盘,以便再次将它读入libreoffice。如何插入数据url /二进制文件以替换libreoffice writer文档中的图像?

我试图使用一个数据网址作为图像路径,但它没有奏效。有没有可能从流中读取图像或其他任何我可以使用而不将文件存储到磁盘?

回答

0

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

要使用此,你需要:

  • 访问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/

相关问题