0

我有以下代码,它返回文档中的总页数。我可以用 javascript soapplet.getNumberOfPages() 调用它

我想做一个类似的方法,只返回 PageNumber。我找到了这个类 com.sun.star.text.TextField.PageNumber 但我不知道如何返回 PageNumber 的字符串。在 javascript 中,我将能够在文档中搜索某个字符串,并且根据找到该字符串的位置,我将能够确定该文档上存在字符串的页码。然后我可以告诉打印机从带头纸盘打印此页。我用谷歌搜索了一两天,找不到任何东西。谢谢

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();
}

我现在正在重新考虑这一点,想知道是否可以在 javascript 中找到字符串,然后返回该字符串所在的页码。我的代码是:

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
    }

它可以找到字符串,但是我如何让它告诉我它的页码?

谢谢

4

1 回答 1

0

万一有人需要,我就解决了。下面的代码搜索文档,找到字符串并返回其所在的页码

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();
}
于 2012-10-15T09:03:38.333 回答