簡體   English   中英

如何使用 java 將圖像插入到 openoffice writer 文檔中?

[英]How to insert an image in to an openoffice writer document with 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);
}

我從這里找到了一些示例代碼,用於將圖像鏈接或嵌入到 xTextDocument 中。 但是,我無法插入 xTextDocument。 有沒有其他方法可以用 Java 做到這一點? Openoffice版本是3.1.0。

有什么答案嗎?

我在這里找到了這個: 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");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM