Iterating over Text

From Apache OpenOffice Wiki
Jump to: navigation, search

The second interface of is XEnumerationAccess. A Text service enumerates all paragraphs in a text and returns objects which support This includes tables, because writer sees tables as specialized paragraphs that support the service.

Paragraphs also have an of their own. They can enumerate every single text portion that they contain. A text portion is a text range containing a uniform piece of information that appears within the text flow. An ordinary paragraph, formatted in a uniform manner and containing nothing but a string, enumerates just a single text portion. In a paragraph that has specially formatted words or other contents, the text portion enumeration returns one service for each differently formatted string, and for every other text content. Text portions include the service and have the properties listed below:

Properties of
TextPortionType string - Contains the type of the text portion (see below).
ControlCharacter short - Returns the control character if the text portion contains a control character as defined in
Bookmark Contains the bookmark if the portion has TextPortionType="Bookmark".
DocumentIndexMark Contains the document index mark if the portion has TextPortionType="DocumentIndexMark".
ReferenceMark Contains the reference mark if the portion has TextPortionType="ReferenceMark".
Footnote Contains the footnote if the portion has TextPortionType="Footnote".
TextField Contains the text field if the portion has TextPortionType="TextField".
InContentMetadata Contains the text range if the portion has TextPortionType="InContentMetadata".
IsCollapsed boolean - Contains whether the portion is a point only.
IsStart boolean - Contains whether the portion is a start portion if two portions are needed to include an object, for example, DocumentIndexMark.

Possible Values for TextPortionType are:

TextPortionType (String) Description
"Text" a portion with mere string content
"TextField" A content.
"TextContent" A text content supplied through the interface XContentEnumerationAccess.
"Footnote" A footnote or an endnote.
"ControlCharacter" A control character.
"ReferenceMark" A reference mark.
"DocumentIndexMark" A document index mark.
"Bookmark" A bookmark.
"Redline" A redline portion which is a result of the change tracking feature.
"Ruby" A ruby attribute which is used in Asian text.
"Frame" A frame supplied through the interface XContentEnumerationAccess.
"SoftPageBreak" A soft page break.
"InContentMetadata" A text range with attached metadata.

The text portion enumeration of a paragraph does not supply contents which do belong to the paragraph, but do not fuse together with the text flow. These could be text frames, graphic objects, embedded objects or drawing shapes anchored at the paragraph, characters or as character. The TextPortionType "TextContent" indicate if there is a content anchored at a character or as a character. If you have a TextContent portion type, you know that there are shape objects anchored at a character or as a character.

This last group of data contained in a text, Paragraphs and TextPortions in writer support the interface This interface tells which text contents other than the text flow contents exist, and supplies them as an

 sequence< string > getAvailableServiceNames()
 com::sun::star::container::XEnumeration createContentEnumeration( [in] string aServiceName)

The XContentEnumerationAccess of the paragraph lists the shape objects anchored at the paragraph while the XContentEnumerationAccess of a text portion lists the shape objects anchored at a character or as a character.

Documentation note.png Precisely the same enumerations are available for the current text cursor selection. The text cursor enumerates paragraphs, text portions and text contents just like the service itself.

The enumeration access to text through paragraphs and text portions is used if every single paragraph in a text needs to be touched. The application area for this enumeration are export filters, that uses this enumeration to go over the whole document, writing out the paragraphs to the target file. The following code snippet centers all paragraphs in a text.

  /** This method demonstrates how to iterate over paragraphs */
  protected void ParagraphExample () {
      try {
          // The service '' supports the XEnumerationAccess interface to
          // provide an enumeration
          // of the paragraphs contained by the text the service refers to. 
          // Here, we access this interface
          XEnumerationAccess xParaAccess = (XEnumerationAccess) UnoRuntime.queryInterface(
              XEnumerationAccess.class, mxDocText);
          // Call the XEnumerationAccess's only method to access the actual Enumeration
          XEnumeration xParaEnum = xParaAccess.createEnumeration();
          // While there are paragraphs, do things to them
          while (xParaEnum.hasMoreElements()) {
              // Get a reference to the next paragraphs XServiceInfo interface. TextTables
              // are also part of this enumeration access, so we ask the element if it is
              // a TextTable, if it doesn't support the
              // service, then it is safe to assume that it really is a paragraph
              XServiceInfo xInfo = (XServiceInfo) UnoRuntime.queryInterface(
                  XServiceInfo.class, xParaEnum.nextElement());
              if (!xInfo.supportsService("")) {
                  // Access the paragraph's property set...the properties in this
                  // property set are listed
                  // in:
                  XPropertySet xSet = (XPropertySet) UnoRuntime.queryInterface(
                      XPropertySet.class, xInfo);
                  // Set the justification to be center justified
      } catch (Exception e) {
          e.printStackTrace (System.out);
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages