How to access an image within a TextFrame

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
roland65
Posts: 32
Joined: Tue Oct 18, 2011 6:41 pm

How to access an image within a TextFrame

Post by roland65 »

Hi,
in a Writer document, I've written a macro that inserts an image in a TextFrame and adds a caption. Within the macro, I have access to the TextFrame object and I can manipulate it.
Now, I'd like to resize the image within the TextFrame (but not the size of the frame itself). I didn't find any way to do this because I don't know how to access the image within the TextFrame object.
I'll be grateful, if someone could point me in the right direction to solve this issue...

Here is my rather simple code:

Code: Select all

sub InsertFigure

Dim document   as object
Dim dispatcher as object

document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

dim args1(3) as new com.sun.star.beans.PropertyValue
args1(0).Name = "FileName"
args1(0).Value = "file://" & environ("HOME") & "/figure.png"
args1(1).Name = "FilterName"
args1(1).Value = "<Tous les formats>"
args1(2).Name = "AsLink"
args1(2).Value = false
args1(3).Name = "Style"
args1(3).Value = "Images"

' Insert image and anchor it to the page
dispatcher.executeDispatch(document, ".uno:InsertGraphic", "", 0, args1())
dispatcher.executeDispatch(document, ".uno:SetAnchorToPage", "", 0, Array())

Dim oDocCtrl as Object
oDocCtrl = ThisComponent.getCurrentController()

' Select frame and center it horizontally
' at 4 cm from the top
Dim oSelection as Object
oSelection = oDocCtrl.getSelection()
oSelection.TextWrap = 0
oSelection.HoriOrient = 2
oSelection.VertOrient = 0
oSelection.VertOrientPosition = 4000

end sub
LibreOffice 7.5.x on Ubuntu
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to access an image within a TextFrame

Post by FJCC »

The text frame and image are both on the Draw Page of the Writer document. I made a document with one text frame with one image in it. I can access those like this.

Code: Select all

oDP = ThisComponent.DrawPage
For i = 0 to oDP.Count - 1
	oObj = oDP.getByIndex(i)
	print oObj.ImplementationName
next i
The print statement gets run twice. When oObj is the text frame, the print statement shows SwXTextFrame. On the next iteration of the loop, oObj is the image and the print statement shows SwXTextGraphicObject.

If you have many text frames and images, identifying the right one may not be easy.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
roland65
Posts: 32
Joined: Tue Oct 18, 2011 6:41 pm

Re: How to access an image within a TextFrame

Post by roland65 »

OK, I'll try this.
Thanks a lot for the quick answer !
LibreOffice 7.5.x on Ubuntu
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

Re: How to access an image within a TextFrame

Post by musikai »

Your code above only inserts an image. There is no Textframe.
To change size of your image you can directly set e.g.:

Code: Select all

factor=0.5
oSelection.width=factor*oSelection.width
oSelection.Height=factor*oSelection.Height
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
Post Reply