I'm trying to write a program in which I have to insert a hyperlink on a text like say:
Text - Hello123
Link - http://www.google.com/
By clicking on Hello123 in the writer (Ctrl+click), I should be able to navigate to the URL mentioned. I have tried it using in the Writer separately, and I know this is possible since the option Insert->Hyperlink allows it. But I'm struggling to accomplish this programmatically. Following is my piece of code in C++:
Code: Select all
Reference <XComponent> xComponent = xCompLoader->loadComponentFromURL(sURL, OUString::createFromAscii("_blank"), 0, Sequence<PropertyValue>());
Reference <XTextDocument> xTextDoc (xComponent, UNO_QUERY);
Reference <XText> xText = xTextDoc->getText();
Reference <XTextCursor> xTextCursor = xText->createTextCursor();
Reference <XTextRange> xTextRange (xTextCursor, UNO_QUERY);
Reference <XPropertySet> xHyperLinkProp (xTextCursor, UNO_QUERY);
Any hyperlink;
hyperlink <<= OUString::createFromAscii("http://www.google.com/");
xHyperLinkProp->setPropertyValue("HyperLinkTarget", hyperlink);
xText->insertString(xTextRange, OUString::createFromAscii("Hyperlink"), false);

Can anyone help me in this?
Thanks in advance!