searchText method

Future<List<CPDFTextRange>> searchText(
  1. String keywords, {
  2. dynamic searchOptions = CPDFSearchOptions.caseInsensitive,
})

Searches for text in the PDF document. This method allows you to search for specific keywords in the PDF document and returns a list of text ranges where the keywords are found.

You can specify search options such as case sensitivity and whether to match whole words.

final searcher = cpdfDocument.getTextSearcher();
List<CPDFTextRange> results = await searcher.searchText(
  'keywords', searchOptions: CPDFSearchOptions.caseInsensitive);

selection can be made on the search results using the selection() method.

Implementation

Future<List<CPDFTextRange>> searchText(String keywords,
    {searchOptions = CPDFSearchOptions.caseInsensitive}) async {
  _searchResults.clear();
  final rawList = await _channel.invokeMethod('search_text',
      {'keywords': keywords, 'search_options': searchOptions.nativeValue});
  if (rawList is! List) return [];

  try {
    final mappedResults = rawList
        .cast<Map>()
        .map((raw) => Map<String, dynamic>.from(raw))
        .map(CPDFTextRange.fromJson)
        .toList();

    _searchResults.addAll(mappedResults);
  } catch (e) {
    debugPrint('Failed to parse search result: $e');
  }
  return searchResults;
}