Text selection API and events in Blazor PDF Viewer

17 Jul 20264 minutes to read

This document provides the reference details for text selection APIs and events in the Blazor PDF Viewer. It includes the available programmatic methods and event callbacks that allow applications to react to selection behavior.

Methods

SelectTextRegionAsync

The SelectTextRegionAsync method programmatically selects text within a specified page and bounds. Use this method to highlight specific text regions based on user interactions, search results, or application logic.

The following example illustrates how to handle the SelectTextRegionAsync method.

@using Syncfusion.Blazor.SfPdfViewer
@using Syncfusion.Blazor.Buttons

<SfButton OnClick="SelectTextRegion">SelectText Region</SfButton>
<SfPdfViewer2 Width="100%"
              Height="100%"
              DocumentPath="@DocumentPath"
              @ref="@Viewer" />

@code {
    private SfPdfViewer2? Viewer;
    private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

    private async Task SelectTextRegion()
    {
        List<Bound> bounds = new List<Bound>() {
            new Bound() {              
                X = 349.312,
                Y = 372.32,
                Height = 32.3104,
                Width = 100
            }
        };
        if(Viewer != null)
            await Viewer.SelectTextRegionAsync(2, bounds);
    }
}

ClearTextSelectionAsync

The ClearTextSelectionAsync method clears all text selection in the PDF document. It clears any highlighted or selected text regions and resets the selection state. Use this method to reset the UI when users start a new operation or when clearing filtered results.

The following example illustrates how to handle the ClearTextSelectionAsync method.

@using Syncfusion.Blazor.SfPdfViewer
@using Syncfusion.Blazor.Buttons

<SfButton OnClick="ClearTextSelection">Clear Text Selection</SfButton>
<SfPdfViewer2 Width="100%"
              Height="100%"
              DocumentPath="@DocumentPath"
              @ref="@Viewer" />

@code {
    private SfPdfViewer2? Viewer;
    private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

    private async Task ClearTextSelection()
    {
        if(Viewer != null)
            await Viewer.ClearTextSelectionAsync();
    }
}

Events

OnTextSelectionStart

The OnTextSelectionStart event is triggered when the user begins selecting text. Use this event to perform actions when text selection starts, such as logging, updating UI elements, or starting data collection.

Event arguments: TextSelectionStartEventArgs exposes:

  • PageNumber (int) – The page where the selection started (1-based indexing).

The following example illustrates how to handle the OnTextSelectionStart event.

@using Syncfusion.Blazor.SfPdfViewer

<SfPdfViewer2 Width="100%"
              Height="100%"
              DocumentPath="@DocumentPath">
    <PdfViewerEvents OnTextSelectionStart="OnTextSelectionStart"></PdfViewerEvents>
</SfPdfViewer2>

@code {
    private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

    private void OnTextSelectionStart(TextSelectionStartEventArgs args)
    {
        int pageNumber = args.PageNumber;
        Console.WriteLine($"Text selection started on page: {pageNumber}");
    }
}

OnTextSelectionEnd

The OnTextSelectionEnd event is triggered when the selection operation completes. Use this event to capture and process selected text, update UI elements, or perform operations based on the selection bounds and content.

Event arguments: TextSelectionEndEventArgs exposes:

  • PageNumber (int) – The page where the selection ended (1-based indexing).
  • TextBounds (List<TextBound>) – The bounds of the selected text in the page.
  • TextContent (string) – The text content selected in the page.

The following example illustrates how to handle the OnTextSelectionEnd event.

@using Syncfusion.Blazor.SfPdfViewer

<SfPdfViewer2 Width="100%"
              Height="100%"
              DocumentPath="@DocumentPath">
    <PdfViewerEvents OnTextSelectionEnd="OnTextSelectionEnd"></PdfViewerEvents>
</SfPdfViewer2>

@code {
    private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

    private void OnTextSelectionEnd(TextSelectionEndEventArgs args)
    {
        string textContent = args.TextContent;
        List<TextBound> textBounds = args.TextBounds;
    }
}

See also