| View previous topic :: View next topic |
| Author |
Message |
DelphaX General User

Joined: 13 Oct 2004 Posts: 6
|
Posted: Wed Oct 13, 2004 7:23 am Post subject: Inserting an image copy (not a linked image) |
|
|
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 |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
pitonyak Administrator


Joined: 09 Mar 2004 Posts: 3618 Location: Columbus, Ohio, USA
|
Posted: Wed Oct 13, 2004 9:39 am Post subject: |
|
|
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 |
|
 |
packer Newbie

Joined: 14 Oct 2004 Posts: 3
|
Posted: Thu Oct 14, 2004 5:25 am Post subject: Insert image contents and not only link |
|
|
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 |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
DelphaX General User

Joined: 13 Oct 2004 Posts: 6
|
Posted: Thu Oct 14, 2004 11:15 pm Post subject: |
|
|
Wow, thanks ! such quick answers, and good ones at that !
Many thanks to all |
|
| Back to top |
|
 |
|
|
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
|