|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Text search Events in Blazor PDF Viewer control | Syncfusion |
| 4 | +description: Learn how to handle text search events, and run programmatic searches in the Syncfusion Blazor PDF Viewer. |
| 5 | +platform: document-processing |
| 6 | +control: Text search |
| 7 | +documentation: ug |
| 8 | +domainurl: ##DomainURL## |
| 9 | +--- |
| 10 | + |
| 11 | +# Text Search Events in Blazor PDF Viewer |
| 12 | + |
| 13 | +The Blazor PDF Viewer triggers events during text search operations, allowing you to customize behavior and respond to different stages of the search process. |
| 14 | + |
| 15 | +## OnTextSearchStart |
| 16 | + |
| 17 | +The [OnTextSearchStart](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.SfPdfViewer.PdfViewerEvents.html#Syncfusion_Blazor_SfPdfViewer_PdfViewerEvents_OnTextSearchStart) event fires as soon as a search begins from the toolbar interface or through the [`SearchTextAsync`](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.SfPdfViewer.SfPdfViewer2.html#Syncfusion_Blazor_SfPdfViewer_SfPdfViewer2_SearchTextAsync_System_String_System_Boolean_) method. Use to reset UI state, log analytics, or cancel the default search flow before results are processed. |
| 18 | + |
| 19 | +- Event arguments: [TextSearchStartEventArgs](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.SfPdfViewer.TextSearchStartEventArgs.html) exposes: |
| 20 | + - `SearchText`: the term being searched. |
| 21 | + - `MatchCase`: indicates whether case-sensitive search is enabled. |
| 22 | + |
| 23 | +{% tabs %} |
| 24 | +{% highlight razor %} |
| 25 | +@using Syncfusion.Blazor.SfPdfViewer |
| 26 | +<SfPdfViewer2 DocumentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" |
| 27 | + Height="100%" |
| 28 | + Width="100%"> |
| 29 | + <PdfViewerEvents OnTextSearchStart="OnTextSearchStart"></PdfViewerEvents> |
| 30 | +</SfPdfViewer2> |
| 31 | + |
| 32 | +@code { |
| 33 | + private void OnTextSearchStart(TextSearchStartEventArgs args) |
| 34 | + { |
| 35 | + Console.WriteLine($"Text search started for: \"{args.SearchText}\""); |
| 36 | + } |
| 37 | +} |
| 38 | +{% endhighlight %} |
| 39 | +{% endtabs %} |
| 40 | + |
| 41 | +## OnTextSearchHighlight |
| 42 | + |
| 43 | +The [OnTextSearchHighlight](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.SfPdfViewer.PdfViewerEvents.html#Syncfusion_Blazor_SfPdfViewer_PdfViewerEvents_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. |
| 44 | + |
| 45 | +- Event arguments: [TextSearchHighlightEventArgs](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.SfPdfViewer.TextSearchHighlightEventArgs.html) exposes: |
| 46 | + - `Bound`: represents the highlighted match position and dimensions. |
| 47 | + - `PageNumber`: page index where the match is highlighted. |
| 48 | + |
| 49 | +{% tabs %} |
| 50 | +{% highlight razor %} |
| 51 | +@using Syncfusion.Blazor.SfPdfViewer |
| 52 | +<SfPdfViewer2 DocumentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" |
| 53 | + Height="100%" |
| 54 | + Width="100%"> |
| 55 | + <PdfViewerEvents OnTextSearchHighlight="OnTextSearchHighlight"></PdfViewerEvents> |
| 56 | +</SfPdfViewer2> |
| 57 | + |
| 58 | +@code { |
| 59 | + private void OnTextSearchHighlight(TextSearchHighlightEventArgs args) |
| 60 | + { |
| 61 | + Console.WriteLine($"Highlighted match at page {args.PageNumber}, bounds: {args.Bound}"); |
| 62 | + } |
| 63 | +} |
| 64 | +{% endhighlight %} |
| 65 | +{% endtabs %} |
| 66 | + |
| 67 | +## OnTextSearchComplete |
| 68 | + |
| 69 | +The [OnTextSearchComplete](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.SfPdfViewer.PdfViewerEvents.html#Syncfusion_Blazor_SfPdfViewer_PdfViewerEvents_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. |
| 70 | + |
| 71 | +- **Typical uses**: |
| 72 | + - Update UI with the total number of matches and enable navigation controls. |
| 73 | + - Hide loading indicators or show a "no results" message if none were found. |
| 74 | + - Record analytics for search effectiveness. |
| 75 | +- **Event arguments**: [TextSearchCompleteEventArgs](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.SfPdfViewer.TextSearchCompleteEventArgs.html) exposes: |
| 76 | + - `SearchText`: the searched term. |
| 77 | + - `MatchCase`: indicates whether case-sensitive search was used. |
| 78 | + |
| 79 | +{% tabs %} |
| 80 | +{% highlight razor %} |
| 81 | +@using Syncfusion.Blazor.SfPdfViewer |
| 82 | +<SfPdfViewer2 DocumentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" |
| 83 | + Height="100%" |
| 84 | + Width="100%"> |
| 85 | + <PdfViewerEvents OnTextSearchComplete="OnTextSearchComplete"></PdfViewerEvents> |
| 86 | +</SfPdfViewer2> |
| 87 | + |
| 88 | +@code { |
| 89 | + private void OnTextSearchComplete(TextSearchCompleteEventArgs args) |
| 90 | + { |
| 91 | + Console.WriteLine($"Text search completed for: \"{args.SearchText}\""); |
| 92 | + } |
| 93 | +} |
| 94 | +{% endhighlight %} |
| 95 | +{% endtabs %} |
| 96 | + |
| 97 | +## See Also |
| 98 | + |
| 99 | +- [Text Search Features](./text-search-features) |
| 100 | +- [Text Selection](../text-selection/overview) |
| 101 | +- [Extract and Highlight Text in Blazor PDF Viewer Component](../faqs/how-to-extract-particular-text-and-highlight) |
0 commit comments