Page 1 of 1

Draw: create custom pagenumbers

PostPosted: Thu Sep 19, 2019 1:21 pm
by musikai
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
ThisComponent.StyleFamilies.getbyname("graphics").insertbyname("pagenumbers",oStyle)
end if

oDrawpages=ThisComponent.getDrawPages()
oDrawpage=oDrawpages.getbyindex(0)
for ig =0 to oDrawpage.count-1
OriginalShape=oDrawpage.getbyindex(ig)
if OriginalShape.name = "pagenumber" then exit for
next

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
    oDrawpage.add(OriginalShape)
    OriginalShape.string="1"
    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
oDrawpage=oDrawpages.getbyindex(i)

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

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

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