Draw: create custom pagenumbers

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
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

Draw: create custom pagenumbers

Post 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

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
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
Post Reply