java-打开Office Writer搜索并查找字符串和移动光标
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-打开Office Writer搜索并查找字符串和移动光标,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2768字,纯文字阅读大概需要4分钟。
内容图文
![java-打开Office Writer搜索并查找字符串和移动光标](/upload/InfoBanner/zyjiaocheng/676/f7704ded423c47489887db38e71c5a04.jpg)
如果任何人都可以使用使用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();
}
内容总结
以上是互联网集市为您收集整理的java-打开Office Writer搜索并查找字符串和移动光标全部内容,希望文章能够帮你解决java-打开Office Writer搜索并查找字符串和移动光标所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。