Bookmarks

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 com.sun.star.text.TextContent service. To search for a bookmark, the text document model implements the interface com.sun.star.text.XBookmarksSupplier that supplies a collection of the bookmarks. The collection supports the service com.sun.star.text.Bookmarks which consists of com.sun.star.container.XNameAccess and com.sun.star.container.XIndexAccess.

The bookmark name can be read and changed through its (com.sun.star.container.XNamed) interface.

To insert, remove or change text, or attributes starting from the position of a bookmark, retrieve its com.sun.star.text.XTextRange by calling getAnchor() at its com.sun.star.text.XTextContent 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 com.sun.star.lang.XMultiServiceFactory interface provided by the text document model to insert an new bookmark into the document. The service name is "com.sun.star.text.Bookmark". Then use the bookmark's com.sun.star.container.XNamed 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 ( "com.sun.star.text.Bookmark" );
  
  // name the bookmark
  XNamed xNamed = (XNamed) UnoRuntime.queryInterface ( 
      XNamed.class, bookmark );
  xNamed.setName("MyUniqueBookmarkName");
  
  // 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