Text Search Events in Blazor PDF Viewer

10 Jun 20263 minutes to read

The Blazor PDF Viewer triggers events during text search operations, allowing you to customize behavior and respond to different stages of the search process.

OnTextSearchStart

The OnTextSearchStart event fires as soon as a search begins from the toolbar interface or through the SearchTextAsync method. Use to reset UI state, log analytics, or cancel the default search flow before results are processed.

  • Event arguments: TextSearchStartEventArgs exposes:
    • SearchText: the term being searched.
    • MatchCase: indicates whether case-sensitive search is enabled.
@using Syncfusion.Blazor.SfPdfViewer
<SfPdfViewer2 DocumentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"
             Height="100%"
             Width="100%">
    <PdfViewerEvents OnTextSearchStart="OnTextSearchStart"></PdfViewerEvents>
</SfPdfViewer2>

@code {
    private void OnTextSearchStart(TextSearchStartEventArgs args)
    {
        Console.WriteLine($"Text search started for: \"{args.SearchText}\"");
    }
}

OnTextSearchHighlight

The OnTextSearchHighlight event triggers whenever a search result is brought into view, including navigation between matches. Use to draw custom overlays or synchronize adjacent UI elements when a match is highlighted.

  • Event arguments: TextSearchHighlightEventArgs exposes:
    • Bound: represents the highlighted match position and dimensions.
    • PageNumber: page index where the match is highlighted.
@using Syncfusion.Blazor.SfPdfViewer
<SfPdfViewer2 DocumentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"
             Height="100%"
             Width="100%">
    <PdfViewerEvents OnTextSearchHighlight="OnTextSearchHighlight"></PdfViewerEvents>
</SfPdfViewer2>

@code {
    private void OnTextSearchHighlight(TextSearchHighlightEventArgs args)
    {
        Console.WriteLine($"Highlighted match at page {args.PageNumber}, bounds: {args.Bound}");
    }
}

OnTextSearchComplete

The OnTextSearchComplete event runs after the search engine finishes scanning the document for the current query. Use to update match counts, toggle navigation controls, or notify users when no results were found.

  • Typical uses:
    • Update UI with the total number of matches and enable navigation controls.
    • Hide loading indicators or show a “no results” message if none were found.
    • Record analytics for search effectiveness.
  • Event arguments: TextSearchCompleteEventArgs exposes:
    • SearchText: the searched term.
    • MatchCase: indicates whether case-sensitive search was used.
@using Syncfusion.Blazor.SfPdfViewer
<SfPdfViewer2 DocumentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"
             Height="100%"
             Width="100%">
    <PdfViewerEvents OnTextSearchComplete="OnTextSearchComplete"></PdfViewerEvents>
</SfPdfViewer2>

@code {
    private void OnTextSearchComplete(TextSearchCompleteEventArgs args)
    {
        Console.WriteLine($"Text search completed for: \"{args.SearchText}\"");
    }
}

See Also