searchText method

PdfTextSearchResult searchText(
  1. String searchText, {
  2. TextSearchOption? searchOption,
})

Searches the given text in the document.

This method returns the PdfTextSearchResult object using which the search navigation can be performed on the instances found.

On mobile and desktop platforms, the search will be performed asynchronously and so the results will be returned periodically on a page-by-page basis, which can be retrieved using the PdfTextSearchResult.addListener method in the application.

Whereas in the web platform, the search will be performed synchronously and so the result will be returned only after completing the search on all the pages. This is since isolate is not supported for the web platform yet.

  • searchText - required - The text to be searched in the document.
  • searchOption - optional - Defines the constants that specify the option for text search.

This example demonstrates how to search text in SfPdfViewer.

class MyAppState extends State<MyApp> {

  late PdfViewerController _pdfViewerController;
  late PdfTextSearchResult _searchResult;

  @override
  void initState() {
    _pdfViewerController = PdfViewerController();
    _searchResult = PdfTextSearchResult();
    super.initState();
  }

  void _showDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Search Result'),
          content: const Text(
              'No more occurrences found. Would you like to continue to search from the beginning?'),
          actions: <Widget>[
            TextButton(
              onPressed: () {
                _searchResult.nextInstance();
                Navigator.of(context).pop();
              },
              child: const Text('YES'),
            ),
            TextButton(
              onPressed: () {
                _searchResult.clear();
                Navigator.of(context).pop();
              },
              child: const Text('NO'),
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: const Text('Syncfusion Flutter PdfViewer'),
              actions: <Widget>[
                IconButton(
                    icon: const Icon(
                      Icons.search,
                      color: Colors.white,
                    ),
                    onPressed: () {
                      _searchResult = _pdfViewerController.searchText('the',
                          searchOption: TextSearchOption.caseSensitive);
                     if (kIsWeb) {
                        setState(() {});
                      } else {
                        _searchResult.addListener(() {
                          if (_searchResult.hasResult) {
                           setState(() {});
                          }
                        });
                      }
                    }),
                Visibility(
                  visible: _searchResult.hasResult,
                  child: IconButton(
                    icon: const Icon(
                      Icons.clear,
                      color: Colors.white,
                    ),
                    onPressed: () {
                      setState(() {
                        _searchResult.clear();
                      });
                    },
                  ),
                ),
                Visibility(
                  visible: _searchResult.hasResult,
                  child: IconButton(
                    icon: const Icon(
                      Icons.navigate_before,
                      color: Colors.white,
                    ),
                    onPressed: () {
                      _searchResult.previousInstance();
                    },
                  ),
                ),
                Visibility(
                  visible: _searchResult.hasResult,
                  child: IconButton(
                   icon: const Icon(
                      Icons.navigate_next,
                     color: Colors.white,
                    ),
                   onPressed: () {
                      if ((_searchResult.currentInstanceIndex ==
                                  _searchResult.totalInstanceCount &&
                              kIsWeb) ||
                          (_searchResult.currentInstanceIndex ==
                                  _searchResult.totalInstanceCount &&
                              _searchResult.isSearchCompleted)) {
                        _showDialog(context);
                      } else {
                        _searchResult.nextInstance();
                      }
                    },
                  ),
                ),
              ],
            ),
            body: SfPdfViewer.network(
              'https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf',
              controller: _pdfViewerController,
              currentSearchTextHighlightColor: Colors.blue,
              otherSearchTextHighlightColor: Colors.yellow,
            )));
  }
}
'''

Implementation

PdfTextSearchResult searchText(String searchText,
    {TextSearchOption? searchOption}) {
  _searchText = searchText;
  _textSearchOption = searchOption;
  _notifyPropertyChangedListeners(property: 'searchText');
  return _pdfTextSearchResult;
}