Page 1 of 1

[Solved] Call Java to insert hyperlinks into text

PostPosted: Tue May 12, 2020 4:19 am
by luodashuai
How does openoffice call Java to insert hyperlinks into text

Java insert hyperLink

PostPosted: Tue May 12, 2020 8:13 am
by luodashuai
How do I invoke Java to insert a hyperlink into an openoffice wirter document

Re: java insert hyperLink

PostPosted: Tue May 12, 2020 10:05 am
by Villeroy
In OpenOffice 2.4? I don't remember.

Re: java insert hyperLink

PostPosted: Tue May 12, 2020 10:17 am
by RoryOF
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."

Re: java insert hyperLink

PostPosted: Tue May 12, 2020 10:21 am
by Villeroy
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.

Re: java insert hyperLink

PostPosted: Tue May 12, 2020 10:23 am
by RoryOF
Ah well, Villeroy: at least you are not the German Professor who was driven mad!

Re: java insert hyperLink

PostPosted: Tue May 12, 2020 10:25 am
by Zizi64

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.

Re: java insert hyperLink

PostPosted: Tue May 12, 2020 10:51 am
by Villeroy
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.

Re: Java insert hyperLink

PostPosted: Tue May 12, 2020 11:08 am
by Villeroy
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.

Re: java insert hyperLink

PostPosted: Wed May 13, 2020 8:10 am
by luodashuai
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

Re: How does OpenOffice call Java to insert hyperlinks into

PostPosted: Wed May 13, 2020 8:56 am
by Villeroy

Re: How does OpenOffice call Java to insert hyperlinks into

PostPosted: Wed May 13, 2020 9:59 am
by Villeroy
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();
   }
}

Re: How does OpenOffice call Java to insert hyperlinks into

PostPosted: Thu May 21, 2020 4:22 pm
by luodashuai
Villeroy, thank you very much for the way you implemented the insertion of hyperlinks into the document.Thank you very much