2011-06-04 119 views
5

我試圖從模板創建一個openoffice編寫器文檔。 我可以與此代碼如何使用java將圖像插入到openoffice writer文檔中?

private static void searchAndReplace(final String search, 
     final String replace, final XTextDocument mxDoc) { 
    XReplaceable xReplaceable = (XReplaceable) UnoRuntime.queryInterface(
      XReplaceable.class, mxDoc); 
    XReplaceDescriptor xRepDesc = xReplaceable.createReplaceDescriptor(); 
    xRepDesc.setSearchString(search); 
    xRepDesc.setReplaceString(replace); 
    xReplaceable.replaceAll(xRepDesc); 
} 

我發現了一些示例代碼從here到鏈接或嵌入的圖像成xTextDocument替換報告的文本部分。 但是,我無法插入到xTextDocument中。有沒有其他的方式來與Java做到這一點? Openoffice版本是3.1.0。

任何答案?

回答

1

我發現這個在這裏:https://svn.apache.org/repos/asf/openoffice/ooo-site/trunk/content/api/Examples/Snippets/Writer/Writer.EmbedAGraphicIntoATextdocument.snip

private void embedGraphic(GraphicInfo grProps, 
      XMultiServiceFactory xMSF, XTextCursor xCursor) { 

    XNameContainer xBitmapContainer = null; 
    XText xText = xCursor.getText(); 
    XTextContent xImage = null; 
    String internalURL = null; 

    try { 
      xBitmapContainer = (XNameContainer) UnoRuntime.queryInterface(
          XNameContainer.class, xMSF.createInstance(
              "com.sun.star.drawing.BitmapTable")); 
      xImage = (XTextContent) UnoRuntime.queryInterface(
          XTextContent.class,  xMSF.createInstance(
              "com.sun.star.text.TextGraphicObject")); 
      XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(
          XPropertySet.class, xImage); 

      // helper-stuff to let OOo create an internal name of the graphic 
      // that can be used later (internal name consists of various checksums) 
      xBitmapContainer.insertByName("someID", grProps.unoURL); 
      internalURL = AnyConverter.toString(xBitmapContainer 
          .getByName("someID")); 

      xProps.setPropertyValue("AnchorType", 
          com.sun.star.text.TextContentAnchorType.AS_CHARACTER); 
      xProps.setPropertyValue("GraphicURL", internalURL); 
      xProps.setPropertyValue("Width", (int) grProps.widthOfGraphic); 
      xProps.setPropertyValue("Height", (int) grProps.heightOfGraphic); 

      // inser the graphic at the cursor position 
      xText.insertTextContent(xCursor, xImage, false); 

      // remove the helper-entry 
      xBitmapContainer.removeByName("someID"); 
    } catch (Exception e) { 
      System.out.println("Failed to insert Graphic"); 
    } 
} 
+0

感謝您的答覆。我發現了這個問題的臨時解決方案。問題已經很久了。我會嘗試你的解決方案並告知你結果。 – 2012-01-06 15:42:03

相關問題