[Solved] Inserting Hyperlinks in Writer

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
SNM90
Posts: 21
Joined: Fri Mar 01, 2013 6:56 am

[Solved] Inserting Hyperlinks in Writer

Post by SNM90 »

Hello,

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); 
Instead of the HyperLinkTarget property used (in character properties) I've tried all - HyperLinkURL and HyperLinkName. None seem to work! :(

Can anyone help me in this?

Thanks in advance!
Last edited by SNM90 on Tue May 07, 2013 11:48 am, edited 1 time in total.
OpenOffice 3.1 on Windows Vista
(Libreoffice 4.0.0 on Windows 7)
User avatar
karolus
Volunteer
Posts: 1226
Joined: Sat Jul 02, 2011 9:47 am

Re: Inserting Hyperlinks in Writer

Post by karolus »

AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 24.8… flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
SNM90
Posts: 21
Joined: Fri Mar 01, 2013 6:56 am

Re: Inserting Hyperlinks in Writer

Post by SNM90 »

I tried that method from what I could translate to C++, but I couldn't figure it out still (already read that post). Can you provide a translation if possible? Because I have to work without text frames and the other method which it shows involves changing the character property of HyperLinkURL or HyperLinkTarget which I have tried unsuccessfully.
OpenOffice 3.1 on Windows Vista
(Libreoffice 4.0.0 on Windows 7)
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Inserting Hyperlinks in Writer

Post by hanya »

You have to select the text with the cursor to set the URL to the specific part of the text:

Code: Select all

Sub HyperlinkOnWriter
  text = ThisComponent.getText()
  cursor = text.createTextCursor()
  text.insertString(cursor, "Hyperlink", False)
  
  cursor.goRight(Len("Hyperlink"), True)
  cursor.HyperLinkURL = "http://foobar"
End Sub
is not for Writer but for Calc.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
SNM90
Posts: 21
Joined: Fri Mar 01, 2013 6:56 am

Re: Inserting Hyperlinks in Writer

Post by SNM90 »

Okay yes, I tried this too. I cannot find a way to do this in C++. In that what I've done is queried the XPropertySet object from XTextCursor. How should I query it from a selected text range?

EDIT: Ok I got it... I had to insert the string first, then highlight it's text range. Then, query XPropertySet from the text cursor. Following is the working piece of code:

Code: Select all

Reference <XText> xText = xTextDoc->getText();
	Reference <XTextCursor> xTextCursor = xText->createTextCursor();
	Reference <XTextRange> xTextRange (xTextCursor, UNO_QUERY);

	xText->insertString(xTextRange, OUString::createFromAscii("Hello123"), false);

	xTextCursor->gotoStart(true);

	Reference <XPropertySet> xHyperLinkProp (xTextCursor, UNO_QUERY);

	Any hyperlink;
	hyperlink <<= OUString::createFromAscii("http://www.google.com/");
	xHyperLinkProp->setPropertyValue("HyperLinkURL", hyperlink);
OpenOffice 3.1 on Windows Vista
(Libreoffice 4.0.0 on Windows 7)
Post Reply