|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Prevent Copy or Print in React PDF Viewer | Syncfusion |
| 4 | +description: Learn how to prevent printing and copying in the React PDF Viewer using viewer settings or server-side permission flags. |
| 5 | +platform: document-processing |
| 6 | +control: PDF Viewer |
| 7 | +documentation: ug |
| 8 | +domainurl: ##DomainURL## |
| 9 | +--- |
| 10 | + |
| 11 | +# Prevent Copy or Print in React PDF Viewer |
| 12 | + |
| 13 | +## Overview |
| 14 | +This guide shows how to prevent users from copying text or printing documents in EJ2 React PDF Viewer. |
| 15 | + |
| 16 | +**Outcome:** You will learn server-side and client-side options to restrict copy/print with a complete React example. |
| 17 | + |
| 18 | +## Steps |
| 19 | + |
| 20 | +1. Use a PDF with permissions already set |
| 21 | + - Load a PDF that already disallows copy or print functionality itself. The Viewer enforces these permission automatically. |
| 22 | + |
| 23 | +2. Preprocess restrictions in server-side |
| 24 | + - Use Syncfusion PDF Library to set permission flags before sending the file to the client. See the server-side example below. |
| 25 | + - Disabling print and copy in server-side automatically enforces them in the PDF Viewer. |
| 26 | + |
| 27 | +3. Hide/disable UI elements in the viewer |
| 28 | + - Print, download and copy options can be disabled or hidden in the viewer regardless of the PDF's permissions. |
| 29 | + - Print and download options can be hidden in the viewer's primary toolbar. See [primary toolbar customization](../toolbar-customization/primary-toolbar). |
| 30 | + - Copy option in the context menu can be disabled in the PDF Viewer. See [customize context menu](../context-menu/custom-context-menu). |
| 31 | + |
| 32 | +4. Disable print programmatically in the viewer |
| 33 | + - Set [`enablePrint`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer#enableprint) to `false` to disable the print UI even if the PDF allows printing. |
| 34 | + |
| 35 | +5. Disable copy via text-selection UI |
| 36 | + - Set [`enableTextSelection`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer#enabletextselection) to `false` to stop text selection and copying through the viewer UI. |
| 37 | + |
| 38 | +**Example:** |
| 39 | + |
| 40 | +The following is a complete React example that demonstrates disabling printing and text selection in the viewer. |
| 41 | + |
| 42 | +{% tabs %} |
| 43 | +{% highlight ts tabtitle="App.tsx" %} |
| 44 | +{% raw %} |
| 45 | +import { PdfViewerComponent, Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView, Print, TextSelection, TextSearch, Annotation, FormFields, FormDesigner, PageOrganizer, Inject } from '@syncfusion/ej2-react-pdfviewer'; |
| 46 | +export default function App() { |
| 47 | + return ( |
| 48 | + <div style={{ height: '100vh' }}> |
| 49 | + <PdfViewerComponent |
| 50 | + id="pdfViewer" |
| 51 | + documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" |
| 52 | + resourceUrl="https://cdn.syncfusion.com/ej2/32.2.5/dist/ej2-pdfviewer-lib" |
| 53 | + enablePrint={false} // disables the print UI |
| 54 | + enableTextSelection={false} // disables text selection (prevents copy) |
| 55 | + enableDownload={false} |
| 56 | + style={{ height: '100%' }}> |
| 57 | + <Inject services={[Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView, Print, TextSelection, TextSearch, Annotation, FormFields, FormDesigner, PageOrganizer]} /> |
| 58 | + </PdfViewerComponent> |
| 59 | + </div> |
| 60 | + ); |
| 61 | +} |
| 62 | +{% endraw %} |
| 63 | +{% endhighlight %} |
| 64 | +{% endtabs %} |
| 65 | + |
| 66 | +**Expected result**: |
| 67 | +- The viewer renders the PDF. |
| 68 | +- Print button and print-related UI are hidden/disabled. |
| 69 | +- Text selection and copy operations from the viewer are disabled. |
| 70 | + |
| 71 | +## Server-side: Enforce restrictions with Syncfusion PDF Library |
| 72 | + |
| 73 | +Process the PDF on the server to set permissions that disallow printing or copying. The viewer will respect these permissions when the PDF is loaded. |
| 74 | + |
| 75 | +{% tabs %} |
| 76 | +{% highlight csharp tabtitle="Program.cs" %} |
| 77 | +{% raw %} |
| 78 | +using Syncfusion.Pdf.Parsing; |
| 79 | +using Syncfusion.Pdf.Security; |
| 80 | + |
| 81 | +using FileStream inputStream = new FileStream( |
| 82 | + Path.GetFullPath("input.pdf"), |
| 83 | + FileMode.Open, |
| 84 | + FileAccess.Read |
| 85 | +); |
| 86 | +PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream); |
| 87 | +loadedDocument.Security.Permissions = PdfPermissionsFlags.EditContent | PdfPermissionsFlags.EditAnnotations | PdfPermissionsFlags.FillFields | PdfPermissionsFlags.AssembleDocument | PdfPermissionsFlags.AccessibilityCopyContent; |
| 88 | +loadedDocument.Save(Path.GetFullPath(@"output.pdf")); |
| 89 | +loadedDocument.Close(true); |
| 90 | +{% endraw %} |
| 91 | +{% endhighlight %} |
| 92 | +{% endtabs %} |
| 93 | + |
| 94 | +Set the [`PdfPermissionsFlags`](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Security.PdfPermissionsFlags.html) appropriately to remove copy/print rights. The example above shows how to set flags. |
| 95 | + |
| 96 | +## Troubleshooting |
| 97 | + |
| 98 | +- If the Print button still appears: |
| 99 | + - Confirm [`enablePrint`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer#enableprint) is set to `false` on `PdfViewerComponent`. |
| 100 | + - If the PDF explicitly allows printing, prefer server-side removal of print permission. |
| 101 | +- If text can still be copied: |
| 102 | + - Confirm [`enableTextSelection`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer#enabletextselection) is set to `false` and your app isn't adding secondary copy handlers. |
| 103 | + |
| 104 | +## Related topics |
| 105 | + |
| 106 | +- [Redact Sensitive content in PDF](../Redaction/overview) |
| 107 | +- [Redaction APIs](../Redaction/programmatic-support) |
| 108 | +- [Redaction toolbar](../Redaction/toolbar) |
0 commit comments