Difference between revisions of "Documentation/DevGuide/Text/Bookmarks"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (FINAL VERSION FOR L10N)
 
(One intermediate revision by one other user not shown)
Line 12: Line 12:
 
The bookmark name can be read and changed through its (<idl>com.sun.star.container.XNamed</idl>) interface.  
 
The bookmark name can be read and changed through its (<idl>com.sun.star.container.XNamed</idl>) interface.  
  
To insert, remove or change text, or attributes starting from the position of a bookmark, retrieve its <idl>com.sun.star.text.XTextRange</idl> by calling <code>getAnchor()</code> at its <idl>com.sun.star.text.XTextContent</idl> interface. Then use <code>getString()</code> or <code>setString()</code> at the <code>XTextRange</code>, or pass this <code>XTextRange</code> to methods expecting a text range, such as [http://api.openoffice.org/docs/common/ref/com/sun/star/text/XSimpleText.html#createTextCursorByRange com.sun.star.text.XSimpleText:createTextCursorByRange](), [http://api.openoffice.org/docs/common/ref/com/sun/star/text/XSimpleText.html#insertString com.sun.star.text.XSimpleText:insertString]() or [http://api.openoffice.org/docs/common/ref/com/sun/star/text/XText.html#insertTextContent com.sun.star.text.XText:insertTextContent]().
+
To insert, remove or change text, or attributes starting from the position of a bookmark, retrieve its <idl>com.sun.star.text.XTextRange</idl> by calling <code>getAnchor()</code> at its <idl>com.sun.star.text.XTextContent</idl> interface. Then use <code>getString()</code> or <code>setString()</code> at the <code>XTextRange</code>, or pass this <code>XTextRange</code> to methods expecting a text range, such as <idlm>com.sun.star.text.XSimpleText:createTextCursorByRange</idlm>(), <idlm>om.sun.star.text.XSimpleText:insertString</idlm>() or <idlm>com.sun.star.text.XText:insertTextContent</idlm>().
  
{{Documentation/Tip|Make sure that the access to the bookmark anchor position always uses the correct text object. Since every <tt>XTextRange</tt> knows its surrounding text, use the <tt>getText()</tt> method of the bookmark's anchor. It is not allowed to call <tt>aText.createTextCursorByRange(oAnchor)</tt> when <tt>aText</tt> represents a different area of the document than the bookmark (different text frames, body text and text frame...)}}
+
{{Tip|Make sure that the access to the bookmark anchor position always uses the correct text object. Since every <tt>XTextRange</tt> knows its surrounding text, use the <tt>getText()</tt> method of the bookmark's anchor. It is not allowed to call <tt>aText.createTextCursorByRange(oAnchor)</tt> when <tt>aText</tt> represents a different area of the document than the bookmark (different text frames, body text and text frame...)}}
 
 
Use the <code>createInstance</code> method of the <idl>com.sun.star.lang.XMultiServiceFactory</idl> interface provided by the text document model to insert an new bookmark into the document. The service name is "<code>com.sun.star.text.Bookmark</code>". Then use the bookmark's <idl>com.sun.star.container.XNamed</idl> interface and call <code>setName()</code>. If no name is set, {{PRODUCTNAME}} 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 <code>createInstance()</code> can only be inserted once.
 
  
 +
Use the <code>createInstance</code> method of the <idl>com.sun.star.lang.XMultiServiceFactory</idl> interface provided by the text document model to insert an new bookmark into the document. The service name is "<code>com.sun.star.text.Bookmark</code>". Then use the bookmark's <idl>com.sun.star.container.XNamed</idl> interface and call <code>setName()</code>. If no name is set, {{AOo}} 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 <code>createInstance()</code> can only be inserted once.
 +
<syntaxhighlight lang="java">
 
   // inserting and retrieving a bookmark
 
   // inserting and retrieving a bookmark
 
   Object bookmark = mxDocFactory.createInstance ( "com.sun.star.text.Bookmark" );
 
   Object bookmark = mxDocFactory.createInstance ( "com.sun.star.text.Bookmark" );
Line 48: Line 48:
 
   xFound.setString(" The throat mike, glued to her neck, "  
 
   xFound.setString(" The throat mike, glued to her neck, "  
 
       + "looked as much as possible like an analgesic dermadisk.");
 
       + "looked as much as possible like an analgesic dermadisk.");
 
+
</syntaxhighlight>
  
 
{{PDL1}}
 
{{PDL1}}
  
 
[[Category:Documentation/Developer's Guide/Text Documents]]
 
[[Category:Documentation/Developer's Guide/Text Documents]]

Latest revision as of 14:16, 3 January 2021



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