Page 1 of 1

Finding the current page number of a Writer document.

PostPosted: Sun Nov 03, 2013 9:26 am
by _savage
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!

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

PostPosted: Sun Nov 03, 2013 10:12 am
by F3K Total
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

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

PostPosted: Sun Nov 03, 2013 10:33 pm
by _savage
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()