Enable or disable text selection in Blazor PDF Viewer

12 Jun 20262 minutes to read

This guide explains how to enable or disable text selection in the Syncfusion Blazor PDF Viewer using both initialization-time settings and runtime toggling.

Disable text selection at initialization

Use the EnableTextSelection property during initialization to disable or enable text selection. The following example disables the text selection during initialization.

@using Syncfusion.Blazor.SfPdfViewer

<SfPdfViewer2 Width="100%"
              Height="100%"
              DocumentPath="@DocumentPath"
              EnableTextSelection="false" />

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

Toggle text selection at runtime

The EnableTextSelection property can be toggled at runtime using buttons to enable or disable text selection dynamically. The following example demonstrates how to toggle text selection using button click events while also updating the InteractionMode and reloading the document.

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

<SfButton OnClick="EnableTextSelections">Enable Text Selection</SfButton>
<SfButton OnClick="DisableTextSelections">Disable Text Selection</SfButton>
<SfPdfViewer2 Width="100%"
              Height="100%"
              DocumentPath="@DocumentPath"
              EnableTextSelection="@EnableTextSelection"
              InteractionMode="@InteractionMode"
              @ref="@Viewer" />

@code {
    SfPdfViewer2 Viewer;
    public Boolean EnableTextSelection=true;
    public InteractionMode InteractionMode=InteractionMode.TextSelection;
    public string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

    public void EnableTextSelections()
    {
        EnableTextSelection=true;
        InteractionMode=InteractionMode.TextSelection;
        Viewer.LoadAsync(DocumentPath,null);
    }

    public void DisableTextSelections()
    {
        EnableTextSelection=false;
         InteractionMode=InteractionMode.Pan;
        Viewer.LoadAsync(DocumentPath,null);
    }
}

Use cases and considerations

  • Document protection: Disable text selection to help prevent copying sensitive content.
  • Read-only documents: Provide a cleaner viewing experience by preventing selection.
  • Interactive apps: Toggle selection based on user roles or document states.

NOTE

Text selection is enabled by default. Set EnableTextSelection to false to disable it.

See also