Finding the current page number of a Writer document.

Keyboard macros or custom scripts

Finding the current page number of a Writer document.

Postby _savage » Sun Nov 03, 2013 9:26 am

When iterating over the paragraphs and text portions of a Writer document, how can I get a hold of the current page and page number that a paragraph is on? Thanks!
Mac 10.14 using LO 6.2.5.2, Gentoo Linux using LO 6.2.4.2.0 headless.
_savage
 
Posts: 176
Joined: Sun Apr 21, 2013 12:55 am

Re: Finding the current page number of a Writer document.

Postby F3K Total » Sun Nov 03, 2013 10:12 am

Hi,
i found the page number in ViewCursor's properties, this should work:
Code: Select all   Expand viewCollapse view
Sub s_find_page
    ocontroller = thisComponent.currentController
    oViewCursor = oController.ViewCursor
    oViewCursor.gotoStart(false)
    oText = thisComponent.Text
    oTextEnumeration = oText.createEnumeration
    while oTextEnumeration.hasmoreelements
        oParagraph = oTextEnumeration.nextelement
        sText = oParagraph.String
        oViewCursor.gotoRange(oParagraph,false)
        msgbox ("Current paragraph:"+chr(13)+chr(13)+""""+sText+""""+chr(13)_
        +chr(13)+"... lies on Page " + oViewCursor.Page,0,"find page number")
    wend
End Sub

R
    MMove 1.0.6
  • Extension for easy, exact positioning of shapes, pictures, controls, frames ...
    my current system
  • Windows 10 AOO, LOLinux Mint AOO, LO
F3K Total
Volunteer
 
Posts: 977
Joined: Fri Dec 16, 2011 8:20 pm

Re: Finding the current page number of a Writer document.

Postby _savage » Sun Nov 03, 2013 10:33 pm

F3K Total wrote:Hi,
i found the page number in ViewCursor's properties, this should work: ...
R

Thanks F3K, that worked for me :super: Here's the Python code for future reference:

Code: Select all   Expand viewCollapse view
def main() :

    ...
    desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
    document = desktop.loadComponentFromURL("file://path/to/file.doc", "_blank", 0, ())
    ...

    def get_page_number(par) :
        """Given a paragraph object, return the page number that paragraph sits on."""
        controller = document.getCurrentController()
        cursor = controller.getViewCursor()
        cursor.gotoRange(par, False)
        return cursor.getPage()
Mac 10.14 using LO 6.2.5.2, Gentoo Linux using LO 6.2.4.2.0 headless.
_savage
 
Posts: 176
Joined: Sun Apr 21, 2013 12:55 am


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest