[Solved] Inserting and positioning image

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
nienberg
Posts: 28
Joined: Mon Sep 21, 2009 9:23 pm
Location: Berkeley, CA

[Solved] Inserting and positioning image

Post by nienberg »

I've been able to insert an image into a Writer document using code like the following:

Code: Select all

dim args1(3) as new com.sun.star.beans.PropertyValue
args1(0).Name = "FileName"
args1(0).Value = "file://tesla/images/674.png"
args1(1).Name = "FilterName"
args1(1).Value = "<All formats>"
args1(2).Name = "AsLink"
args1(2).Value = false
args1(3).Name = "Style"
args1(3).Value = "Graphics"

dispatcher.executeDispatch(document, ".uno:InsertGraphic", "", 0, args1())
The image comes in with the anchor in the right place (at the beginning of the paragraph of my cursor location), but then I need to change the position so it is relative to the paragraph instead of the page and I need to set the Wrap to "through" and the options to "In background". Is there a way to set these properties before the insertion? Or is there a better way than the dispatcher to insert the image directly and then manipulate the properties?

I've used Xray to examine the properties of the images in the document (code below), but I don't see where these values are set.

Code: Select all

oDoc = ThisComponent
DrawPage = oDoc.DrawPage

For i = 0 To DrawPage.Count - 1 
	DrawPageObj = DrawPage.getByIndex(i)
	xray DrawPageObj
next i
Thanks,
Last edited by nienberg on Thu Dec 10, 2009 10:04 pm, edited 1 time in total.
LibreOffice 3.4.3
Windows XP, Windows 7, and MacOS X 10.7
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Inserting and positioning image

Post by rudolfo »

I have tried to do something similar because I had several screenshots that I had to integrate into a document and after 2 hours I couldn't stand anymore the cazillions of mouse clicks that were required to get this done.

For the wrapping settings I suggest to define a new frame style with the appropriate settings (derive it from the default "Graphics" style). Keep this style in your document or in your default template. And let's say we named it "AdjustedGraphics". Then change your macro to:

Code: Select all

dim args1(3) as new com.sun.star.beans.PropertyValue
 ... (snip) ....
args1(3).Name = "Style"
args1(3).Value = "AdjustedGraphics"

dispatcher.executeDispatch(document, ".uno:InsertGraphic", "", 0, args1())

dispatcher.executeDispatch(document,  ".uno:SetAnchorToPara", "", 0, Array())
I haven't figured out how to specify the type of anchoring in the frame style (the typical 4 radio buttons that you have for the individual frames/pictures are not available on the "Type" screen of the style) hence I need the additional dispatcher step to change the anchoring.

BTW, if you are sure that you use the default style "Graphics" always for the same purpose you don't need the additional style "AdjustedGraphics", but you can directly modify the "Graphics" style instead.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
nienberg
Posts: 28
Joined: Mon Sep 21, 2009 9:23 pm
Location: Berkeley, CA

Re: Inserting and positioning image

Post by nienberg »

Thanks for your suggestion. I worked on it some more and ended up with this:

Code: Select all

sub addSigImage
	'insert the image of a signature
	dim oDoc as object, oDocFrame as object
	dim oDrawPage as object, oDrawPageObj as object
	dim dispatcher as object
	
	oDoc = ThisComponent	
	oDocFrame = oDoc.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://tesla/images/674.png"
	args1(1).Name = "FilterName"
	args1(1).Value = "<All formats>"
	args1(2).Name = "AsLink"
	args1(2).Value = false
	args1(3).Name = "Style"
	args1(3).Value = "TMsigFrame"
	dispatcher.executeDispatch(oDocFrame, ".uno:InsertGraphic", "", 0, args1())

	'operate on the last image inserted
	oDrawPage = oDoc.DrawPage
	oDrawPageObj = oDrawPage.getByIndex(oDrawPage.Count - 1)

	oDrawPageObj.AnchorType = 0  'zero is paragraph anchor
	oDrawPageObj.Opaque = False  'In Background
	oDrawPageObj.Surround = 1    'this and next seem to give Wrap Through
	oDrawPageObj.TextWrap = 1
end sub
It turns out that if you specify a "Style" that does not exist, it will be created. Then the properties of the DrawPage object can be set and they will override the properties of the frame style. This means that you don't have to create the frame style ahead of time and give it the correct properties. However, it is important that you do not modify the properties of the frame style manually after inserting the image. If you do, the image will inherit all of the frame properties and the image will jump to a new location!

It may also be possible to insert the image, then set some properties of the frame style, then set some properties of the image. I may try that too.

Thanks again for your help.
LibreOffice 3.4.3
Windows XP, Windows 7, and MacOS X 10.7
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: [Solved] Inserting and positioning image

Post by rudolfo »

Looks good what you have put together!
Allthough I think that you don't need the "TMsigFrame" style. I don't see where you make use of it, because later on you apply all modifications that you want to have to the single frame/image object, but not to the style object. I think i mentioned this in my previous post that you have no other choice for the anchor type, but all the other modifications can be applied to the style object.

When you work with OpenOffice you'd better learn to appreciate the beauty of styles. Free your mind! Your initial thinking might have been:
"Okay, I click the mouse to insert the image, then I click on it and change the wrapping of the surrounding text .. and I need to change the anchoring. That's a lot of things, I record this to a macro and replay it whenever I want."
But what you actually want to do is to change several attributes of an image in one go.
The alternative approach is to group these attributes together (this is done only once) and later on apply this attribute group in one go to the image. "Grouping attributes together" is just a more abstract way to say "create a re-usable style".
I was always a bit annoyed by the fact that I had to click 4 or 5 times to execute a macro if I used the menus. Only if you assign a key to a macro it is a time saver. And too many macros that I use regularly to remember all their keystrokes.
So my guideline is: "Do as much as possible with styles, macros are only the last ressort"
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
Post Reply