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

How to insert a graphic into a document
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
simje
Newbie
Newbie


Joined: 09 May 2005
Posts: 1

PostPosted: Mon May 09, 2005 5:38 am    Post subject: Re: How to insert a graphic into a document Reply with quote

I must note, that I am new to oo and java.

My problem is, that I just cant figure out how can I use BitmapTable service in my code, as this class is not defined anywhere, so I cannot use methods specified in the example.

Please help...
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: Mon May 09, 2005 9:30 am    Post subject: Reply with quote

I have takled the problem of inserting an image in a OOoDraw document in my Document. It uses C++ as programming language (I have no experience with Java). If your problem is really to use the bitmapTable service, sorry this is not tackled in my document.
_________________
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
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Sat May 14, 2005 10:16 am    Post subject: Re: How to insert a graphic into a document Reply with quote

simje wrote:
I must note, that I am new to oo and java.

My problem is, that I just cant figure out how can I use BitmapTable service in my code, as this class is not defined anywhere, so I cannot use methods specified in the example.

Please help...

For documentation of the BitmapTable service see the API docs. I gave a bunch of API links in the first message of this thread.

You need to include in your classpath the jar files in your OOo/program/classes folder.

If you have more questions about using Java and OOo in general, please start a new thread in the Macros and API section. (This way, everyone can see what the thread is about, and get the benefit of a focused discussion on that topic.)

See also...


How to navigate the OOo API Docs
================================

In Java, how to use API Docs to find available interfaces, methods.
How to call getSupportedServiceNames()
http://www.oooforum.org/forum/viewtopic.php?p=58512#58512

In this post, begin at the sentence...
"So how did I learn about how to use removeByIndex() ?"
http://www.oooforum.org/forum/viewtopic.php?p=25537#25537

http://www.oooforum.org/forum/viewtopic.php?p=29989#29989
http://www.oooforum.org/forum/viewtopic.php?p=13511#13511
http://www.oooforum.org/forum/viewtopic.php?p=15399#15399
http://www.oooforum.org/forum/viewtopic.php?p=15246#15246
http://www.oooforum.org/forum/viewtopic.php?p=11995#11995
http://www.oooforum.org/forum/viewtopic.php?t=3479
http://www.oooforum.org/forum/viewtopic.php?p=16186#16186
http://www.oooforum.org/forum/viewtopic.php?t=4608
http://www.oooforum.org/forum/viewtopic.php?p=13834#13834
http://www.oooforum.org/forum/viewtopic.php?p=15034#15034
http://www.oooforum.org/forum/viewtopic.php?t=3735
http://www.oooforum.org/forum/viewtopic.php?t=3681

XShapes, DrawPage, GenericDrawPage
http://www.oooforum.org/forum/viewtopic.php?t=5328

Developer's Guide
http://api.openoffice.org/DevelopersGuide/DevelopersGuide.html

(Developer's Guide TOC is here)
http://api.openoffice.org/docs/DevelopersGuide/DevelopersGuide.htm

and the online API reference....
http://api.openoffice.org/docs/common/ref/com/sun/star/module-ix.html

Useful links from Russ...
http://www.oooforum.org/forum/viewtopic.php?p=16374#16374
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
dcrintea
Power User
Power User


Joined: 26 Feb 2004
Posts: 68

PostPosted: Wed Jun 15, 2005 7:21 am    Post subject: ... Reply with quote

I would like to insert graphic from an input stream (I do this already - loading the document from an input stream).

I tried to load also the image from an input stream:

Code:

        XComponentLoader xcomponentloader = ( XComponentLoader )
        UnoRuntime.queryInterface( XComponentLoader.class,
        xRemoteServiceManager.createInstanceWithContext(
        "com.sun.star.drawing.BitmapTable", xRemoteContext ) );
       
        PropertyValue[] loadProperties = new PropertyValue[1];
        loadProperties[ 0 ] = new PropertyValue();
        loadProperties[ 0 ].Name = "InputStream";
        loadProperties[ 0 ].Value = ois;           
           
        Object newImage = xcomponentloader.loadComponentFromURL("private:stream", "_beamer", 0, loadProperties );          


Any idea if images can be loaded from input streams, just like documents,
and what should I change in the above code?

Thank you.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Wed Jun 15, 2005 7:50 am    Post subject: Reply with quote

Over here
http://www.oooforum.org/forum/viewtopic.phtml?p=82012#82012
see my explanation of why your queryInterface will fail.



If you can access a graphic as a stream, then I think a different approach might work.

Open the stream as a new drawing. For example, use the Desktop's XComponentLoader to open a new (invisible) Drawing document, using the import filter to import a JPG, giving the URL of the jpeg.

You get a new invisible drawing that has only one object -- the jpeg.

Get the first draw page of the drawing. Then get the only object on the draw page. It is a shape. Select the shape. Copy to the clipboard. Close the drawing. Then in your drawing, paste the shape from the clipboard. The shape is selected, so get the selection from the document controller, and you now have a reference to the pasted shape. You can then resize or reposition the shape anywhere you want.

Also note that when you paste the shape, you could actually paste it into any kind of document: Writer, Calc, Impress, etc.

In my Basic Library (elsewhere here in Code Snippets) I have high level routines to make operations like Select, Copy, Paste, etc. into single line operations in Basic. I would advise creating similar high-level routines in Java.

In order to load a Drawing, importing from a jpeg, via. a stream, see these...

Load a document from an XInputStream using private:stream
Load a document from database blob
http://www.oooforum.org/forum/viewtopic.php?t=6807
http://www.oooforum.org/forum/viewtopic.php?t=7032
http://www.oooforum.org/forum/viewtopic.php?t=7010
how to store to blob, technique explained, but no working example
http://www.oooforum.org/forum/viewtopic.php?t=7011
http://www.oooforum.org/forum/viewtopic.phtml?t=17181
Working example, store a document to private:stream
http://www.oooforum.org/forum/viewtopic.php?p=55665#55665
Document output to Http Response
http://www.oooforum.org/forum/viewtopic.php?p=58945#58945
http://www.oooforum.org/forum/viewtopic.php?p=55665#55665
Possible to save to an OutputStream?
http://www.oooforum.org/forum/viewtopic.phtml?t=18431
Example of how to use TypeDetection on an input stream
http://www.oooforum.org/forum/viewtopic.phtml?p=80918#80918
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
dcrintea
Power User
Power User


Joined: 26 Feb 2004
Posts: 68

PostPosted: Wed Jun 15, 2005 8:58 am    Post subject: ... Reply with quote

1. first problem

I have no problems working with I/O streams for documents.

OO server does not want to open a JPG from an input stream.

I can not open the image from an URL, It is a must to open the image from a stream also.
Is there another option?


2. I can not find any doc regarding object copy-paste.

In some Basic example i see: ClipboardCopy( oDoc )
Could you give more details about Clipboard ?

Thank you.
Back to top
View user's profile Send private message
dcrintea
Power User
Power User


Joined: 26 Feb 2004
Posts: 68

PostPosted: Thu Jun 16, 2005 12:54 am    Post subject: .. Reply with quote

I succeded to open the jpeg in Draw.
The filter name for loading a jpeg must be clearly indicated:
Code:

        loadProperties[ 1 ] = new PropertyValue();
        loadProperties[ 1 ].Name = "FilterName";
        loadProperties[ 1 ].Value = "JPG - JPEG";                 


otherwise it takes the jpeg as writer_Text_encoded.

But now I have problems to copy-paste the jpeg from Draw into the Writer(Text) document.

I do:
Code:

        Object oClipboard = xRemoteServiceManager.createInstanceWithContext("com.sun.star.datatransfer.clipboard.SystemClipboard", xRemoteContext);
        XClipboard xClipboard = (XClipboard) UnoRuntime.queryInterface(XClipboard.class, oClipboard);
        ClipboardOwner aClipOwner = new ClipboardOwner();
.....
       Object newImage = xShapes.getByIndex(0);
.....
        xClipboard.setContents(new BmpTransferable(newImage), aClipOwner);
       
        XTransferable xTransferable = xClipboard.getContents();
        DataFlavor[] aDflvArr = xTransferable.getTransferDataFlavors();
        Object aData = xTransferable.getTransferData(aDflvArr[0]);

        XTextContent xNewTextContent = ( XTextContent ) UnoRuntime.queryInterface(XTextContent.class, aData );
               
        xText.insertTextContent( xTextCursor, xNewTextContent, false );


xText.insertTextContent says "second parameter invalid",
meaning that the object(image) I get from Draw can not paste into Writer.

Any idea?

Thank you.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Jun 16, 2005 7:03 am    Post subject: Re: ... Reply with quote

dcrintea wrote:
OO server does not want to open a JPG from an input stream.

I can not open the image from an URL, It is a must to open the image from a stream also.


Here is an example of opening a drawing from a jpeg that comes from a stream.

Code:

Sub Main
   ' Obtain an input stream for a JPEG file.
   ' In this example, the XInputStream comes from a local jpeg file.
   cFile = "C:\Danny\Graphics\microsoft cash cow.jpg"
   oInputStream = GetStreamForFile( cFile )
   
   ' Open a drawing document from an XInputStream which is a JPEG file.
   ' (The XInputStream is connected to a local file,
   '  but that is irrelevant.)
   oDoc = StarDesktop.loadComponentFromURL( "private:stream", "_blank", 0, _
      Array( _
         MakePropertyValue( "InputStream", oInputStream ),_
         MakePropertyValue( "FilterName", "JPG - JPEG" ) ) )
End Sub


' This function takes a cFile, and returns an XInputStream
'  which is connected to the URL.
Function GetStreamForFile( cFile )
   cUrl = ConvertToURL( cFile )

   ' The SimpleFileAccess service provides mechanisms to open, read, write files,
   '  as well as scan the directories of folders to see what they contain.
   ' The advantage of this over Basic's ugly file manipulation is that this
   '  technique works the same way in any programming language.
   '  Furthermore, the program could be running on one machine, while the SimpleFileAccess
   '  accesses files from the point of view of the machine running OOo, not the machine
   '  where, say a remote Java or Python program is running.
   oSimpleFileAccess = createUnoService( "com.sun.star.ucb.SimpleFileAccess" )
   
   ' Open input file.
   oInputStream = oSimpleFileAccess.openFileRead( cUrl )
   
   GetStreamForFile = oInputStream
End Function

Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue
   oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" )
   If Not IsMissing( cName ) Then
      oPropertyValue.Name = cName
   EndIf
   If Not IsMissing( uValue ) Then
      oPropertyValue.Value = uValue
   EndIf
   MakePropertyValue() = oPropertyValue
End Function


Tested in OOo 1.1.4 and OOo 1.9.104 on Win XP.

An import filter is used so that the jpeg is opened into a drawing.

The URL given to loadComponentFromURL is the url: "private:stream". When this url is used, you must also supply another property called InputStream, which contains the stream. Therefore, you can load a document using loadComponentFromURL, from a stream. You can even use an import filter in the process.

See Also....

Load a document from an XInputStream using private:stream
Load a document from database blob
http://www.oooforum.org/forum/viewtopic.php?t=6807
http://www.oooforum.org/forum/viewtopic.php?t=7032
http://www.oooforum.org/forum/viewtopic.php?t=7010
how to store to blob, technique explained, but no working example
http://www.oooforum.org/forum/viewtopic.php?t=7011
http://www.oooforum.org/forum/viewtopic.phtml?t=17181
Working example, store a document to private:stream
http://www.oooforum.org/forum/viewtopic.php?p=55665#55665
Document output to Http Response
http://www.oooforum.org/forum/viewtopic.php?p=58945#58945
http://www.oooforum.org/forum/viewtopic.php?p=55665#55665
Possible to save to an OutputStream?
http://www.oooforum.org/forum/viewtopic.phtml?t=18431
Example of how to use TypeDetection on an input stream
http://www.oooforum.org/forum/viewtopic.phtml?p=80918#80918
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Jun 16, 2005 7:05 am    Post subject: Re: ... Reply with quote

dcrintea wrote:
2. I can not find any doc regarding object copy-paste.

In some Basic example i see: ClipboardCopy( oDoc )
Could you give more details about Clipboard ?


Study my Basic code, starting at the routine ClipboardCopy.

What you'll find is that I am using the dispatcher to execute the url: .uno:Copy, to cause the dispatcher to do a Copy as if the user had picked the Edit --> Copy command from the menu.

Using the dispatcher may or may not work if your window is invisible. I am not sure.

All the ClipboardCopy routine is, is a heavy layer of sugar-coating around the dispatcher.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
dcrintea
Power User
Power User


Joined: 26 Feb 2004
Posts: 68

PostPosted: Thu Jun 16, 2005 9:08 pm    Post subject: ... Reply with quote

Thank you for your answers.

I know(knew) how to open the JPG from a stream,

BUT I want it to open as a graphic insertion, and not as a new file in Draw.

Why the only way to insert a graphic it is from an url? Smile

Thank you.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Fri Jun 17, 2005 7:10 am    Post subject: Re: ... Reply with quote

dcrintea wrote:
I know(knew) how to open the JPG from a stream,
BUT I want it to open as a graphic insertion, and not as a new file in Draw.
Why the only way to insert a graphic it is from an url? Smile


One approach I was suggesting, is to open the jpeg as a new drawing (but an invisible drawing). Then find the only shape on the first draw page. Select it, copy it. Close drawing. Then paste into your own document.

A different approach, which you seem to favor is to Insert the graphic using a url. In that case, you need a URL. The problem is that you have a stream of the data. The obvious solution is for you to pull the data through the stream and create a local temp file, and then use a url that points to the temp file. Don't forget to delete that temp file.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
disc0stu
General User
General User


Joined: 01 Aug 2005
Posts: 9

PostPosted: Mon Aug 15, 2005 12:28 pm    Post subject: Reply with quote

I cant figure out how to use Java rather than Basic to perform the function of permanently embedding the image using the bitmap table Does anyone have any code snippets or suggestions that might help?

thanks in advance,
Disc0stu
Back to top
View user's profile Send private message
dcrintea
Power User
Power User


Joined: 26 Feb 2004
Posts: 68

PostPosted: Mon Aug 15, 2005 11:05 pm    Post subject: ... Reply with quote

Code:

XTextDocument xTextDocument = ( XTextDocument ) UnoRuntime.queryInterface(XTextDocument.class, xo );
XText xText = xTextDocument.getText();
XTextCursor xTextCursor = xText.createTextCursor();                 

XMultiServiceFactory xMultiServiceFactory = ( XMultiServiceFactory ) UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument );     
Object newImage = xMultiServiceFactory.createInstance("com.sun.star.text.GraphicObject" );
XTextContent xNewTextContent = ( XTextContent ) UnoRuntime.queryInterface(XTextContent.class, newImage );       

Object oBitmaps = xMultiServiceFactory.createInstance("com.sun.star.drawing.BitmapTable");
XNameContainer xBitmaps = (XNameContainer)UnoRuntime.queryInterface(XNameContainer.class, oBitmaps);
xBitmaps.insertByName(imageName, url);             
Object insideurl = xBitmaps.getByName(imageName);         
            
XPropertySet xNewPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,newImage);
xNewPropertySet.setPropertyValue( "GraphicURL", insideurl );

XNamed xName = (XNamed)UnoRuntime.queryInterface(XNamed.class, newImage);
xName.setName(imageName);

Back to top
View user's profile Send private message
disc0stu
General User
General User


Joined: 01 Aug 2005
Posts: 9

PostPosted: Tue Aug 16, 2005 9:41 am    Post subject: Reply with quote

I should have been more clear...will the above code (slightly modified) work also for impress documents?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Aug 16, 2005 10:00 am    Post subject: Reply with quote

disc0stu wrote:
I should have been more clear...will the above code (slightly modified) work also for impress documents?


In general, Impress is like Draw, but with additional features. Anything you can do in Draw, you can do in Impress.
_________________
Want to make OOo Drawings like the colored flower design to the left?
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
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
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