| View previous topic :: View next topic |
| Author |
Message |
packer Newbie

Joined: 14 Oct 2004 Posts: 3
|
Posted: Thu Oct 14, 2004 11:31 pm Post subject: insert image contents in a Writer document |
|
|
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 inspired by Andrew Pitonyak's macro code (thanks a lot to you and a bit of luck.
I include now the neccessary code to do that:
-------------------------------------------------------------
| Code: | 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:
-------------------------------------------------------------------
| Code: | 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.
P.D.: While testing this code I have seen that sometimes (still trying to find a clue) openoffice is hanging and I need to restart it. If someone finds out why this can be happening I would thank you very much.
Tagged code as code - floris v, moderator |
|
| Back to top |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
ShaKeSPeaR Newbie

Joined: 18 Jul 2008 Posts: 1
|
Posted: Fri Jul 18, 2008 2:18 am Post subject: |
|
|
Thanks for this example! This is my simple code in C++, written on Borland Builder 6 (for MS Windows only ). This code is a part of test automation application (this part: finding text tag for image in open document and replacing it by image from file).
| Code: |
#include <variants.hpp>
//create serviceManager, desktop and dispatchHelper object
Variant ServiceManager = Variant::CreateObject("com.sun.star.ServiceManager");
Variant Desktop = ServiceManager.OleFunction("createInstance",
StringToOleStr("com.sun.star.frame.Desktop"));
Variant DispatchHelper = ServiceManager.OleFunction("createInstance",
StringToOleStr("com.sun.star.frame.DispatchHelper"));
//open document template
int Bounds[2] = {0,-1};
Variant Document = Desktop.OleFunction("LoadComponentFromURL",
StringToOleStr(/*path to document template*/),
StringToOleStr("_blank"), 0, VarArrayCreate(Bounds, 1, varVariant) );
//get current frame from document
Variant CurrentFrame = Document.OleFunction("getCurrentController").OleFunction("getFrame");
//finding text '%diagramm%' (this is tag for image) and select it
Variant xSearchDescr = Document.OleFunction("createSearchDescriptor");
xSearchDescr.OlePropertySet("SearchString",StringToOleStr(%diagramm%));
xSearchDescr.OlePropertySet("SearchCaseSensitive", false);
xSearchDescr.OlePropertySet("SearchWords", true);
Variant xFound = Document.OleFunction("findAll", xSearchDescr);
//found text is selected to be replaced by pasted image
if (xFound.OleFunction("getCount") > 0)
{
Document.OleFunction("getCurrentController").OleProcedure("select",xFound.OleFunction("getByIndex",0));
}
//prepare structure with image name
int Bounds2[2] = {0,0};
Variant VariantArray = VarArrayCreate(Bounds2, 1, varVariant);
Variant MyStruct = ServiceManager.OleFunction("Bridge_GetStruct","com.sun.star.beans.PropertyValue");
MyStruct.OlePropertySet("name", StringToOleStr("FileName"));
MyStruct.OlePropertySet("value", StringToOleStr(/*path to image here*/));
VariantArray.PutElement(MyStruct, 0);
//call the executeDispatch function from DispatchHelper.
DispatchHelper.OleProcedure("executeDispatch", CurrentFrame, ".uno:InsertGraphic", "", 0, VariantArray);
//this is all. but all Variant object need to be cleaned (by Clear() method execute)
|
|
|
| Back to top |
|
 |
rclakmal General User

Joined: 23 Sep 2010 Posts: 5
|
Posted: Mon Sep 27, 2010 10:16 am Post subject: |
|
|
Can someone please explain me what is meant by the argument "strConnect" in the below method ?
| Quote: | | private XMultiServiceFactory connect(String strConnect) |
And what is meant by argument strURL in the below method ?
| Quote: | | private XComponent loadDocument(XDesktop xDesktop, String strURL) |
I'm totally new to OO API and try to develop a small plugin using java.Any helps .Thanks in Advance !!! |
|
| Back to top |
|
 |
floris_v Moderator


Joined: 12 Jul 2007 Posts: 4619 Location: Netherlands
|
Posted: Mon Sep 27, 2010 10:26 am Post subject: |
|
|
This forum is not meant for asking questions but for posting examples of working code. Go to the Macros and API forum and start a new thread for your question. Still better: go to the new forum (see my signature). _________________ LibreOffice 3.6.3; OOo 3.4.1 on Windows Vista
Join the Official community forum - in several languages, including Nederlandstalig forum |
|
| 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
|