From Apache OpenOffice Wiki
Jump to: navigation, search


The text model cursor allows for free navigation over the model by character, words, sentences, or paragraphs. There can be several model cursors at the same time. Model cursor creation, movement and usage is discussed in the section Word Processing . The text model cursors are services that are based on the interface, which is based on

The text view cursor enables the user to travel over the document in the view by character, line, screen page and document page. There is only one text view cursor. Certain information about the current layout, such as the number of lines and page number must be retrieved at the view cursor. The chapter Text Document Controller below discusses the view cursor in detail. The text view cursor is a service that includes

Locating Text Contents

The text document model has suppliers that yield all text contents in a document as collections. To find a particular text content, such as bookmarks or text fields, use the appropriate supplier interface. The following supplier interfaces are available at the model:

Supplier interfaces Methods
XTextTablesSupplier getTextTables()
XTextFramesSupplier getTextFrames()
XTextGraphicObjectsSupplier getGraphicObjects()
XTextEmbeddedObjectsSupplier getEmbeddedObjects()
XTextFieldsSupplier getTextFields() getTextFieldMasters()

XBookmarksSupplier getBookmarks()
XReferenceMarksSupplier getReferenceMarks()
XFootnotesSupplier getFootnotes() getFootnoteSettings()
XEndnotesSupplier getEndnotes() getEndnoteSettings()

XTextSectionsSupplier getTextSections()
XDocumentIndexesSupplier getDocumentIndexes()
XRedlinesSupplier getRedlines()

You can work with text content directly, set properties and use its interfaces, or find out where it is and do an action at the text content location in the text. To find out where a text content is located call the getAnchor() method at the interface, which every text content must support.

In addition, text contents located at the current text cursor position or the content where the cursor is currently located are provided in the PropertySet of the cursor. The corresponding cursor properties are:

  • DocumentIndexMark
  • TextField
  • ReferenceMark
  • Footnote
  • Endnote
  • DocumentIndex
  • TextTable
  • TextFrame
  • Cell
  • TextSection

Search and Replace

The writer model supports the interface that inherits from the interface for searching and replacing in text. It contains the following methods:

 com::sun::star::util::XSearchDescriptor createSearchDescriptor() 
 com::sun::star::util::XReplaceDescriptor createReplaceDescriptor() 
 com::sun::star::uno::XInterface findFirst( [in] com::sun::star::util::XSearchDescriptor xDesc) 
 com::sun::star::uno::XInterface findNext( [in] com::sun::star::uno::XInterface xStartAt,
                                           [in] com::sun::star::util::XSearchDescriptor xDesc) 
 com::sun::star::container::XIndexAccess findAll( [in] com::sun::star::util::XSearchDescriptor xDesc) 
 long replaceAll( [in] com::sun::star::util::XSearchDescriptor xDesc)

To search or replace text, first create a descriptor service using createSearchDescriptor() or createReplaceDescriptor(). You receive a service that supports the interface with methods to describe what you are searching for, what you want to replace with and what attributes you are looking for. It is described in detail below.

Pass in this descriptor to the methods findFirst(), findNext(), findAll() or replaceAll().

The methods findFirst() and findNext() return a pointing to an object that contains the found item. If the search is not successful, a null reference to an XInterface is returned, that is, if you try to query other interfaces from it, null is returned. The method findAll() returns a containing one or more pointing to the found text ranges or if they failed an empty interface. The method replaceAll() returns the number of replaced occurrences only.


The interface is required to describe your search. It is a powerful interface and inherits from XReplaceDescriptor, XSearchDescriptor and XPropertySet.

The target of your search is described by a string containing a search text or a style name using setSearchString(). Correspondingly, provide the text string or style name that should replace the found occurrence of the search target to the XReplaceDescriptor using setReplaceString(). Refine the search mode through the properties included in the service

Properties of
SearchBackwards boolean - Search backward
SearchCaseSensitive boolean - Search is case sensitive.
SearchRegularExpression boolean - Search interpreting the search string as a regular expression.
SearchSimilarity boolean - Use similarity search using the four following options:
SearchSimilarityAdd short - Determines the number of characters the word in the document may be longer than the search string for it to remain valid.
SearchSimilarityExchange short - Determines how many characters in the search term can be exchanged.
SearchSimilarityRelax boolean - If true, the values of added, exchanged, and removed characters are combined The search term is then found if the word in the document can be generated through any combination of these three conditions.
SearchSimilarityRemove short - Determines how many characters the word in the document may be shorter than the search string for it to remain valid. The characters may be removed from the word at any position.
SearchStyles boolean - Determines if the search and replace string should be interpreted as paragraph style names. Note that the Display Name of the style has to be used.
SearchWords boolean - Determines if the search should find complete words only.
SearchType (not included in API-reference ) integer - Determines if the search should be done in cell formulas (0) values (1) or notes (2). [Calc only]

In XPropertyReplace, the methods to get and set search attributes, and replace attributes allow the attributes to search for to be defined and the attributes to insert instead of the existing attributes. All of these methods expect a sequence of structs.

Any properties contained in the services, and can be used for an attribute search. If setValueSearch(false) is used, checks if an attribute exists, whereas setValueSearch(true) finds specific attribute values. If only searching to see if an attribute exists at all, it is sufficient to pass a PropertyValue struct with the Name field set to the name of the required attribute.

The following code snippet replaces all occurrences of the text "random numbers" by the bold text "replaced numbers" in a given document mxDoc.

 XReplaceable xReplaceable = (XReplaceable) UnoRuntime.queryInterface(XReplaceable.class, mxDoc);
 XReplaceDescriptor xRepDesc = xReplaceable.createReplaceDescriptor();
 // set a string to search for
 xRepDesc.setSearchString("random numbers");
 // set the string to be inserted
 xRepDesc.setReplaceString("replaced numbers");
 // create an array of one property value for a CharWeight property
 PropertyValue[] aReplaceArgs = new PropertyValue[1];
 // create PropertyValue struct
 aReplaceArgs[0] = new PropertyValue();
 // CharWeight should be bold
 aReplaceArgs[0].Name = "CharWeight";
 aReplaceArgs[0].Value = new Float(;
 // set our sequence with one property value as ReplaceAttribute
 XPropertyReplace xPropRepl = (XPropertyReplace) UnoRuntime.queryInterface(
         XPropertyReplace.class, xRepDesc);
 // replace
 long nResult = xReplaceable.replaceAll(xRepDesc);

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages