2017-08-11 129 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/

相關問題