Kien Dang Ngoc
Kien Dang Ngoc

Reputation: 1079

insert image into table cells in writer using openoffice API

I'm trying to insert images into cell of a table inside document using openoffice api. However, I got blank images like the image below

enter image description here



This is the code:

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();
    }
}


What I tried to do is to create an image object (com.sun.star.text.TextGraphicObject) and add the image path into the image object. I did it the same way on another function that insert image into a text field and it worked. I don't know why this function does not work.

Upvotes: 2

Views: 1203

Answers (0)

Related Questions