|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Customize the context menu in React PDF Viewer | Syncfusion |
| 4 | +description: Learn how to add and customize custom context menu options in the React PDF Viewer using addCustomMenu, customContextMenuSelect, and related events. |
| 5 | +control: PDF Viewer |
| 6 | +platform: document-processing |
| 7 | +documentation: ug |
| 8 | +domainurl: ##DomainURL## |
| 9 | +--- |
| 10 | + |
| 11 | +# How to Customize the context menu in PDF Viewer in React |
| 12 | + |
| 13 | +The PDF Viewer supports extensive customization of the context menu, including reaching specific goals like adding new items, hiding default options, and handling custom click events. |
| 14 | + |
| 15 | +## Add Custom Context Menu Items |
| 16 | + |
| 17 | +You can add custom options to the context menu using the [addCustomMenu()](https://ej2.syncfusion.com/react/documentation/api/pdfviewer#addcustommenu) method. This is typically implemented during the [`documentLoad`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer#documentload) event. |
| 18 | + |
| 19 | +### Implementation Guide |
| 20 | + |
| 21 | +1. Define the menu items as an array of objects. |
| 22 | +2. Call the [`addCustomMenu`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer#addcustommenu) method within the [`documentLoad`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer#documentload) event handler. |
| 23 | + |
| 24 | +```jsx |
| 25 | + export function App() { |
| 26 | + |
| 27 | + let menuItems = [ |
| 28 | + { |
| 29 | + text: 'Search In Google', |
| 30 | + id: 'search_in_google', |
| 31 | + iconCss: 'e-icons e-search' |
| 32 | + }, |
| 33 | + { |
| 34 | + text: 'Lock Annotation', |
| 35 | + iconCss: 'e-icons e-lock', |
| 36 | + id: 'lock_annotation' |
| 37 | + }, |
| 38 | + { |
| 39 | + text: 'Unlock Annotation', |
| 40 | + iconCss: 'e-icons e-unlock', |
| 41 | + id: 'unlock_annotation' |
| 42 | + }, |
| 43 | + { |
| 44 | + text: 'Lock Form Field', |
| 45 | + iconCss: 'e-icons e-lock', |
| 46 | + id: 'read_only_true' |
| 47 | + }, |
| 48 | + { |
| 49 | + text: 'Unlock Form Field', |
| 50 | + iconCss: 'e-icons e-unlock', |
| 51 | + id: 'read_only_false' |
| 52 | + }, |
| 53 | + ]; |
| 54 | + function documentLoad(args) { |
| 55 | + var viewer = document.getElementById('container').ej2_instances[0]; |
| 56 | + viewer.addCustomMenu(menuItems, false); |
| 57 | + } |
| 58 | + |
| 59 | + return ( |
| 60 | + <div> |
| 61 | + <div className='control-section'> |
| 62 | + {/* Render the PDF Viewer */} |
| 63 | + <PdfViewerComponent |
| 64 | + id="container" |
| 65 | + documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" |
| 66 | + resourceUrl="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib" |
| 67 | + documentLoad={documentLoad} |
| 68 | + customContextMenuSelect={customContextMenuSelect} |
| 69 | + customContextMenuBeforeOpen={customContextMenuBeforeOpen} |
| 70 | + height='640px'> |
| 71 | + <Inject services={[Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, BookmarkView, |
| 72 | + ThumbnailView, Print, TextSelection, TextSearch, FormFields, FormDesigner]} /> |
| 73 | + </PdfViewerComponent> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | + ); |
| 77 | +} |
| 78 | +const root = ReactDOM.createRoot(document.getElementById('sample')); |
| 79 | +root.render(<App />); |
| 80 | +``` |
| 81 | + |
| 82 | +## Handle Click Events for Custom Menu Items |
| 83 | + |
| 84 | +The [customContextMenuSelect()](https://ej2.syncfusion.com/react/documentation/api/pdfviewer#customcontextMenuselect) method defines actions for custom menu items. |
| 85 | + |
| 86 | +```jsx |
| 87 | +function customContextMenuSelect(args) { |
| 88 | + var viewer = document.getElementById('container').ej2_instances[0]; |
| 89 | + switch (args.id) { |
| 90 | + case 'search_in_google': |
| 91 | + for (var i = 0; i < viewer.textSelectionModule.selectionRangeArray.length; i++) { |
| 92 | + var content = viewer.textSelectionModule.selectionRangeArray[i].textContent; |
| 93 | + if ((viewer.textSelectionModule.isTextSelection) && (\/\S\/.test(content))) { |
| 94 | + window.open('http://google.com/search?q=' + content); |
| 95 | + } |
| 96 | + } |
| 97 | + break; |
| 98 | + case 'lock_annotation': |
| 99 | + lockAnnotations(args); |
| 100 | + break; |
| 101 | + case 'unlock_annotation': |
| 102 | + unlockAnnotations(args); |
| 103 | + break; |
| 104 | + case 'read_only_true': |
| 105 | + setReadOnlyTrue(args); |
| 106 | + break; |
| 107 | + case 'read_only_false': |
| 108 | + setReadOnlyFalse(args); |
| 109 | + break; |
| 110 | + default: |
| 111 | + break; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +function lockAnnotations(args) { |
| 116 | + for (var i = 0; i < viewer.annotationCollection.length; i++) { |
| 117 | + if (viewer.annotationCollection[i].uniqueKey === viewer.selectedItems.annotations[0].id) { |
| 118 | + viewer.annotationCollection[i].annotationSettings.isLock = true; |
| 119 | + viewer.annotationCollection[i].isCommentLock = true; |
| 120 | + viewer.annotation.editAnnotation(viewer.annotationCollection[i]); |
| 121 | + } |
| 122 | + args.cancel = false; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +function unlockAnnotations(args) { |
| 127 | + for (var i = 0; i < viewer.annotationCollection.length; i++) { |
| 128 | + if (viewer.annotationCollection[i].uniqueKey === viewer.selectedItems.annotations[0].id) { |
| 129 | + viewer.annotationCollection[i].annotationSettings.isLock = false; |
| 130 | + viewer.annotationCollection[i].isCommentLock = false; |
| 131 | + viewer.annotation.editAnnotation(viewer.annotationCollection[i]); |
| 132 | + } |
| 133 | + args.cancel = false; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +function setReadOnlyTrue(args) { |
| 138 | + var viewer = document.getElementById('container').ej2_instances[0]; |
| 139 | + var selectedFormFields = viewer.selectedItems.formFields; |
| 140 | + for (var i = 0; i < selectedFormFields.length; i++) { |
| 141 | + var selectedFormField = selectedFormFields[i]; |
| 142 | + if (selectedFormField) { |
| 143 | + viewer.formDesignerModule.updateFormField(selectedFormField, { |
| 144 | + isReadOnly: true, |
| 145 | + }); |
| 146 | + } |
| 147 | + args.cancel = false; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +function setReadOnlyFalse(args) { |
| 152 | + var viewer = document.getElementById('container').ej2_instances[0]; |
| 153 | + var selectedFormFields = viewer.selectedItems.formFields; |
| 154 | + for (var i = 0; i < selectedFormFields.length; i++) { |
| 155 | + var selectedFormField = selectedFormFields[i]; |
| 156 | + if (selectedFormField) { |
| 157 | + viewer.formDesignerModule.updateFormField(selectedFormField, { |
| 158 | + isReadOnly: false, |
| 159 | + }); |
| 160 | + } |
| 161 | + args.cancel = false; |
| 162 | + } |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +## Dynamic Context Menu Customization |
| 167 | + |
| 168 | +The [customContextMenuBeforeOpen()](https://ej2.syncfusion.com/react/documentation/api/pdfviewer#customcontextMenubeforeopen) event allows for dynamic showing or hiding of items based on selection or document state. |
| 169 | + |
| 170 | +```jsx |
| 171 | +function customContextMenuBeforeOpen(args) { |
| 172 | + for (var i = 0; i < args.ids.length; i++) { |
| 173 | + var search = document.getElementById(args.ids[i]); |
| 174 | + var viewer = document.getElementById('container').ej2_instances[0]; |
| 175 | + if (search) { |
| 176 | + search.style.display = 'none'; |
| 177 | + if (args.ids[i] === 'search_in_google' && (viewer.textSelectionModule) && viewer.textSelectionModule.isTextSelection) { |
| 178 | + search.style.display = 'block'; |
| 179 | + } else if (args.ids[i] === "lock_annotation" || args.ids[i] === "unlock_annotation") { |
| 180 | + var isLockOption = args.ids[i] === "lock_annotation"; |
| 181 | + for (var j = 0; j < viewer.selectedItems.annotations.length; j++) { |
| 182 | + var selectedAnnotation = viewer.selectedItems.annotations[j]; |
| 183 | + if (selectedAnnotation && selectedAnnotation.annotationSettings) { |
| 184 | + var shouldDisplay = (isLockOption && !selectedAnnotation.annotationSettings.isLock) || |
| 185 | + (!isLockOption && selectedAnnotation.annotationSettings.isLock); |
| 186 | + search.style.display = shouldDisplay ? 'block' : 'none'; |
| 187 | + } |
| 188 | + } |
| 189 | + } else if (args.ids[i] === "read_only_true" && viewer.selectedItems.formFields.length !== 0) { |
| 190 | + var selectedFormField = viewer.selectedItems.formFields[0].isReadonly; |
| 191 | + search.style.display = selectedFormField ? 'none' : 'block'; |
| 192 | + } else if (args.ids[i] === "read_only_false" && viewer.selectedItems.formFields.length !== 0) { |
| 193 | + var selectedFormField = viewer.selectedItems.formFields[0].isReadonly; |
| 194 | + search.style.display = selectedFormField ? 'block' : 'none'; |
| 195 | + } else if (args.ids[i] === 'formfield properties' && viewer.selectedItems.formFields.length !== 0) { |
| 196 | + search.style.display = 'block'; |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
| 201 | +``` |
| 202 | + |
| 203 | +## Disable the Context Menu Entirely |
| 204 | + |
| 205 | +The context menu in the PDF Viewer can be fully disabled by setting the [`contextMenuOption`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer#contextmenuoption) property to `None`. |
| 206 | + |
| 207 | +```js |
| 208 | +<PdfViewerComponent |
| 209 | + id="pdfViewer" |
| 210 | + documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" |
| 211 | + height="100%" |
| 212 | + width="100%" |
| 213 | + contextMenuOption='None' > |
| 214 | + <Inject |
| 215 | + services={[ |
| 216 | + Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView, Print, TextSelection, TextSearch, Annotation, FormDesigner, FormFields |
| 217 | + ]} |
| 218 | + /> |
| 219 | +</PdfViewerComponent> |
| 220 | +``` |
| 221 | + |
| 222 | +The following is the output of the custom context menu with customization. |
| 223 | + |
| 224 | +{% tabs %} |
| 225 | +{% highlight js tabtitle="index.jsx" %} |
| 226 | +{% raw %} |
| 227 | +{% include code-snippet/pdfviewer/react/custom-context-menu/app/index.jsx %} |
| 228 | +{% endraw %} |
| 229 | +{% endhighlight %} |
| 230 | +{% highlight ts tabtitle="index.tsx" %} |
| 231 | +{% raw %} |
| 232 | +{% include code-snippet/pdfviewer/react/custom-context-menu/app/index.tsx %} |
| 233 | +{% endraw %} |
| 234 | +{% endhighlight %} |
| 235 | +{% endtabs %} |
| 236 | + |
| 237 | +N> To set up the **server-backed PDF Viewer**, add the following `serviceUrl` within the <div> element in either the `index.TSX` or `index.JSX` file: **serviceUrl="https://document.syncfusion.com/web-services/pdf-viewer/api/pdfviewer"**. |
| 238 | + |
| 239 | +{% previewsample "document-processing/code-snippet/pdfviewer/react/custom-context-menu" %} |
| 240 | + |
| 241 | +[View the sample in Stack blitz](https://stackblitz.com/edit/react-zmbkebwq?file=index.js) |
0 commit comments