OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Inserting an image copy (not a linked image)

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
DelphaX
General User
General User


Joined: 13 Oct 2004
Posts: 6

PostPosted: Wed Oct 13, 2004 7:23 am    Post subject: Inserting an image copy (not a linked image) Reply with quote

Hi,

I just recently started programming OpenOffice with Java and there's something that's bugging me. I already know how to insert an image in a Spreadsheet or Writer document, but what I want to do is make sure the image in the document is a copy of the image file and not just a link, so that when I move, rename or delete the image file, the image is still present in the document.

I know how to do it in OpenOffice (Link option when picking image file to insert), but what I'd like to know is how to do it via Java ?

As I said before, just creating a GraphicObjectShape and setting its GraphicURL property just creates a shape that links to the image file, not a copy of it.

Thanks in advance
Back to top
View user's profile Send private message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Wed Oct 13, 2004 8:23 am    Post subject: Reply with quote

I have read somewhere it is only possible with menus not with UNO API.
I never go further to convince myself if its true or false.
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Wed Oct 13, 2004 9:39 am    Post subject: Reply with quote

See section 7.15.3 Can I embed or link a graphics object? in my free macro document (I do not remember if I cover this in my published book.....) It turns out that you can do this with a com.sun.star.drawing.GraphicObjectShape but not with a com.sun.star.text.GraphicObject (this is unfortunate because I prefer the com.sun.star.text.GraphicObject objects in a Write document). I also understand that you may be able to do this in the next version of OOo (as in version 2.0).

This might be possible with a UNO dispatch, but I have not specifically looked into this.
_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
packer
Newbie
Newbie


Joined: 14 Oct 2004
Posts: 3

PostPosted: Thu Oct 14, 2004 5:25 am    Post subject: Insert image contents and not only link Reply with quote

Hello.

I have been trying for some days to insert an image in a document with the help of this forum. According to OOo documentation is impossible to insert a GraphicObject but as a link. However, I have been able to insert it from java by using macro code and a bit of luck.

I include now the neccessary code to do that:

-------------------------------------------------------------
private void insertImage(XMultiServiceFactory xFactory, XComponent xcomponent, XTextRange xTextRange, String url)
{
// Querying for the interface XTextDocument on the xcomponent
XTextDocument xtextdocument = ( XTextDocument ) UnoRuntime.queryInterface(XTextDocument.class, xcomponent );
XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class, xcomponent);
XController xController = xModel.getCurrentController();

// the controller gives us the TextViewCursor
XTextViewCursorSupplier xViewCursorSupplier = (XTextViewCursorSupplier)UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xController);
XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();

xViewCursor.gotoRange(xTextRange, false);

try {
Object dispatchHelperObject = xFactory.createInstance("com.sun.star.frame.DispatchHelper");
XDispatchHelper xDispatchHelper = (com.sun.star.frame.XDispatchHelper) UnoRuntime.queryInterface(
com.sun.star.frame.XDispatchHelper.class, dispatchHelperObject);

PropertyValue[] args = new PropertyValue[4];
args[0] = new PropertyValue();
args[0].Name = "FileName";
args[0].Value = url;
args[1] = new PropertyValue();
args[1].Name = "FilterName";
args[1].Value = "<Todos los formatos>";
args[2] = new PropertyValue();
args[2].Name = "AsLink";
args[2].Value = new Boolean(false);
args[3] = new PropertyValue();
args[3].Name = "Style";
args[3].Value = "Imagen";

xController = xtextdocument.getCurrentController();
XFrame xFrame = xController.getFrame();
XDispatchProvider xDispatchProvider = (XDispatchProvider)UnoRuntime.queryInterface(XDispatchProvider.class, xFrame);

xDispatchHelper.executeDispatch(xDispatchProvider, ".uno:InsertGraphic", "", 0, args);
} catch (Exception e) {
e.printStackTrace();
}
}
-------------------------------------------------

To obtain the variables to call this function:
XFactory:
-------------------------------------------------------------------
private XMultiServiceFactory connect(String strConnect) throws com.sun.star.uno.Exception,
java.lang.Exception
{

XMultiServiceFactory xFactory = null;

// Get component context
XComponentContext xcomponentcontext = Bootstrap.createInitialComponentContext(null);

// initial serviceManager
XMultiComponentFactory xLocalServiceManager = xcomponentcontext.getServiceManager();

// create a connector, so that it can contact the office
Object xUrlResolver = xLocalServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", xcomponentcontext);

XUnoUrlResolver urlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(
XUnoUrlResolver.class, xUrlResolver);

Object rInitialObject = urlResolver.resolve(strConnect);

XNamingService rName = (XNamingService) UnoRuntime.queryInterface(XNamingService.class,
rInitialObject);

if (rName != null)
{
Object rXsmgr = rName.getRegisteredObject("StarOffice.ServiceManager");
xFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class,
rXsmgr);
}

return xFactory;
}
---------------------------------------------

For XComponent:

---------------------------------------------------
private XDesktop getDesktop(XMultiServiceFactory xFactory) throws com.sun.star.uno.Exception
{

XInterface xInterface = null;
XDesktop xDesktop = null;

xInterface = (XInterface) xFactory.createInstance("com.sun.star.frame.Desktop");
xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, xInterface);

return xDesktop;
}

private XComponent loadDocument(XDesktop xDesktop, String strURL)
throws com.sun.star.uno.Exception
{

XComponent xComponent = null;
PropertyValue[] arguments = new PropertyValue[1];
arguments[0] = new PropertyValue();
arguments[0].Name = "Hidden";
arguments[0].Value = new Boolean(true);

XComponentLoader xComponentLoader = null;

xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,
xDesktop);

xComponent = xComponentLoader.loadComponentFromURL(strURL, "_blank", 0, arguments);

return xComponent;
}
---------------------------------------------------

You can obtain a XTextRange as you prefer.

This code will replace the XTextRange you pass to the function insertImage with the contents of the file referenced by the url.

I hope this helps someone.
Thank you all for all the posts about macros, inserting images and invoking DispatchHelper from java.
Back to top
View user's profile Send private message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Thu Oct 14, 2004 8:17 am    Post subject: Reply with quote

Interesting.
Please put this code in the code snippets thread. It's a thread for working code.
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
DelphaX
General User
General User


Joined: 13 Oct 2004
Posts: 6

PostPosted: Thu Oct 14, 2004 11:15 pm    Post subject: Reply with quote

Wow, thanks ! such quick answers, and good ones at that ! Wink

Many thanks to all
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group