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 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
DannyB
Moderator
Moderator


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

PostPosted: Thu Dec 09, 2004 10:43 am    Post subject: How to insert a graphic into a document Reply with quote

This article describes how to insert a graphic into a document in such a way that the graphic becomes permanently part of the document. The technique works on Calc, Writer, Draw, and Impress documents.

Another mystery solved. I know people have wanted to know how to do this for a long time. It has been discussed off and on for well over a year.

General description

Start with a URL to an external graphic resource. (The "external URL")
Load that graphic into an office document.
Convert the URL into a new URL to the internal graphic resource. (The "internal URL")
Use that new internal URL for the URL of a GraphicObjectShape.

Now, even if the original graphic resource no longer exists, and its URL is invalid, the document now carries a copy of the graphic inside of it, and references using the internal URL will always be valid.

I believe that the internal URL to the graphic can also be used in Dialog controls, or in Form controls. Therefore, image buttons on either forms or dialog boxes could refer to graphics that are within the document by using an "internal URL" to a graphic.


Theory of operation

The DrawingDocument service includes the service DrawingDocumentFactory.

The DrawingDocumentFactory service offers to create various services, such as...

Notice the BitmapTable service.

The BitmapTable service is a collection. A table. It is a table of internal URL's to graphics that live inside of an office document. You simply insert a URL into the table, and give it a name. When you get the URL back, it is transformed into an internal URL.

When you create a GraphicShape, you can assign the internal (or external) url to the GraphicURL property.

When you create a RectangleShape you can assign the internal URL to the FillBitmapURL property. Or you can assign the name of the inserted graphic to the FillBitmapName property. This does not only apply to RectangleShape, but to any shape that has FillProperties.


Draw Example

Here is the code to load two graphics into a drawing. One is a GraphicObjectShape, the other is a RectangleShape.

Code:
Sub Main

   oDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
'   oDoc = StarDesktop.loadComponentFromURL( "private:factory/simpress", "_blank", 0, Array() )
   oDrawPage = oDoc.getDrawPages().getByIndex( 0 )
   
   ' CHANGE ME !!!
   ' CHANGE ME !!!
   cFile = "C:\Documents and Settings\dbrewer\Desktop\microsoft cash cow.jpg"
   'cFile = "/home/danny/Desktop/microsoft cash cow.jpg"
   cUrl = ConvertToUrl( cFile )
   
   ' Convert the URL into an internal URL within the document.
   ' If you comment out this line, then the shape that is created from the url
   '  will refer to the external graphic, which must always be present.
   cUrl = LoadGraphicIntoDocument( oDoc, cUrl, "Cash Cow" )
   ' Now the URL points to a graphic *inside* of the document's Zip file,
   '  rather than an external url.
   
   ' Create a GraphicObjectShape.
   oShape = MakeGraphicObjectShape( oDoc, MakePoint( 1500, 3000 ), MakeSize( 3000, 4000 ) )
   ' Add it to the drawing page.
   oDrawPage.add( oShape )
   ' Set its URL to a particular graphic.
   oShape.GraphicURL = cUrl
   
   
   
   ' Now create a RectangleShape, with a similar fill graphic.
   
   ' CHANGE ME !!!
   ' CHANGE ME !!!
   cFile = "C:\Documents and Settings\dbrewer\Desktop\Bill Gates - Borg.bmp"
   'cFile = "/home/danny/Desktop/Bill Gates - Borg.bmp"
   cUrl = ConvertToUrl( cFile )
   cUrl = LoadGraphicIntoDocument( oDoc, cUrl, "Bill Gates Borg" )
   
   ' Create a GraphicObjectShape.
   oShape = MakeRectangleShape( oDoc, MakePoint( 7500, 3000 ), MakeSize( 3000, 4000 ) )
   ' Add it to the drawing page.
   oDrawPage.add( oShape )
   
   ' Fill the rectangle with a bitmap
   oShape.FillStyle = com.sun.star.drawing.FillStyle.BITMAP

   ' Now assign the URL.
   ' Note that EITHER ONE of the following two techniques will work
   '  to assign the graphic.
   'oShape.FillBitmapName = "Bill Gates Borg"   ' use name of internal graphic
   oShape.FillBitmapURL = cUrl ' use URL of internal OR external graphic
End Sub


The key to the above example is the new LoadGraphicIntoDocument function.
Code:
' Given a URL to an external graphic resource,
'  load that graphic permanently into this drawing document,
'  and return a new URL to the internal resource.
' The new URL can be used in place of the old URL.
Function LoadGraphicIntoDocument( oDoc As Object, cUrl As String, cInternalName As String ) As String
   ' Get the BitmapTable from this drawing document.
   ' It is a service that maintains a list of bitmaps that are internal
   '  to the document.
   oBitmaps = oDoc.createInstance( "com.sun.star.drawing.BitmapTable" )
   
   ' Add an external graphic to the BitmapTable of this document.
   oBitmaps.insertByName( cInternalName, cUrl )
   
   ' Now ask for it back.
   ' What we get back is an different Url that points to a graphic
   '  which is inside this document, and remains with the document.
   cNewUrl = oBitmaps.getByName( cInternalName )
   
   LoadGraphicIntoDocument = cNewUrl
End Function


In order to make the above work, you also need....

Code:
Function MakePoint( ByVal x As Long, ByVal y As Long ) As com.sun.star.awt.Point
   oPoint = createUnoStruct( "com.sun.star.awt.Point" )
   oPoint.X = x
   oPoint.Y = y
   MakePoint = oPoint
End Function

Function MakeSize( ByVal width As Long, ByVal height As Long ) As com.sun.star.awt.Size
   oSize = createUnoStruct( "com.sun.star.awt.Size" )
   oSize.Width = width
   oSize.Height = height
   MakeSize = oSize
End Function

Function MakeRectangleShape( oDoc As Object,_
               Optional oPosition As com.sun.star.awt.Point,_
               Optional oSize As com.sun.star.awt.Size ) As com.sun.star.drawing.RectangleShape
   oShape = oDoc.createInstance( "com.sun.star.drawing.RectangleShape" )
   If Not IsMissing( oPosition ) Then
      oShape.Position = oPosition
   EndIf
   If Not IsMissing( oSize ) Then
      oShape.Size = oSize
   EndIf
   MakeRectangleShape = oShape
End Function

Function MakeGraphicObjectShape( oDoc As Object,_
               Optional oPosition As com.sun.star.awt.Point,_
               Optional oSize As com.sun.star.awt.Size ) As com.sun.star.drawing.GraphicObjectShape
   oShape = oDoc.createInstance( "com.sun.star.drawing.GraphicObjectShape" )
   If Not IsMissing( oPosition ) Then
      oShape.Position = oPosition
   EndIf
   If Not IsMissing( oSize ) Then
      oShape.Size = oSize
   EndIf
   MakeGraphicObjectShape = oShape
End Function


Once you combine all of the above code fragments, you have a complete working program, ready to run.

Please change the lines that say CHANGE ME!!!.
These file paths must be changed to point to graphics on your own particular system.



Writer Example

Code:
Sub Main
   oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )
   oDrawPage = oDoc.getDrawPage()
   
   ' CHANGE ME !!!
   ' CHANGE ME !!!
   cFile = "C:\Documents and Settings\dbrewer\Desktop\microsoft cash cow.jpg"
   'cFile = "/home/danny/Desktop/microsoft cash cow.jpg"
   cUrl = ConvertToUrl( cFile )
   
   ' Convert the URL into an internal URL within the document.
   ' If you comment out this line, then the shape that is created from the url
   '  will refer to the external graphic, which must always be present.
   cUrl = LoadGraphicIntoDocument( oDoc, cUrl, "Cash Cow" )
   ' Now the URL points to a graphic *inside* of the document's Zip file,
   '  rather than an external url.
   
   ' Create a GraphicObjectShape.
   oShape = MakeGraphicObjectShape( oDoc, MakePoint( 1500, 3000 ), MakeSize( 3000, 4000 ) )
   ' Add it to the drawing page.
   oDrawPage.add( oShape )
   ' Set its URL to a particular graphic.
   oShape.GraphicURL = cUrl
End Sub





Calc Example

Code:
Sub Main
   oDoc = StarDesktop.loadComponentFromURL( "private:factory/scalc", "_blank", 0, Array() )
   oSheet = oDoc.getSheets().getByIndex( 0 )
   oDrawPage = oSheet.getDrawPage()
   
   ' CHANGE ME !!!
   ' CHANGE ME !!!
   cFile = "C:\Documents and Settings\dbrewer\Desktop\microsoft cash cow.jpg"
   'cFile = "/home/danny/Desktop/microsoft cash cow.jpg"
   cUrl = ConvertToUrl( cFile )
   
   ' Convert the URL into an internal URL within the document.
   ' If you comment out this line, then the shape that is created from the url
   '  will refer to the external graphic, which must always be present.
   cUrl = LoadGraphicIntoDocument( oDoc, cUrl, "Cash Cow" )
   ' Now the URL points to a graphic *inside* of the document's Zip file,
   '  rather than an external url.
   
   ' Create a GraphicObjectShape.
   oShape = MakeGraphicObjectShape( oDoc, MakePoint( 1500, 3000 ), MakeSize( 3000, 4000 ) )
   ' Add it to the drawing page.
   oDrawPage.add( oShape )
   ' Set its URL to a particular graphic.
   oShape.GraphicURL = cUrl
End Sub



If you understand how the BitmapTable works, you can probably also see how to add custom Gradients, Hatch's, or Dashes into a document.


See also....

Here is an example showing how to insert graphics into a Writer Table.
http://www.oooforum.org/forum/viewtopic.php?p=35993#35993
Note that this example uses URLs of graphics that come from the web.

How to insert a graphic by a macro
http://www.oooforum.org/forum/viewtopic.php?p=22407#22407

Insert graphic, change size
http://www.oooforum.org/forum/viewtopic.php?t=2788
http://www.oooforum.org/forum/viewtopic.php?t=3783
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1569
Location: Frankfurt, Germany

PostPosted: Thu Dec 09, 2004 12:42 pm    Post subject: Reply with quote

Great DannyB.
A few suggestions however:
make the title more appropriate, because it's not only about inserting a graphic into the document but also permanently implementing it into the archive file. I would change it to something like "Embedding graphics into a document".

change the function names, you have two "Main" sub-procedures, thus when one pastes your code into one module he will get a BASIC syntax error.


Christian
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
B Marcelly
Super User
Super User


Joined: 12 May 2004
Posts: 1453
Location: France

PostPosted: Fri Dec 10, 2004 10:04 am    Post subject: Reply with quote

Very interesting article Laughing
Somewhere in the SDK it is said that only linked graphics is possible and I was happy to see a solution.
I started the macros on a Draw document and it worked. Examining the file with zip showed that each bitmap was indeed embedded.

Then I manually selected the image and deleted it, saved the document. But when examining the file with zip I discovered that the image was still in the file, though not visible. Rolling Eyes
With another macro I confirmed (hasByName) that the image was still in the Bitmap collection.

The API says in the page com.sun.star.drawing.BitmapTable that it is not possible to withdraw a bitmap from the collection.
Being curious, I tried removeByName and it worked ! Well, not exactly Confused
The bitmap could no more be obtained with getByName or hasByName, but it was still there, in the document.

OOo is smart enough to store only once a bitmap when it is used several times. A typical example is using a bitmap for bullets in a Writer document.
My interpretation of all this is that OOo maintains a counter of how many times each bitmap is referenced in the document, and this counter is not updated with the proposed solution. I don't know how to handle this counter through API, perhaps it's not accessible.
Too bad Sad

_________
Bernard
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: Fri Dec 10, 2004 11:00 am    Post subject: Reply with quote

Cybb20 wrote:
make the title more appropriate, because it's not only about inserting a graphic into the document but also permanently implementing it into the archive file. I would change it to something like "Embedding graphics into a document".


A very good idea. I might try to make the change tomorrow. (And to all the places I linked the article.)


Cybb20 wrote:
change the function names, you have two "Main" sub-procedures, thus when one pastes your code into one module he will get a BASIC syntax error.


There is one Main for the Draw example.
Another Main for the Writer example.
A third Main for the Calc example.

All three of the Main's share the other routines in common. Maybe I wasn't clear enough that there were three separate examples.
_________________
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: Tue Dec 14, 2004 6:34 am    Post subject: Reply with quote

Someone wanted me to further expand on what I meant....
DannyB wrote:
Start with a URL to an external graphic resource. (The "external URL")
Load that graphic into an office document.
Convert the URL into a new URL to the internal graphic resource. (The "internal URL")
Use that new internal URL for the URL of a GraphicObjectShape.


To explain that, let's look at this portion of the example code from earlier.

Code:
   cFile = "C:\Documents and Settings\dbrewer\Desktop\microsoft cash cow.jpg"
   'cFile = "/home/danny/Desktop/microsoft cash cow.jpg"
   cUrl = ConvertToUrl( cFile )
   
   ' Convert the URL into an internal URL within the document.
   ' If you comment out this line, then the shape that is created from the url
   '  will refer to the external graphic, which must always be present.
   cUrl = LoadGraphicIntoDocument( oDoc, cUrl, "Cash Cow" )
   ' Now the URL points to a graphic *inside* of the document's Zip file,
   '  rather than an external url.
 


The variable cUrl starts out with this URL....
file:///C:/Documents%20and%20Settings/dbrewer/Desktop/microsoft%20cash%20cow.jpg
It contains this url right after the ConvertToURL() function.
This url points to a file on my system. That is, it points to a graphic which is external to the office document.

The magic all happens on this line....

cUrl = LoadGraphicIntoDocument( oDoc, cUrl, "Cash Cow" )

The subroutine LoadGraphicIntoDocument does two things....

  1. It loads the graphic into the document. The document's zip file now contains a copy of the original jpeg image. That image stays with the document. So if I send you the document, it has the image in it, even though you don't have the same jpeg image on your desktop. i.e. normally, on your system, the graphic would not display, because the document's link to the graphic would not find a "microsoft cash cow.jpg" image on your desktop.
  2. It transforms the url into a different url that points into the document.


Remember, the variable cUrl starts out with this URL....
file:///C:/Documents%20and%20Settings/dbrewer/Desktop/microsoft%20cash%20cow.jpg

But after the call to LoadGraphicIntoDocument, the variable cUrl now contains this url....
vnd.sun.star.GraphicObject:10000000000000FA000000BEB3552DDF

Eventually, when we get to the statement....
Code:
oShape.GraphicURL = cUrl

which assigns the url to the graphic, instead of pointing the graphic shape to an external image, it is pointing it to the url of an image which is now part of the office document.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
cam
General User
General User


Joined: 29 Dec 2004
Posts: 8
Location: France

PostPosted: Thu Dec 30, 2004 8:19 am    Post subject: Reply with quote

Here is my Python function to embed a graphic into a spreadsheet document (note that X/Y coordinates are converted to cell positions):

Code:
   # self.page is the Sheet
   # self.createInstance() creates the specified object
   def insert_image(self, url, x, y, width, height):
      oBitmaps = self.createInstance('com.sun.star.drawing.BitmapTable')
      oBitmaps.insertByName(url, url)
      url = oBitmaps.getByName(url)
      size = uno.createUnoStruct('com.sun.star.awt.Size')
      size.Width = width
      size.Height = height
      oShape = self.createInstance('com.sun.star.drawing.GraphicObjectShape')
      oShape.Position = self.page.getCellByPosition(x, y).Position
      oShape.Size = size
      oShape.GraphicURL = url
      oDrawPage = self.page.getDrawPage()
      oDrawPage.add(oShape)
Back to top
View user's profile Send private message
goa103
OOo Advocate
OOo Advocate


Joined: 11 May 2003
Posts: 279

PostPosted: Fri Mar 18, 2005 9:25 am    Post subject: Re: How to insert a graphic into a document Reply with quote

DannyB wrote:
This article describes how to insert a graphic into a document in such a way that the graphic becomes permanently part of the document. The technique works on Calc, Writer, Draw, and Impress documents.

Another mystery solved. I know people have wanted to know how to do this for a long time. It has been discussed off and on for well over a year.


Terrific.

Now I wonder how I can fix the documents I written using my stupid Insert Graphics As Character macro. I didn't know the graphics were only linked to the document, I checked and renaming original graphics file corrupted all documents Sad. Is there a macro to convert my linked graphics to embedded graphics ?

Anyway thanks for this amazing topic, I was about to post my macro when I stumbled on your explanation.

DannyB wrote:


The key to the above example is the new LoadGraphicIntoDocument function.
Code:
' Given a URL to an external graphic resource,
'  load that graphic permanently into this drawing document,
'  and return a new URL to the internal resource.
' The new URL can be used in place of the old URL.
Function LoadGraphicIntoDocument( oDoc As Object, cUrl As String, cInternalName As String ) As String
   ' Get the BitmapTable from this drawing document.
   ' It is a service that maintains a list of bitmaps that are internal
   '  to the document.
   oBitmaps = oDoc.createInstance( "com.sun.star.drawing.BitmapTable" )
   
   ' Add an external graphic to the BitmapTable of this document.
   oBitmaps.insertByName( cInternalName, cUrl )
   
   ' Now ask for it back.
   ' What we get back is an different Url that points to a graphic
   '  which is inside this document, and remains with the document.
   cNewUrl = oBitmaps.getByName( cInternalName )
   
   LoadGraphicIntoDocument = cNewUrl
End Function


I don't really understand why the insertByName method has a aName, what is it used for ?

DannyB wrote:
Remember, the variable cUrl starts out with this URL....
file:///C:/Documents%20and%20Settings/dbrewer/Desktop/microsoft%20cash%20cow.jpg

But after the call to LoadGraphicIntoDocument, the variable cUrl now contains this url....
vnd.sun.star.GraphicObject:10000000000000FA000000BEB3552DDF


See it seems it only needs the URL to generate an internal URL. I don't see why we have to name the element to insert.
_________________
An OOo mascot designer
Back to top
View user's profile Send private message
goa103
OOo Advocate
OOo Advocate


Joined: 11 May 2003
Posts: 279

PostPosted: Fri Mar 18, 2005 9:47 am    Post subject: Re: How to insert a graphic into a document Reply with quote

goa103 wrote:
See it seems it only needs the URL to generate an internal URL. I don't see why we have to name the element to insert.


Wrong guess, try again Smile. In fact the name is used by OOo to uniquely identify images, for example I got the following line in my styles.xml file :

Code:
<draw:fill-image draw:name="Name of the external graphics to embed" xlink:href="#Pictures/100002010000001800000018F8FC9437.png" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/>


As I think naming graphics is inappropriate as the name is rarely used, specially in my case, I will try to find a routine to generate a unique identifier for graphics, like the GUID for Windows applications for example. It's made up by the current date and time and a few other information. It would make the images unique.
_________________
An OOo mascot designer
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 Mar 18, 2005 9:54 am    Post subject: Re: How to insert a graphic into a document Reply with quote

goa103 wrote:
Terrific Now I wonder how I can fix the documents I written using my stupid Insert Graphics As Character macro. I didn't know the graphics were only linked to the document, I checked and renaming original graphics file corrupted all documents Sad. Is there a macro to convert my linked graphics to embedded graphics ?

Probably this can be fixed by a macro.

First I am going to assume that you have NOT renamed all your graphics, so that they are all in the same place, and the document can access them.

Manually, you can "load" the graphics into the document by using the Edit --> Links dialog box.

From a macro, I suspect that a workable approach would be to iterate over all of the graphics, obtain the "external" URL of each one, transform it to an "internal" URL, and then store the new url back to the graphic. Finally, save the document.

goa103 wrote:
I don't really understand why the insertByName method has a aName, what is it used for ?

Not quite sure what you are asking.

goa103 wrote:
See it seems it only needs the URL to generate an internal URL. I don't see why we have to name the element to insert.

I think to keep them unique. But I haven't tested that uniqueness is enforced.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
goa103
OOo Advocate
OOo Advocate


Joined: 11 May 2003
Posts: 279

PostPosted: Fri Mar 18, 2005 11:17 am    Post subject: Re: How to insert a graphic into a document Reply with quote

DannyB wrote:
I think to keep them unique. But I haven't tested that uniqueness is enforced.


Well you get an exception if you try to insert an existing name. The solution I found is to generate a GUID : Graphics Unique IDentifier Smile. The GUID is generated following the OOo syntax : Graphic + number. For example the first time you insert a graphics, OOo use the Graphic1 name, then Graphic 2 and so on.

Here is the GetGUID macro :

Code:
Function GetGUID ()
   
   ' Get bitmap table element names
   
   oDoc = ThisComponent
   oBitmaps = oDoc.createInstance ("com.sun.star.drawing.BitmapTable")
   aNames = oBitmaps.getElementNames ()

   ' GUID syntax : Graphic + number, ex : Graphic1, Graphic2...

   prefix = "Graphic"
   j = 1
   
   ' Try GUID one after an other from 1 to ...
   
   GUIDFound = TRUE

   While (GUIDFound)

      ' Compare the current GUID to all bitmap names
   
      GUID = prefix + j
      
      GUIDFound = FALSE
      
      For i = LBound (aNames) To UBound (aNames)
      
         ' Is the name already used ?
      
         If (aNames (i) = GUID) Then
         
            GUIDFound = TRUE
            Exit For
         
         End If
         
      Next i

      ' If the GUID wasn't find, it means we got it !

      j = j + 1   
         
   Wend
   
   GetGUID = GUID
   
End Function


Now thanks to you all the graphics are embedded to the documents and named using unique names. But I kind of worry as my previous graphics made more bad that good Sad.

By the way do you know why our bitmaps are inserted as styles when inserted graphics by OOo are not ? They're just inserted in the content. I don't really understand why it generates a style...
_________________
An OOo mascot designer
Back to top
View user's profile Send private message
goa103
OOo Advocate
OOo Advocate


Joined: 11 May 2003
Posts: 279

PostPosted: Fri Mar 18, 2005 11:21 am    Post subject: Re: How to insert a graphic into a document Reply with quote

DannyB wrote:
Probably this can be fixed by a macro.

First I am going to assume that you have NOT renamed all your graphics, so that they are all in the same place, and the document can access them.

Manually, you can "load" the graphics into the document by using the Edit --> Links dialog box.

From a macro, I suspect that a workable approach would be to iterate over all of the graphics, obtain the "external" URL of each one, transform it to an "internal" URL, and then store the new url back to the graphic. Finally, save the document.


I agree I will probably follow the same process. Check for all bitmaps from the BitmapTable and all graphics from the content (what's the object ? I will search for it anyway...) and fix their URLs and use your macro... Happy coding Smile.
_________________
An OOo mascot designer
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 Mar 18, 2005 11:40 am    Post subject: Re: How to insert a graphic into a document Reply with quote

goa103 wrote:
I agree I will probably follow the same process. Check for all bitmaps from the BitmapTable and all graphics from the content (what's the object ? I will search for it anyway...) and fix their URLs and use your macro... Happy coding Smile.

Depends on the type of document.

In a Draw / Impress document...

  1. getDrawPages() gives you a collection of pages
  2. iterate over each page
  3. on each page, iterate over all shapes on the page
  4. If a shape has a graphic url, then fix it


In a Calc document...

  1. getSheets() gives you a collection of sheets
  2. iterate over each sheet
  3. on each sheet call getDrawPage()
  4. The draw page is the same thing you had in the above Draw / Impress example. Iterate over each shape on the draw page in the same way.


How to iterate over shapes on a draw page? Search OOoForum for "FindShapeByName", a routine I have written and no doubt posted in the past.

In a Writer document....

  • I don't know. But there is a single draw page for the entire doc. Just call getDrawPage() on the doc. Then iterate over all shapes on that draw page and you've done the whole document.
  • Is it possible to have graphics in a Writer document that are not shapes on the draw page and would be covered by the previous procedure?

_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
goa103
OOo Advocate
OOo Advocate


Joined: 11 May 2003
Posts: 279

PostPosted: Sat Mar 19, 2005 4:03 am    Post subject: Re: How to insert a graphic into a document Reply with quote

DannyB wrote:
In a Writer document....

  • I don't know. But there is a single draw page for the entire doc. Just call getDrawPage() on the doc. Then iterate over all shapes on that draw page and you've done the whole document.
  • Is it possible to have graphics in a Writer document that are not shapes on the draw page and would be covered by the previous procedure?


Thanks I got a working macro and was able to fix all my linked graphics. However before releasing it I have to update it because when I developed it I didn't know about the XTextGraphicObjectsSupplier interface. I used some tricks to check if a FrameShape is a graphics or not... Tricky Smile. But it works.
_________________
An OOo mascot designer
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


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

PostPosted: Sat Mar 19, 2005 9:48 am    Post subject: Reply with quote

Very cold. I look forward to seeing your macro.
_________________
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: Mon May 02, 2005 10:00 am    Post subject: Reply with quote

How to insert graphic with transparency....
http://www.oooforum.org/forum/viewtopic.phtml?t=19580
_________________
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 1, 2, 3  Next
Page 1 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