[Draw] Center an image on the page.

Creating Extension - 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 forum is not for asking questions about writing your own macros.

[Draw] Center an image on the page.

Postby Sébastien C » Wed Mar 07, 2018 3:42 pm

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   Expand viewCollapse view
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 5.x under GNU-Linux Mageia 5.0 and M$-W XP
User avatar
Sébastien C
 
Posts: 57
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

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

Postby Zizi64 » Sun Mar 11, 2018 10:36 am

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; LO4.4.7, LO5.4.7 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.0.4 and AOO4.1.5
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.
User avatar
Zizi64
Volunteer
 
Posts: 6737
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary


Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest