Text selection API and events in Blazor PDF Viewer

12 Jun 20264 minutes to read

This document provides the reference details for text selection APIs and events in the Syncfusion Blazor PDF Viewer. It includes the available configuration property, 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 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 {
    SfPdfViewer2? Viewer;
    public string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

    public 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 event clears all text selection in the PDF document. Removes any highlighted or selected text regions and resets the selection state. Use 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 {
    SfPdfViewer2? Viewer;
    public string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

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

Events

OnTextSelectionStart

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

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 {
    public 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 OnTextSlectionEnd event is triggered when the selection operation completes. Use to capture and process selected text, update UI elements, or perform operations based on the selection bounds and content.

  • Event arguments: TextSelectionEndEventArgs exposes:
  • TextBounds - Defines the bounds of the selected text in the page.
  • TextContent - Defines 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 {
    public 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