[Solved] Call Java to insert hyperlinks into text

Creating a macro - Writing a Script - Using the API

[Solved] Call Java to insert hyperlinks into text

Postby luodashuai » Tue May 12, 2020 4:19 am

How does openoffice call Java to insert hyperlinks into text
Last edited by Hagar Delest on Thu May 21, 2020 8:31 pm, edited 2 times in total.
Reason: tagged solved
OpenOffice 4.1.7
luodashuai
 
Posts: 5
Joined: Tue May 12, 2020 4:17 am

Java insert hyperLink

Postby luodashuai » Tue May 12, 2020 8:13 am

How do I invoke Java to insert a hyperlink into an openoffice wirter document
Last edited by MrProgrammer on Tue May 12, 2020 5:13 pm, edited 1 time in total.
Reason: Merged duplicate topic into original one
OpenOffice 4.1.7
luodashuai
 
Posts: 5
Joined: Tue May 12, 2020 4:17 am

Re: java insert hyperLink

Postby Villeroy » Tue May 12, 2020 10:05 am

In OpenOffice 2.4? I don't remember.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28431
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: java insert hyperLink

Postby RoryOF » Tue May 12, 2020 10:17 am

Villeroy wrote:In OpenOffice 2.4? I don't remember.


Your answer, Villeroy, reminds me of the famous story of Lord Palmerston (English Prime Minister 1855-65) who was asked in his later years about the Schleswig Holstein crisis. He replied "Only three people have ever really understood the Schleswig-Holstein business—the Prince Consort, who is dead—a German professor, who has gone mad—and I, who have forgotten all about it."
Apache OpenOffice 4.1.7 on Xubuntu 18.04.4 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 31230
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: java insert hyperLink

Postby Villeroy » Tue May 12, 2020 10:21 am

I may have forgotten everything but keep some very old Basic libraries. One of them used XTextCursor, set properties HyperlinkURL, HyperlinkName and HyperlinkTarget and then writes the string to the endof that cursor.
Code: Select all   Expand viewCollapse view
                  cursor.HyperlinkURL = rg.TextField.URL
                  cursor.HyperlinkName = rg.TextField.Representation
                  cursor.HyperlinkTarget = rg.TextField.TargetFrame
                  celltext.setString(rg.getString())

celltext is the end of the cursor.
rg is a text snipped derived from the enumeration of a paragraph.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28431
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: java insert hyperLink

Postby RoryOF » Tue May 12, 2020 10:23 am

Ah well, Villeroy: at least you are not the German Professor who was driven mad!
Apache OpenOffice 4.1.7 on Xubuntu 18.04.4 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 31230
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: java insert hyperLink

Postby Zizi64 » Tue May 12, 2020 10:25 am


java insert hyperLink

Postby luodashuai » 2020 May 12, 8:13 am
How do I invoke Java to insert a hyperlink into an openoffice wirter document
________________________
OpenOffice 2.4


Please update your signature in this forum - if you are using other version then that archaic-old version of the OpenOffice.org.
Tibor Kovacs, Hungary; LO6.1.6, 6.2.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-6.4.5;AOO4.1.7
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 9427
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: java insert hyperLink

Postby Villeroy » Tue May 12, 2020 10:51 am

When I try to do it in MRI, the following Java code is generated. However, I do something wrong. The result is plain text.
Code: Select all   Expand viewCollapse view
import com.sun.star.beans.PropertyVetoException;
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.text.XSimpleText;
import com.sun.star.text.XText;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
   try
   {
      XTextDocument xTextDocument = UnoRuntime.queryInterface(
         XTextDocument.class, oInitialTarget);
      XText xText = xTextDocument.getText();
      
      XSimpleText xSimpleText = UnoRuntime.queryInterface(
         XSimpleText.class, xText);
      XTextCursor xTextCursor = xSimpleText.createTextCursor();
      
      XPropertySet xPropSet = UnoRuntime.queryInterface(
         XPropertySet.class, xTextCursor);
      String sHyperLinkName = AnyConverter.toString(xPropSet.getPropertyValue("HyperLinkName"));
      
      xPropSet.setPropertyValue("HyperLinkName", "My_Hyperlink");
      
      xPropSet.setPropertyValue("HyperLinkTarget", "https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=101940");
      
      xPropSet.setPropertyValue("HyperLinkTarget", "_blank");
      
      xPropSet.setPropertyValue("HyperLinkURL", "https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=101940");
      
      XTextRange xTextRange = UnoRuntime.queryInterface(
         XTextRange.class, xTextCursor);
      xTextRange.setString("Topic: java insert hyperLink");
      
      xTextCursor.gotoEnd(false);
      
      xTextRange.setString("java insert hyperLink");
      
      XTextRange xTextRange2 = UnoRuntime.queryInterface(
         XTextRange.class, xText);
      xTextRange2.setString("java insert hyperLink");
      
   }
   catch (WrappedTargetException e1)
   {
      // getPropertyValue, setPropertyValue
      e1.printStackTrace();
   }
   catch (PropertyVetoException e2)
   {
      // setPropertyValue
      e2.printStackTrace();
   }
   catch (IllegalArgumentException e3)
   {
      // , setPropertyValue
      e3.printStackTrace();
   }
   catch (UnknownPropertyException e4)
   {
      // getPropertyValue, setPropertyValue
      e4.printStackTrace();
   }
}


Rory wrote:Ah well, Villeroy: at least you are not the German Professor who was driven mad!

The mad professor was my Grand-Grand-Grandfather Dr Strangelove, advisor to Chancellor Bismarck.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28431
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Java insert hyperLink

Postby Villeroy » Tue May 12, 2020 11:08 am

This is what I get when I inspect an existing hyperlink in OpenOffice 4.1.7
Code: Select all   Expand viewCollapse view
import com.sun.star.beans.PropertyVetoException;
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.NoSuchElementException;
import com.sun.star.container.XEnumeration;
import com.sun.star.container.XEnumerationAccess;
import com.sun.star.container.XNameAccess;
import com.sun.star.frame.XModel;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.text.XText;
import com.sun.star.text.XTextContent;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.RuntimeException;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
   try
   {
      XModel xModel = UnoRuntime.queryInterface(
         XModel.class, oInitialTarget);
      String sURL = xModel.getURL();
            
      XTextDocument xTextDocument = UnoRuntime.queryInterface(
         XTextDocument.class, oInitialTarget);
      XText xText = xTextDocument.getText();
      
      XEnumerationAccess xEnumerationAccess = UnoRuntime.queryInterface(
         XEnumerationAccess.class, xText);
      XEnumeration xEnumeration = xEnumerationAccess.createEnumeration();
      
      XTextContent xTextContent = UnoRuntime.queryInterface(
         XTextContent.class, xEnumeration.nextElement());
      
      XTextRange xTextRange = UnoRuntime.queryInterface(
         XTextRange.class, xTextContent);
      XTextRange xTextRange2 = xTextRange.getEnd();
      
      XTextRange xTextRange3 = xTextRange.getStart();
      
      XPropertySet xPropSet = UnoRuntime.queryInterface(
         XPropertySet.class, xTextRange3);
      
      String sHyperLinkTarget = AnyConverter.toString(xPropSet.getPropertyValue("HyperLinkTarget"));
      
      String sHyperLinkURL = AnyConverter.toString(xPropSet.getPropertyValue("HyperLinkURL"));
      
      xPropSet.setPropertyValue("HyperLinkName", "What_is_that_name");
      
   }
   catch (IllegalArgumentException e1)
   {
      // , setPropertyValue
      e1.printStackTrace();
   }
   catch (PropertyVetoException e2)
   {
      // setPropertyValue
      e2.printStackTrace();
   }
   catch (WrappedTargetException e3)
   {
      // nextElement, getPropertyValue, setPropertyValue
      e3.printStackTrace();
   }
   catch (UnknownPropertyException e4)
   {
      // getPropertyValue, setPropertyValue
      e4.printStackTrace();
   }
   catch (NoSuchElementException e5)
   {
      // nextElement
      e5.printStackTrace();
   }
   catch (RuntimeException e6)
   {
      // createEnumeration, nextElement
      e6.printStackTrace();
   }
}

I don't understand the HyperlinkName which does not appear in the navigator.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28431
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: java insert hyperLink

Postby luodashuai » Wed May 13, 2020 8:10 am

Villeroy wrote:I may have forgotten everything but keep some very old Basic libraries. One of them used XTextCursor, set properties HyperlinkURL, HyperlinkName and HyperlinkTarget and then writes the string to the endof that cursor.
Code: Select all   Expand viewCollapse view
                  cursor.HyperlinkURL = rg.TextField.URL
                  cursor.HyperlinkName = rg.TextField.Representation
                  cursor.HyperlinkTarget = rg.TextField.TargetFrame
                  celltext.setString(rg.getString())

celltext is the end of the cursor.
rg is a text snipped derived from the enumeration of a paragraph.



I am sorry that my signature is incorrect, it is 4.1.7, and cursor in this version no longer has these properties
OpenOffice 4.1.7
luodashuai
 
Posts: 5
Joined: Tue May 12, 2020 4:17 am

Re: How does OpenOffice call Java to insert hyperlinks into

Postby Villeroy » Wed May 13, 2020 8:56 am

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28431
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How does OpenOffice call Java to insert hyperlinks into

Postby Villeroy » Wed May 13, 2020 9:59 am

I start a new Writer document with dummy text and call MRI.
I getText, getEnd, test if this is a blank paragraph, setText, expand back until the beginning of the paragraph, apply character style "Internet LInk" (just cosmetic) and set property HyperlinkURL.
The result is a formatted hyperlink at the end of the document and the following Java code. I do not code any Java.
Code: Select all   Expand viewCollapse view
import com.sun.star.beans.PropertyVetoException;
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.text.XParagraphCursor;
import com.sun.star.text.XText;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
   try
   {
      XTextDocument xTextDocument = UnoRuntime.queryInterface(
         XTextDocument.class, oInitialTarget);
      XText xText = xTextDocument.getText();
      
      XTextRange xTextRange = UnoRuntime.queryInterface(
         XTextRange.class, xText);
      XTextRange xTextRange2 = xTextRange.getEnd();
      
      XParagraphCursor xParagraphCursor = UnoRuntime.queryInterface(
         XParagraphCursor.class, xTextRange2);
      boolean bisStartOfParagraph = xParagraphCursor.isStartOfParagraph();
      
      boolean bisEndOfParagraph = xParagraphCursor.isEndOfParagraph();
      
      XTextCursor xTextCursor = UnoRuntime.queryInterface(
         XTextCursor.class, xTextRange2);
      boolean bisCollapsed = xTextCursor.isCollapsed();
      
      xTextRange2.setString("How does OpenOffice call Java to insert hyperlinks into text?");
      
      boolean bgotoStartOfParagraph = xParagraphCursor.gotoStartOfParagraph(true);
      
      XPropertySet xPropSet = UnoRuntime.queryInterface(
         XPropertySet.class, xTextRange2);
      xPropSet.setPropertyValue("CharStyleName", "Internet Link");
      
      xPropSet.setPropertyValue("HyperLinkURL", "https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=101935");
      
   }
   catch (WrappedTargetException e1)
   {
      // setPropertyValue
      e1.printStackTrace();
   }
   catch (PropertyVetoException e2)
   {
      // setPropertyValue
      e2.printStackTrace();
   }
   catch (IllegalArgumentException e3)
   {
      // setPropertyValue
      e3.printStackTrace();
   }
   catch (UnknownPropertyException e4)
   {
      // setPropertyValue
      e4.printStackTrace();
   }
}
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28431
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How does OpenOffice call Java to insert hyperlinks into

Postby luodashuai » Thu May 21, 2020 4:22 pm

Villeroy, thank you very much for the way you implemented the insertion of hyperlinks into the document.Thank you very much
OpenOffice 4.1.7
luodashuai
 
Posts: 5
Joined: Tue May 12, 2020 4:17 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 2 guests