编程语言
首页 > 编程语言> > java-打开Office Writer搜索并查找字符串和移动光标

java-打开Office Writer搜索并查找字符串和移动光标

作者:互联网

如果任何人都可以使用使用Java的UNO API来协助在开放式办公室作者文档中搜索字符串的方法,那将非常有帮助.一旦它搜索了字符串并找到了字符串,它就可以(甚至隐藏)将光标移动到该字符串.

然后,我可以使用下面的getPageNumber方法返回页码并在页眉纸上打印该页.

任何帮助,不胜感激

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

public int getPageNumber()
{
    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);

    System.out.println("current page = " + curPage.getPage());
    return curPage.getPage();
}

我知道可以结合使用以下内容来完成

    curTextView.setString("zzzzz");
    curTextView.getText();
    curTextView.gotoRange(arg0, arg1)

或XTextRange.

谢谢

解决方法:

万一有人需要答案.我解决了问题并在下面发布.

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

标签:search,api,openoffice-writer,uno,java
来源: https://codeday.me/bug/20191031/1978454.html