List<TextRange> () { List<TextRange> searchResultsRanges = new ArrayList<>(); for (int i = 0; i < documentOfPagesModel.getPagesList().size(); i++) { Page page = documentOfPagesModel.getPagesList().get(i); List<SearchResult> searchResults = dataProvider.getSearchResultsInPage(page); if (searchResults != null) { for (SearchResult result : searchResults) { int from = documentOfPagesModel.getSymbolOffsetToStartOfPage( documentOfPagesModel.getIndexOfPageByPageNumber(result.startPosition.pageNumber)) + result.startPosition.symbolOffsetInPage; int to = documentOfPagesModel.getSymbolOffsetToStartOfPage( documentOfPagesModel.getIndexOfPageByPageNumber(result.endPostion.pageNumber)) + result.endPostion.symbolOffsetInPage; if (from < 0) from = 0; if (to > documentOfPagesModel.getDocument().getTextLength()) to = documentOfPagesModel.getDocument().getTextLength(); if (from < to) { searchResultsRanges.add(new TextRange(from, to)); } } } } return searchResultsRanges; }