![](/img/trans.png)
[英]How to insert an image in to an openoffice writer document with java?
[英]How to insert a data url/binary to replace an image in a libreoffice writer document?
我正在使用LibreOffice SDK和Apache Batik在Java中创建报告打印机。 使用蜡染,我绘制了svg,然后将其插入LibreOffice Writer文档中。 为了正确插入图像,我发现所有使用的是从磁盘加载图像并将其插入文档的路径。 到目前为止一切顺利,但是我必须将文档显式保存到磁盘上才能再次将其读入libreoffice。
我试图使用数据网址作为图像路径,但是它不起作用。 在不将文件存储到磁盘的情况下,是否可以从流中读取图像或其他任何可用的方法?
我找到了解决方案。 当我意识到我添加的所有图像仅仅是图像链接时,我意识到了该怎么做。 所以我不得不嵌入图像。
要使用此功能,您需要:
编码:
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/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.