如何使用java将图像插入到openoffice编写器文档中? [英] How to insert an image in to an openoffice writer document with java?

查看:105
本文介绍了如何使用java将图像插入到openoffice编写器文档中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从模板创建一个openoffice编写器文档。
我可以用此代码替换报告的文本部分

I'm trying to create an openoffice writer document from a template. I can replace text parts of report with this code

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。

I found some sample code from here to link or embed an image into an xTextDocument. But, I can't insert into an xTextDocument. Is there any other way to do this with Java? Openoffice version is 3.1.0.

任何答案?

推荐答案

我在这里找到了这个: https://svn.apache.org/repos/asf/openoffice/ooo-site/trunk/content/api/Examples/Snippets/Writer/Writer.EmbedAGraphicIntoATextdocument.snip

I found this here: 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将图像插入到openoffice编写器文档中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆