Hi Tobias,

from a quick look on what you wrote I would guess the problem lies in XComponent openDocument instead of
XTextDocument openDocument, maybe someone wants to correct me.

I found in the Developers Guide:

7.3.6  Bookmarks

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 com.sun.star.text.XSimpleText:createTextCursorByRange(), com.sun.star.text.XSimpleText:insertString() or com.sun.star.text.XText:insertTextContent().
Have a nice evening
Christoph



Tobias Krais schrieb:
Hi together,

at the moment I try to write a little program that jumps to a bookmark
and inserts some text on this position. For this I consulted Developers
Guide chapter 7.1.2. You can find my whole code on the end of the mail.

Following code always retruns "null". The variable openDocument is an
instance of the class XComponent. Why does it always return null,
although the document is opened correctly and contains two bookmarks
named "Textmarke01" and "Textmarke02"?
-----%<------
// Get bookmark interface from document
XBookmarksSupplier xBookmarksSupplier = (XBookmarksSupplier)
        UnoRuntime.queryInterface(XBookmarksSupplier.class,
        openDocument);
-----%<-----

Please tell me what I am doing wrong. I use OOo 2.0.4 and Debian Linux.

Greetings, Tobias

-----%<-----
/**
 * Sets the Cursor to a different position.
 *
 * @param bookmarkName Name of the Bookmark the cursor should jump to.
 * @param text The text that is going to be insertet on bookmark
 * position
 */
public void enterStringOnBookmark(String bookmarkName, String text) {
        // Get bookmark interface from document
        XBookmarksSupplier xBookmarksSupplier = (XBookmarksSupplier)
                UnoRuntime.queryInterface(XBookmarksSupplier.class,
                openDocument);
                
        // Accessing the Bookmarks collection of the document
        XNameAccess xNamedBookmarks = xBookmarksSupplier.getBookmarks();
                
        // Find the Bookmark specified in bookmarkName
        Object bookmark = null;
        try {
                bookmark = xNamedBookmarks.getByName(bookmarkName);
        } catch (Exception e) {
                if(this.debug == true) System.out.println(e);
        }
                
        // Query for XTextContext, the content of the Bookmark
        XTextContent xBookmarkContent = (XTextContent)
                UnoRuntime.queryInterface(XTextContent.class, bookmark);
                
        // Get the anchor of the bookmark (its XTextRange)
        XTextRange xBookmarkRange = xBookmarkContent.getAnchor();
                
        // Set string at bookmark position
        xBookmarkRange.setString(text);
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to