Search code examples
javaopenoffice.orgopenoffice-writer

getPageNumber Open Office


I have the below code which returns the total number of pages in my document. I can call this with javascript soapplet.getNumberOfPages()

I want to do a similar method that will return just the PageNumber. I have found this class com.sun.star.text.TextField.PageNumber but I cannot figure out how to return a string of the PageNumber. In javascript then I will be able to search the document for a certain string and depending where the string is found I will be able to determine the page number on that document where string exists. I can then tell the printer to print this page from the headed paper tray. I've googled for a day or two and can't find anything. Thanks

public int getNumberOfPages()
{
    XController xController = OODocument.getCurrentDocument().getXFrame().getController();

    XTextViewCursorSupplier supTextViewCursor =
                (XTextViewCursorSupplier) UnoRuntime.queryInterface(
                    XTextViewCursorSupplier.class, xController);

    XTextViewCursor curTextView = supTextViewCursor.getViewCursor();
    XPageCursor curPage =
                (XPageCursor) UnoRuntime.queryInterface(
                    XPageCursor.class, curTextView);
    curPage.jumpToLastPage();
    System.out.println("pages = " + curPage.getPage());
    return curPage.getPage();
}

I'm rethinking this now and wondering if its possible in javascript to find the string and then to return the page number that string is on. code I have is:

if(printOnPlainPaper == true)    {

            if(soApplet.isConnected())  
            {
                soDoc = soDoc + soApplet.getEncodedHtmlDocument( null );
                if(soDoc.indexOf("zzzzz"))  
                {                                       
                    alert("trueeeee");
                }
                else 
                {
                    soApplet.printDocument(PLAIN_PAPER, "1-");    // print page 1 to plain paper printer
                }
            }
        //soApplet.printDocument(PLAIN_PAPER, "1-");    // print page 1 to plain paper printer
    }

It can find the string but then how do I get it to tell me the page number its on?

Thanks


Solution

  • Incase anyone needs it I worked it out. Code below searches document , finds string and returns page number its on

    public int searchPageNumber()
    {
        XController xController = OODocument.getCurrentDocument().getXFrame().getController();
    
        XTextViewCursorSupplier supTextViewCursor =
                    (XTextViewCursorSupplier) UnoRuntime.queryInterface(
                        XTextViewCursorSupplier.class, xController);
    
        XTextViewCursor curTextView = supTextViewCursor.getViewCursor();
    
        // gets the page cursor and assigns the text view cursor to the page
        XPageCursor curPage =
                    (XPageCursor) UnoRuntime.queryInterface(
                        XPageCursor.class, curTextView);
        System.out.println("The current page number is " + curPage.getPage());
    
        // gets the model
        XModel model = xController.getModel();
        // assigns model to the document
        XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, model);
        // Xsearchable so we can search the text
        XSearchable xSearchable = (XSearchable) UnoRuntime.queryInterface(XSearchable.class, xTextDocument); 
        XSearchDescriptor xsd = (XSearchDescriptor) xSearchable.createSearchDescriptor(); 
    
        xsd.setSearchString("zzzzz");
    
        XInterface xfi = (XInterface) xSearchable.findFirst(xsd); 
        if (xfi != null) { 
            XTextRange xStart = (com.sun.star.text.XTextRange) UnoRuntime.queryInterface( 
                    com.sun.star.text.XTextRange.class, xfi); 
    
            curTextView.gotoRange(xStart, false); 
        } 
    
        System.out.println("current page = " + curPage.getPage());
        return curPage.getPage();
    }