[Solved] Breaking links and emedding images into doc - Java

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
sarath
Posts: 10
Joined: Tue Oct 27, 2009 5:50 pm

[Solved] Breaking links and emedding images into doc - Java

Post by sarath »

Hello,
Is there an API call to break links (equivalent of " Edit->Links->Break Link" in the UI)? I want to keep the Word Document and data intact but break the link and embedd the images in the docuemnt itself.
I have seen this happening in VB here.
I am very new to UNO API :?: .. i don't have enough time to dig up and use it :| Could someone help me in doing this. After trying a lot i reached till this:
Can someone help me from here.
NOTE: The following code is incomplete and might have many mistakes. Please do not use

Code: Select all

    public final void embedImagesInWriter(XComponent oDoc)
	{	
		XTextGraphicObjectsSupplier XTxtGraphObjSupplier = (XTextGraphicObjectsSupplier) UnoRuntime.queryInterface(XTextGraphicObjectsSupplier.class, oDoc);
		XNameAccess XNameAcc;
		XMultiServiceFactory xMSFDoc = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc);
		Object oGraphic = xMSFDoc.createInstance("com.sun.star.text.TextGraphicObject");

		String[] allImages = null;
		int x = 0;
		PropertyValue[] aMediaProperties = new PropertyValue[1];

		XNameAcc = XTxtGraphObjSupplier.getGraphicObjects();
		allImages = XNameAcc.getElementNames();
		XGraphicProvider XGraphProv = (XGraphicProvider) UnoRuntime.queryInterface(XGraphicProvider.class, oDoc);
		for (x = 0; x < allImages.length; x++)
		{	
			oGraphic = XNameAcc.getByName(allImages[x]);
			aMediaProperties = MakePropertyValue("URL", oGraphic.GraphicURL);
			oGraphic.Graphic = XGraphProv.queryGraphic(aMediaProperties);
		  
		}
	}

	public final Object MakePropertyValue(String cName, Object uValue)
	{
		PropertyValue[] tempMakePropertyValue = new PropertyValue[1];
		tempMakePropertyValue[0].Name = cName;
		tempMakePropertyValue[0].Value= uValue;
		return tempMakePropertyValue;
	}
Calling this

Code: Select all

oGraphic.GraphicURL
and this

Code: Select all

oGraphic.Graphic
on a Object are not valid..
Something is seriously wrong in this code :crazy: .. please correct it.
Thank you
Last edited by sarath on Mon Nov 02, 2009 8:08 am, edited 3 times in total.
GOOO 3.1.1 on Windows XP
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: [Java] Breaking links and emedding images into doc

Post by hanya »

sarath wrote:Calling this

Code: Select all

    oGraphic.GraphicURL
and this

Code: Select all

    oGraphic.Graphic
"GraphicURL" and "Graphic" are properties, so that you need to use com.sun.star.beans.XPropertySet interface to treat them.
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
sarath
Posts: 10
Joined: Tue Oct 27, 2009 5:50 pm

Re: [Java] Breaking links and emedding images into doc

Post by sarath »

Thanks a lot hanya.. that helped me solve it.

My problem is just partially solved though

here is the code which worked for me:

Code: Select all

	private final void embedImagesInWriter(XComponent oDoc)
	{	
		XTextGraphicObjectsSupplier XTxtGraphObjSupplier = (XTextGraphicObjectsSupplier) UnoRuntime.queryInterface(XTextGraphicObjectsSupplier.class, oDoc);
		XNameAccess XNameAcc;
		XMultiServiceFactory xMSFDoc = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc);
		Object oGraphic=null;
		XComponentContext xComponentContext = openOfficeConnection.getComponentContext();
        XMultiComponentFactory xMCF = xComponentContext.getServiceManager();
        Object graphicProviderObject = null;
		
		graphicProviderObject = xMCF.createInstanceWithContext("com.sun.star.graphic.GraphicProvider", xComponentContext);
        XGraphicProvider XGraphProv = (XGraphicProvider) UnoRuntime.queryInterface(XGraphicProvider.class, graphicProviderObject); 
		oGraphic = xMSFDoc.createInstance("com.sun.star.text.TextGraphicObject");
		
		String[] allImages = null;
		int x = 0;
		PropertyValue[] aMediaProperties = new PropertyValue[1];

		XNameAcc = XTxtGraphObjSupplier.getGraphicObjects();
		allImages = XNameAcc.getElementNames();
		for (x = 0; x < allImages.length; x++)
		{	
			oGraphic = XNameAcc.getByName(allImages[x]);
			XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, oGraphic);
			aMediaProperties = MakePropertyValue("URL", xPropSet.getPropertyValue("GraphicURL").toString());
			xPropSet.setPropertyValue("Graphic", XGraphProv.queryGraphic(aMediaProperties));
		}
	}
and

Code: Select all

	private final PropertyValue[] MakePropertyValue(String cName, Object uValue)
	{
		PropertyValue[] tempMakePropertyValue = new PropertyValue[1];
		tempMakePropertyValue[0] = new PropertyValue();
		tempMakePropertyValue[0].Name = cName;
		tempMakePropertyValue[0].Value= uValue;
		return tempMakePropertyValue;
	}
Changed this line accordingly to get 'XComponentContext '

Code: Select all

XComponentContext xComponentContext = openOfficeConnection.getComponentContext();
I know that the code is not well structured and might not be efficient. Any improvements are welcome.

The only problem I am left with, is the image size. the images that are embedded get a default size instead of their original size.Probably I have to set some other PropertyValues of "xPropSet" to get them. I'l post back the final code if I find it out.
ThankYou
GOOO 3.1.1 on Windows XP
Post Reply