java - 如何使用 java 将图像插入到 openoffice writer 文档中?

标签 java image openoffice.org openoffice-writer

我正在尝试从模板创建 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。

有答案吗?

最佳答案

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

关于java - 如何使用 java 将图像插入到 openoffice writer 文档中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6236515/

相关文章:

c# - 无法更改 datagridview winforms 中的图像

html - 网站图片作为标题,谷歌无法识别图片

reference - 引用 OpenOffice/LibreOffice Calc 中当前行下方的行

java - 连接 Open Office 时出现问题 (com.sun.star.lang.DisposeException)

java - 如何克服java堆空间的OutOfMemoryError?

java - Java中处理异常网络事件(UDP)

java - 有人可以解释一下这个链表空指针异常吗?

java - Graphql 在 Postman 中使用变量作为文本发送 POST

android - 使用通用图像加载器保存本地恢复的图像

java - 什么时候使用GSON解析对象?