Page 1 of 1

[Draw] Center an image on the page.

Posted: Wed Mar 07, 2018 3:42 pm
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

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

Posted: Sun Mar 11, 2018 10:36 am
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.