Draw: create custom pagenumbers

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: create custom pagenumbers

Postby musikai » Thu Sep 19, 2019 1:21 pm

In Draw pagenumbers only can start with 1.
Here is a workaround if you want to start with another number.

If it doesn't exist this macro creates a textshape called "pagenumber" on the Bottom Center of the 1st page with a number "1" inside and creates and assigns a Style called "pagenumbers".
Then it will duplicate the object on all subsequent pages at the same position, Size and Style and increase the number by 1.
You can change the Position, Size, number of the object on the 1st page and just run the macro again to update all other pages.
Other properties can immediately be changed by editing the Style "pagenumbers".

Code: Select all   Expand viewCollapse view
sub create_custom_Draw_pagenumbers
if not ThisComponent.StyleFamilies.getbyname("graphics").hasbyname("pagenumbers") then
oStyle = ThisComponent.StyleFamilies.getbyname("graphics").createInstance("com.sun.star.style.StyleFamily")
oStyle.ParaAdjust = 3
oStyle.FillStyle = com.sun.star.drawing.FillStyle.NONE
oStyle.LineStyle = com.sun.star.drawing.LineStyle.NONE
end if

for ig =0 to oDrawpage.count-1
if OriginalShape.name = "pagenumber" then exit for

if ig=oDrawpage.count then
OriginalShape = Thiscomponent.createInstance("com.sun.star.drawing.TextShape")
    With OriginalShape
      .Position = CreatePoint(oDrawpage.Width/2-500,oDrawpage.Height-oDrawpage.BorderBottom-1000)
      .Size = CreateSize(1000,1000)   
      .Name = "pagenumber"
    End With
    OriginalShape.Style = ThisComponent.StyleFamilies.getbyname("graphics").getbyname("text")
   OriginalShape.Style = ThisComponent.StyleFamilies.getbyname("graphics").getbyname("pagenumbers")
   OriginalShape.ZOrder = oDrawpage.count-1
end if

for i=1 to oDrawpages.count-1

for ig =oDrawpage.count-1 to 0 step -1
if existingTextShape.name = "pagenumber" then  oDrawpage.remove(existingTextShape)

oTextShape = Thiscomponent.createInstance("com.sun.star.drawing.TextShape")
    With oTextShape
      .Position = OriginalShape.Position
      .Size = OriginalShape.Size     
      .Name = "pagenumber"
    End With
    oTextShape.Style = ThisComponent.StyleFamilies.getbyname("graphics").getbyname("text")
   oTextShape.Style = OriginalShape.Style
   oTextShape.ZOrder = oDrawpage.count-1
   oTextShape.SizeProtect = True
   oTextShape.MoveProtect = True

end sub

Function CreatePoint(ByVal x As Long,ByVal y As Long) As com.sun.star.awt.Point
  Dim oPoint
  oPoint = CreateUnoStruct("com.sun.star.awt.Point")
  oPoint.X = x : oPoint.Y = y
  CreatePoint = oPoint
End Function

Function CreateSize(ByVal x As Long,ByVal y As Long) As com.sun.star.awt.Size
  Dim oSize
  oSize = CreateUnoStruct("com.sun.star.awt.Size")
  oSize.Width = x : oSize.Height = y
  CreateSize = oSize
End Function
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
Posts: 282
Joined: Wed Nov 11, 2015 12:19 am

Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest