|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Enable or Disable Text Selection in ASP.NET Core PDF Viewer | Syncfusion |
| 4 | +description: Learn how to enable text selection in Syncfusion ##Platform_Name## Pdfviewer component of Syncfusion Essential JS 2 and more. |
| 5 | +platform: ej2-asp-core-mvc |
| 6 | +control: PDF Viewer |
| 7 | +documentation: ug |
| 8 | +domainurl: ##DomainURL## |
| 9 | +--- |
| 10 | + |
| 11 | +# Enable or Disable Text Selection in Syncfusion PDF Viewer |
| 12 | + |
| 13 | +The Syncfusion PDF Viewer provides the `enableTextSelection` property, which allows you to control whether users can select text within the displayed PDF document. This feature can be toggled programmatically during runtime. |
| 14 | + |
| 15 | +## Configure Text Selection on Initialization |
| 16 | + |
| 17 | +You can set the initial text selection behavior when initializing the PDF Viewer control by configuring the `enableTextSelection` property. By default, text selection is enabled, but you can disable it as shown in the following example: |
| 18 | + |
| 19 | +{% tabs %} |
| 20 | +{% highlight cshtml tabtitle="Standalone" %} |
| 21 | + |
| 22 | +@page "{handler?}" |
| 23 | +@model IndexModel |
| 24 | +@{ |
| 25 | + ViewData["Title"] = "Home page"; |
| 26 | +} |
| 27 | + |
| 28 | +<div class="text-center"> |
| 29 | + <ejs-pdfviewer id="pdfviewer" |
| 30 | + style="height:600px" |
| 31 | + resourceUrl="https://cdn.syncfusion.com/ej2/30.1.37/dist/ej2-pdfviewer-lib" |
| 32 | + documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" |
| 33 | + enableTextSelection="false"> |
| 34 | + </ejs-pdfviewer> |
| 35 | +</div> |
| 36 | + |
| 37 | +{% endhighlight %} |
| 38 | +{% endtabs %} |
| 39 | + |
| 40 | +## Toggle Text Selection Dynamically |
| 41 | + |
| 42 | +You can change the text selection behavior at runtime using buttons, menu options, or other UI elements. The following example demonstrates how to toggle text selection with button clicks: |
| 43 | + |
| 44 | +{% tabs %} |
| 45 | +{% highlight cshtml tabtitle="Standalone" %} |
| 46 | + |
| 47 | +@page "{handler?}" |
| 48 | +@model IndexModel |
| 49 | +@{ |
| 50 | + ViewData["Title"] = "Home page"; |
| 51 | +} |
| 52 | + |
| 53 | +<div class="text-center"> |
| 54 | + <button onclick="enableTextSelection()">Enable Text Selection</button> |
| 55 | + <button onclick="disableTextSelection()">Disable Text Selection</button> |
| 56 | + <ejs-pdfviewer id="pdfviewer" |
| 57 | + style="height:600px" |
| 58 | + resourceUrl="https://cdn.syncfusion.com/ej2/30.1.37/dist/ej2-pdfviewer-lib" |
| 59 | + documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" |
| 60 | + enableTextSelection="false"> |
| 61 | + </ejs-pdfviewer> |
| 62 | +</div> |
| 63 | + |
| 64 | +<script type="text/javascript"> |
| 65 | + // You can dynamically toggle text selection with JavaScript |
| 66 | + function enableTextSelection() { |
| 67 | + var viewer = document.getElementById('pdfviewer').ej2_instances[0]; |
| 68 | + viewer.enableTextSelection = true; |
| 69 | + } |
| 70 | + |
| 71 | + function disableTextSelection() { |
| 72 | + var viewer = document.getElementById('pdfviewer').ej2_instances[0]; |
| 73 | + viewer.enableTextSelection = false; |
| 74 | + } |
| 75 | +</script> |
| 76 | + |
| 77 | +{% endhighlight %} |
| 78 | +{% endtabs %} |
| 79 | + |
| 80 | +## Use Cases and Considerations |
| 81 | + |
| 82 | +- **Document Protection**: Disabling text selection helps prevent unauthorized copying of sensitive content. |
| 83 | +- **Read-only Documents**: In scenarios where documents are meant for viewing only, disabling text selection can provide a cleaner user experience. |
| 84 | +- **Interactive Applications**: Toggle text selection based on user roles or document states in complex applications. |
| 85 | +- **Controlled Access**: Implement conditional text selection depending on user permissions or document sections. |
| 86 | + |
| 87 | +## Default Behavior |
| 88 | + |
| 89 | +By default, text selection is enabled in the PDF Viewer. Set the `enableTextSelection` property to `false` explicitly if you want to disable this functionality. |
| 90 | + |
| 91 | +N> When text selection is disabled, users will not be able to select or copy text from the document, which can be useful for protecting document content in certain scenarios. |
| 92 | + |
| 93 | +[View sample in GitHub](https://github.com/SyncfusionExamples/asp-core-pdf-viewer-examples/tree/master/How%20to) |
0 commit comments