Page 1 of 1

[Solved] Get TextFrames in Text Document with Java?

Posted: Fri Sep 09, 2022 11:13 am
by wanglong
How can I get TextFrame Objects already has been added to Text Document with JAVA?
I didn't found a relevant tutorials with this topic.
Please show me with non scripting language!
Thank you very much!

Re: How can I get TextFrames in Text Document with JAVA?

Posted: Fri Sep 09, 2022 2:51 pm
by FJCC
I do not use Java but I recorded the following code with the MRI extension. The oInitialTarget is the text document, the object you would get with ThisComponent in Basic.

Code: Select all

import com.sun.star.container.NoSuchElementException;
import com.sun.star.container.XNameAccess;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.text.XTextFrame;
import com.sun.star.text.XTextFramesSupplier;
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
	{
		XTextFramesSupplier xTextFramesSupplier = UnoRuntime.queryInterface(
			XTextFramesSupplier.class, oInitialTarget);
		XNameAccess xNameAccess = xTextFramesSupplier.getTextFrames();
		
		XTextFrame xTextFrame = UnoRuntime.queryInterface(
			XTextFrame.class, xNameAccess.getByName("FlowerImage"));
		
	}
	catch (NoSuchElementException e1)
	{
		// getByName
		e1.printStackTrace();
	}
	catch (WrappedTargetException e2)
	{
		// getByName
		e2.printStackTrace();
	}
	catch (RuntimeException e3)
	{
		// getByName
		e3.printStackTrace();
	}
}

Re: How can I get TextFrames in Text Document with JAVA?

Posted: Fri Sep 09, 2022 2:54 pm
by Villeroy

Re: How can I get TextFrames in Text Document with JAVA?

Posted: Fri Sep 09, 2022 4:33 pm
by wanglong
Thanks for your reply so quickly!