From Apache OpenOffice Wiki
Jump to: navigation, search

A Bookmark is a text content that marks a position inside of a paragraph or a text selection that supports the service. To search for a bookmark, the text document model implements the interface that supplies a collection of the bookmarks. The collection supports the service which consists of and

The bookmark name can be read and changed through its ( interface.

To insert, remove or change text, or attributes starting from the position of a bookmark, retrieve its by calling getAnchor() at its interface. Then use getString() or setString() at the XTextRange, or pass this XTextRange to methods expecting a text range, such as createTextCursorByRange(), insertString() or insertTextContent().

Tip.png Make sure that the access to the bookmark anchor position always uses the correct text object. Since every XTextRange knows its surrounding text, use the getText() method of the bookmark's anchor. It is not allowed to call aText.createTextCursorByRange(oAnchor) when aText represents a different area of the document than the bookmark (different text frames, body text and text frame...)

Use the createInstance method of the interface provided by the text document model to insert an new bookmark into the document. The service name is "". Then use the bookmark's interface and call setName(). If no name is set, Apache OpenOffice makes up generic names, such as Bookmark1 and Bookmark2. Similarly, if a name is used that is not unique, writer automatically appends a number to the bookmark name. The bookmark object obtained from createInstance() can only be inserted once.

  // inserting and retrieving a bookmark
  Object bookmark = mxDocFactory.createInstance ( "" );
  // name the bookmark
  XNamed xNamed = (XNamed) UnoRuntime.queryInterface ( 
      XNamed.class, bookmark );
  // get XTextContent interface 
  XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface ( 
      XTextContent.class, bookmark );
  // insert bookmark at the end of the document
  // instead of mxDocText.getEnd you could use a text cursor's XTextRange interface or any XTextRange
  mxDocText.insertTextContent ( mxDocText.getEnd(), xTextContent, false ); 
  // query XBookmarksSupplier from document model and get bookmarks collection
  XBookmarksSupplier xBookmarksSupplier = (XBookmarksSupplier)UnoRuntime.queryInterface(
      XBookmarksSupplier.class, xWriterComponent);
  XNameAccess xNamedBookmarks = xBookmarksSupplier.getBookmarks();
  // retrieve bookmark by name
  Object foundBookmark = xNamedBookmarks.getByName("MyUniqueBookmarkName");
  XTextContent xFoundBookmark = (XTextContent)UnoRuntime.queryInterface(
      XTextContent.class, foundBookmark);
  // work with bookmark
  XTextRange xFound = xFoundBookmark.getAnchor();
  xFound.setString(" The throat mike, glued to her neck, " 
      + "looked as much as possible like an analgesic dermadisk.");
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages