Access to Text Content

From Apache OpenOffice Wiki
Jump to: navigation, search



The service com.sun.star.text.Text supports the modification of simple or formatted text contents. Changing text contents and text formatting is provided by the interface com.sun.star.text.XText as discussed in First Steps. Refer to chapter Editing Text for further information. It implements the interfaces com.sun.star.container.XEnumerationAccess that provides access to the paragraphs of the text and the interface com.sun.star.text.XText to insert and modify text contents. For detailed information about text handling, see Editing Text.

  // --- Insert two text paragraphs into the cell. ---
  com.sun.star.text.XText xText = (com.sun.star.text.XText)
      UnoRuntime.queryInterface(com.sun.star.text.XText.class, xCell);
  com.sun.star.text.XTextCursor xTextCursor = xText.createTextCursor();
 
  xText.insertString(xTextCursor, "Text in first line.", false);
  xText.insertControlCharacter(xTextCursor,
      com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
  xText.insertString(xTextCursor, "Some more text.", false);
 
  // --- Query the separate paragraphs. ---
  String aText;
  com.sun.star.container.XEnumerationAccess xParaEA =
      (com.sun.star.container.XEnumerationAccess) UnoRuntime.queryInterface(
          com.sun.star.container.XEnumerationAccess.class, xCell);
  com.sun.star.container.XEnumeration xParaEnum = xParaEA.createEnumeration();
 
  // Go through the paragraphs
  while (xParaEnum.hasMoreElements()) {
      Object aPortionObj = xParaEnum.nextElement();
      com.sun.star.container.XEnumerationAccess xPortionEA =
          (com.sun.star.container.XEnumerationAccess) UnoRuntime.queryInterface(
              com.sun.star.container.XEnumerationAccess.class, aPortionObj);
      com.sun.star.container.XEnumeration xPortionEnum = xPortionEA.createEnumeration();
      aText = "";
 
      // Go through all text portions of a paragraph and construct string.
      while (xPortionEnum.hasMoreElements()) {
          com.sun.star.text.XTextRange xRange =
              (com.sun.star.text.XTextRange) xPortionEnum.nextElement();
          aText += xRange.getString();
      }
      System.out.println("Paragraph text: " + aText);
  }

The SheetCell interface com.sun.star.text.XTextFieldsSupplier contains methods that provide access to the collection of text fields in the cell. For details on inserting text fields, refer to Text Fields.

Documentation note.png Currently, the only possible text field in Calc cells is the hyperlink field com.sun.star.text.textfield.URL.
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages