getText method
Retrieves a text snippet from the current PDF page based on the specified range.
This method calls the native side to extract a substring of text from the page,
starting at the given location
and reading length
number of characters.
Typically used with a CPDFTextRange, which can be adjusted using the expanded
method to include surrounding context for better readability in search results.
Parameters:
range
: A CPDFTextRange object that specifies the pageIndex, start location, and length.
Returns: A String containing the extracted text. Returns an empty string if no result is found.
Usage Example:
final searcher = cpdfDocument.getTextSearcher();
List<CPDFTextRange> results = await searcher.searchText(
'keywords', searchOptions: CPDFSearchOptions.caseInsensitive);
// then: select the first result
CPDFTextRange range = searcher.searchResults[0];
// With context (before and after)
final expandedRange = range.expanded(before: 20, after: 20);
final contextText = await page.getText(expandedRange);
print("Text with context: $contextText");
Implementation
Future<String> getText(CPDFTextRange range) async {
final result = await _channel.invokeMethod('get_search_text', {
'page_index': pageIndex,
'location': range.location,
'length': range.length,
});
return result ?? '';
}