[Draw] Center an image on the page.

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

[Draw] Center an image on the page.

Post by Sébastien C »

Hello to all

I have so much pictures to put in Draw’s files and center on each pages.
With LibreOffice, I open a new Draw page and drag and drop my picture first. This did, I right click on the picture and choose "Align", then "Center Horizontal". Then I repeat right click on the picture and choose "Align", then "Vertical Center". How long is that!!!
For accelerate this, I writed a macro and put a choice in my image context menu I called "Center image". A dream...

Code: Select all

Sub centerDrawObjet
 Dim      myPage As Object, myImage As Object
 Dim newPosition As New com.sun.star.awt.Point
  
 If isNull(thisComponent.currentSelection) Then
  msgBox "Maybe a selection can help...", 16, "Select something!!!"
  Exit Sub
 End If

  myPage = thisComponent.masterPages.getByIndex(0)
 myImage = thisComponent.currentSelection.getByIndex(0)

 newPosition.x = int((myPage.width  - myImage.size.width)  / 2)
 newPosition.y = int((myPage.height - myImage.size.height) / 2)

 myImage.position = newPosition
End Sub
:D
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Draw] Center an image on the page.

Post by Zizi64 »

Hi, Sébastien,

Just a note:
The built-in Center functions will center the objects on the drawing area inside of the margins.
Your macro will center the object on the whole page area, but not on the area inside the margins.

The different methods will give you same result, when you use symmetrical Top/Bottom and Left/Right margin values on the page.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Post Reply