使用openoffice API将图像插入到writer的表格单元格中

时间:2015-01-10 10:24:00

标签: java openoffice-writer openoffice-api

我正在尝试使用openoffice api将图像插入到文档内的表格的单元格中。但是,我得到了如下图所示的空白图像

enter image description here



这是代码:

private void insertFigureIntoTable(Hashtable<String, Object> tblTable) {
    if (document == null) {
        System.out.println("Invalid document. Exiting.");
        System.exit(1);
    }
    XModel model = (XModel) UnoRuntime.queryInterface(XModel.class, document);
    // first query the XTextTablesSupplier interface from our document

    XTextTablesSupplier xTablesSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, document);

    // get the tables collection
    XNameAccess xNamedTables = xTablesSupplier.getTextTables();

    // now query the XIndexAccess from the tables collection
    XIndexAccess xIndexedTables = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xNamedTables);

    String[] tableNames = xNamedTables.getElementNames();

    try {
        for (int i = 0; i < xIndexedTables.getCount(); i++) {
            XTextTable currTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xIndexedTables.getByIndex(i));
            String currTableName = tableNames[i];

            // We have data to populate the table
            if (tblTable.containsKey(currTableName)) {
                List<String> tableValues = (List<String>) tblTable.get(currTableName);
                int rowCount = currTable.getRows().getCount();
                int columnCount = currTable.getColumns().getCount();

                for (int j = 0; j < rowCount - 1; j++) {
                    // Start from B2
                    String cellName = "B" + Integer.toString(j + 2);
                    XTextTableCursor currTableCursor = currTable.createCursorByCellName(cellName);

                    for (int k = 0; k < columnCount - 1; k++) {
                        String rangeName = currTableCursor.getRangeName();
                        XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, currTable.getCellByName(rangeName));

                        XMultiServiceFactory docServices = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, model);

                        XTextContent imageObject = (XTextContent) UnoRuntime.queryInterface(XTextContent.class,
                                docServices.createInstance("com.sun.star.text.TextGraphicObject"));
                        XPropertySet pSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, imageObject);

                        // Set anchor type
                        pSet.setPropertyValue("AnchorType", com.sun.star.text.TextContentAnchorType.AS_CHARACTER);
                        pSet.setPropertyValue("GraphicURL", tableValues.get(j * (columnCount - 1) + k));
                        pSet.setPropertyValue("Width", Globals.defaultWidth);
                        pSet.setPropertyValue("Height", Globals.defaultHeight);

                        xCellText.insertTextContent(xCellText.createTextCursor(), imageObject, false);
                        currTableCursor.goRight((short) 1, false);
                    }
                }
            }
        }
    }

    catch (Exception e) {
        e.printStackTrace();
    }
}


我尝试做的是创建一个图像对象(com.sun.star.text.TextGraphicObject)并将图像路径添加到图像对象中。我在另一个将图像插入文本字段的函数中以相同的方式完成它并且它起作用。我不知道为什么这个功能不起作用。

0 个答案:

没有答案