[Solved] Call Java to insert hyperlinks into text
Posted: Tue May 12, 2020 4:19 am
				
				How does openoffice call Java to insert hyperlinks into text
			User community support forum for Apache OpenOffice, LibreOffice and all the OpenOffice.org derivatives
https://forum.openoffice.org/en/forum/
https://forum.openoffice.org/en/forum/viewtopic.php?t=101935
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."Villeroy wrote:In OpenOffice 2.4? I don't remember.
Code: Select all
                  cursor.HyperlinkURL = rg.TextField.URL
                  cursor.HyperlinkName = rg.TextField.Representation
                  cursor.HyperlinkTarget = rg.TextField.TargetFrame
                  celltext.setString(rg.getString())
Please update your signature in this forum - if you are using other version then that archaic-old version of the OpenOffice.org.
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
Code: Select all
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();
	}
}The mad professor was my Grand-Grand-Grandfather Dr Strangelove, advisor to Chancellor Bismarck.Rory wrote:Ah well, Villeroy: at least you are not the German Professor who was driven mad!
Code: Select all
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();
	}
}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.celltext is the end of the cursor.Code: Select all
cursor.HyperlinkURL = rg.TextField.URL cursor.HyperlinkName = rg.TextField.Representation cursor.HyperlinkTarget = rg.TextField.TargetFrame celltext.setString(rg.getString())
rg is a text snipped derived from the enumeration of a paragraph.
Code: Select all
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();
	}
}