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

insert image contents in a Writer document

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
packer
Newbie
Newbie


Joined: 14 Oct 2004
Posts: 3

PostPosted: Thu Oct 14, 2004 11:31 pm    Post subject: insert image contents in a Writer document 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 inspired by Andrew Pitonyak's macro code (thanks a lot to you Smile 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
View user's profile Send private message
SergeM
Super User
Super User


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

PostPosted: Fri Oct 15, 2004 9:06 am    Post subject: Reply with quote

Interesting. I will translate this code in C++, but when ?
_________________
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
ShaKeSPeaR
Newbie
Newbie


Joined: 18 Jul 2008
Posts: 1

PostPosted: Fri Jul 18, 2008 2:18 am    Post subject: Reply with quote

Thanks for this example! This is my simple code in C++, written on Borland Builder 6 (for MS Windows only Smile). 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
View user's profile Send private message
rclakmal
General User
General User


Joined: 23 Sep 2010
Posts: 5

PostPosted: Mon Sep 27, 2010 10:16 am    Post subject: Reply with quote

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
View user's profile Send private message
floris_v
Moderator
Moderator


Joined: 12 Jul 2007
Posts: 4791
Location: Netherlands

PostPosted: Mon Sep 27, 2010 10:26 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets 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