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

Java, C++, C#, Delphi, ??? - Using the UNO bridges

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

Postby sarath » Sat Oct 31, 2009 6:11 am

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   Expand viewCollapse view
    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   Expand viewCollapse view
oGraphic.GraphicURL
and this
Code: Select all   Expand viewCollapse view
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
sarath
 
Posts: 10
Joined: Tue Oct 27, 2009 5:50 pm

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

Postby hanya » Sat Oct 31, 2009 6:15 pm

sarath wrote:Calling this
Code: Select all   Expand viewCollapse view
    oGraphic.GraphicURL
and this
Code: Select all   Expand viewCollapse view
    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
hanya
Volunteer
 
Posts: 884
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

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

Postby sarath » Sun Nov 01, 2009 2:35 pm

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   Expand viewCollapse view
   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   Expand viewCollapse view
   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   Expand viewCollapse view
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
sarath
 
Posts: 10
Joined: Tue Oct 27, 2009 5:50 pm


Return to External Programs

Who is online

Users browsing this forum: No registered users and 1 guest