OO-Snippets: Add Page Number

Commons

Keywordspage number, header
LanguageooRexx
ApplicationWriter
Authors
Supported Versions2.0.x  
Supported OSAll  
QuestionHow can I add the current page number and total page number to the header?
Answer

Code-Snippet-Listing (snippet-source)

/* AddPageNumbering.rex */
/* Macro */

/* get the script context, the XModel and the XTextDocument interface */
xScriptContext=uno.getScriptContext()
oDoc=xScriptContext~getDocument
xTextDoc=oDoc~XTextDocument

/* access the PageStyles using XMultiServiceFactory */
xServiceManager=oDoc~XMultiServiceFactory
xPageStyle=xServiceManager~createInstance("com.sun.star.style.PageStyle")
xFamiliesSupplier = xTextDoc~XStyleFamiliesSupplier
xStyle=xFamiliesSupplier~getStyleFamilies~getByName("PageStyles")~XNameContainer

/* set the Header up */
xHeader=xStyle~getByName("Standard")
HeaderProperty=xHeader~XPropertySet
HeaderProperty~setPropertyValue("HeaderIsOn", box("boolean", .true))
headerText=HeaderProperty~getPropertyValue("HeaderText")~XText

/* Creating the total number of pages */
pageCount=xServiceManager~createInstance("com.sun.star.text.TextField.PageCount")
pageCountTC=pageCount~XTextContent()
pageCountPS=pageCount~XPropertySet()
pageCountPS~setPropertyValue("NumberingType", box("Short", bsf.getConstant("com.sun.star.style.NumberingType", "ARABIC")))

/* Creating the actual page number */
pageNumber=xServiceManager~createInstance("com.sun.star.text.TextField.PageNumber")
pageNumberTC=pageNumber~XTextContent()
pageNumberPS=pageNumber~XPropertySet()
pageNumberPS~setPropertyValue("NumberingType", box("Short", bsf.getConstant("com.sun.star.style.NumberingType", "ARABIC")))
pageNumberPS~setPropertyValue("SubType", box("Short", "1"))

/* Insert text in the header */
headerText~setString("Page ")
headerText~insertTextContent(headerText~getEnd, pageNumberTC, .false)
headerText~getEnd~setString(" of ")
headerText~insertTextContent(headerText~getEnd, pageCountTC, .false)

::requires UNO.CLS

Changelog

DateUserModification
2006-07-09Initial version

and