简体   繁体   English

如何在libreoffice writer文档中插入数据url /二进制文件以替换图像?

[英]How to insert a data url/binary to replace an image in a libreoffice writer document?

I am creating a report printer in Java using the LibreOffice SDK and Apache Batik. 我正在使用LibreOffice SDK和Apache Batik在Java中创建报告打印机。 Using Batik, I draw svgs which I then insert into a LibreOffice Writer document. 使用蜡染,我绘制了svg,然后将其插入LibreOffice Writer文档中。 To properly insert the image, all I found is using a path to load the image from disk and insert it into the document. 为了正确插入图像,我发现所有使用的是从磁盘加载图像并将其插入文档的路径。 So far so good, but I have to explicitly save the document to disk in order to read it into libreoffice again. 到目前为止一切顺利,但是我必须将文档显式保存到磁盘上才能再次将其读入libreoffice。

I tried to use a data url as the image path but it did not work. 我试图使用数据网址作为图像路径,但是它不起作用。 Are there any possibilities to read an image from a stream or anything else I can use without storing the file to disk? 在不将文件存储到磁盘的情况下,是否可以从流中读取图像或其他任何可用的方法?

I found a solution. 我找到了解决方案。 I realized how to do it when I realized that all my images I added were just image links. 当我意识到我添加的所有图像仅仅是图像链接时,我意识到了该怎么做。 So I had to embed the images instead. 所以我不得不嵌入图像。

To use this, you need: 要使用此功能,您需要:

  • Access to the XComponentContext 访问XComponentContext
  • A TextGraphicObject in your document (see the links above) 您文档中的TextGraphicObject(请参见上面的链接)
  • The image as byte[] or use another stream 图片为byte []或使用其他流

The code: 编码:

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);

This worked effortlessly even for my svg images. 即使对于我的svg图像,这也毫不费力。

Source: https://blog.oio.de/2010/05/14/embed-an-image-into-an-openoffice-org-writer-document/ 资料来源: https : //blog.oio.de/2010/05/14/embed-an-image-into-an-openoffice-org-writer-document/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM