Writer documents primarily deal with text content arranged in paragraphs. This chapter introduces appropriate methods to manipulate, traverse, search, format, and modify the content contained in an OpenOffice.org Writer document.
Conceptually, all document types have two components : the data they contain and the controller that determines how the data is displayed. Writer documents primarily contain simple formatted text. In addition to simple text, Writer documents may contain other content such as tables, frames , graphics, text fields, bookmarks, footnotes, endnotes, text sections, index entries, tracked document changes (called redlining), objects for styling, and objects for numbering. OOo uses the same methods and interfaces to interact with most of these capabilities. Consequently, learning to manipulate a few types of content will give you a solid basis for dealing with all of them.
Note | In OOo, the data contained in a document is called the "model." The base model interface is com. sun. star. frame.XModel. |
In OpenOffice.org, the data contained in a document is called the model. Each model has a controller that is responsible for the visual presentation of the data. The controller knows the location of the visible text cursor, the current page, and what is currently selected.
Tip | While trying to determine which portion of the OOo API to use to solve a specific problem, first ask if the problem is display-centric or data-centric. For example, a paragraph boundary is specified in the data, but a new line is usually determined by the controller when the data is formatted. |
Every text document supports the com.sun.star.text.TextDocument service. When I write a macro that must be user friendly and requires a text document, I verify that the document is the correct type by using the object method supportsService (see Listing 1 ).
REM If it really matters, you should check the document type REM to avoid a run-time error. If NOT ThisComponent.supportsService("com.sun.star.text.TextDocument") Then MsgBox "The current document is not a Writer text document", 48, "Error" Exit Sub End If
An interface defines a series of methods. If an object implements an interface, it also implements every method defined by that interface. A service defines an object by specifying the interfaces that it implements, the properties that it contains, and the other services that it exports. A service indirectly specifies the implemented methods by specifying interfaces. The interfaces supported by the TextDocument service provide a good overview of the provided functionality (see Table 1 ).
Service | Description |
---|---|
com.sun.star.text.XTextDocument | Main text document interface. |
com.sun.star.text.XBookmarksSupplier | Access the bookmarks. |
com.sun.star.text.XChapterNumberingSupplier | Numbering rules for chapters. |
com.sun.star.text.XDocumentlndexesSupplier | Access the collection of indexes. |
com.sun.star.text.XTextEmbeddedObjectsSupplier | Access embedded objects. |
com.sun.star.text.XEndnotesSupplier | Access endnote content. |
com.sun.star.text.XFootnotesSupplier | Access footnote content. |
com.sun.star.text.XLineNumberingSupplier | Numbering rules for line numbers . |
com.sun.star.text.XPagePrintable | Print multiple pages on one page. |
com.sun.star.text.XReferenceMarksSupplier | Access the document reference marks, which are used to refer to text positions in a text document. |
com.sun.star.text.XTextFieldsSupplier | Access contained fields. |
com.sun.star.text.XTextFramesSupplier | Access contained text frames. |
com.sun.star.text.XTextGraphicObjectsSupplier | Access embedded and linked graphics. |
com.sun.star.text.XTextSectionsSupplier | Access contained text sections. |
com.sun.star.text.XTextTablesSupplier | Access contained tables. |
com.sun.star.style.XStyleFamiliesSupplier | Access contained styles by type. |
com.sun.star.util.XNumberFormatsSupplier | Access contained number formats. |
com.sun.star.util.XRefreshable | Refresh data that can be refreshed from a database. |
com.sun.star.util.XReplaceable | Replace text found with a search descriptor. |
com.sun.star.util.XSearchable | Search a text range for a specific string pattern. |
com.sun.star.beans.XPropertySet | Access document properties by name . |