Accessing Existing Tables

From Apache OpenOffice Wiki
Jump to: navigation, search



To access the tables contained in a text document, the text document model supports the interface com.sun.star.text.XTextTablesSupplier with one single method getTextTables(). It returns a com.sun.star.text.TextTables service, which is a named and indexed collection, that is, tables are retrieved using com.sun.star.container.XNameAccess or com.sun.star.container.XIndexAccess.

The following snippet iterates over the text tables in a given text document object mxDoc and colors them green.

  import com.sun.star.text.XTextTablesSupplier;
  import com.sun.star.container.XNameAccess;
  import com.sun.star.container.XIndexAccess;
  import com.sun.star.beans.XPropertySet;
 
  ...
 
  // first query the XTextTablesSupplier interface from our document
  XTextTablesSupplier xTablesSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface(
      XTextTablesSupplier.class, mxDoc );
  // get the tables collection
  XNameAccess xNamedTables = xTablesSupplier.getTextTables();
 
  // now query the XIndexAccess from the tables collection
  XIndexAccess xIndexedTables = (XIndexAccess) UnoRuntime.queryInterface(
      XIndexAccess.class, xNamedTables);
 
  // we need properties
  XPropertySet xTableProps = null;
 
  // get the tables
  for (int i = 0; i < xIndexedTables.getCount(); i++) {
      Object table = xIndexedTables.getByIndex(i);
      // the properties, please!
      xTableProps = (XPropertySet) UnoRuntime.queryInterface(
          XPropertySet.class, table);
 
      // color the table light green in format 0xRRGGBB
      xTableProps.setPropertyValue("BackColor", new Integer(0xC8FFB9));
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages