|
| 1 | +<template> |
| 2 | + <div id="app"> |
| 3 | + <ul> |
| 4 | + <ejs-checkbox label='Hide Default Context Menu' id="enable" @change="contextmenuHelper" checked="false"></ejs-checkbox> |
| 5 | + <ejs-checkbox label='Add Custom option at bottom' id="position" @change="contextmenuHelper" checked="false"></ejs-checkbox> |
| 6 | + </ul> |
| 7 | + <ejs-pdfviewer |
| 8 | + id="pdfViewer" |
| 9 | + :resourceUrl="resourceUrl" |
| 10 | + :documentPath="documentPath" |
| 11 | + :documentLoad="documentLoad" |
| 12 | + :customContextMenuBeforeOpen="customContextMenuBeforeOpen" |
| 13 | + :customContextMenuSelect="customContextMenuSelect" > |
| 14 | + </ejs-pdfviewer> |
| 15 | + </div> |
| 16 | +</template> |
| 17 | + |
| 18 | +<script> |
| 19 | +import { PdfViewerComponent, Toolbar, Magnification, Navigation, LinkAnnotation, |
| 20 | + BookmarkView, ThumbnailView, Print, TextSelection, TextSearch, |
| 21 | + Annotation, FormDesigner, FormFields, PageOrganizer } from '@syncfusion/ej2-vue-pdfviewer'; |
| 22 | +import { CheckBoxComponent } from "@syncfusion/ej2-vue-buttons"; |
| 23 | +
|
| 24 | +export default { |
| 25 | + components: { |
| 26 | + 'ejs-pdfviewer': PdfViewerComponent, |
| 27 | + 'ejs-checkbox': CheckBoxComponent, |
| 28 | + }, |
| 29 | + name: 'app', |
| 30 | + data () { |
| 31 | + return { |
| 32 | + documentPath:"https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf", |
| 33 | + resourceUrl:"https://cdn.syncfusion.com/ej2/23.1.43/dist/ej2-pdfviewer-lib", |
| 34 | + menuItems: [{ |
| 35 | + text: 'Search In Google', |
| 36 | + id: 'search_in_google', |
| 37 | + iconCss: 'e-icons e-search' |
| 38 | + }, |
| 39 | + { |
| 40 | + text: 'Lock Annotation', |
| 41 | + iconCss: 'e-icons e-lock', |
| 42 | + id: 'lock_annotation' |
| 43 | + }, |
| 44 | + { |
| 45 | + text: 'Unlock Annotation', |
| 46 | + iconCss: 'e-icons e-unlock', |
| 47 | + id: 'unlock_annotation' |
| 48 | + }, |
| 49 | + { |
| 50 | + text: 'Lock Form Fields', |
| 51 | + iconCss: 'e-icons e-lock', |
| 52 | + id: 'read_only_true' |
| 53 | + }, |
| 54 | + { |
| 55 | + text: 'Unlock Form Fields', |
| 56 | + iconCss: 'e-icons e-unlock', |
| 57 | + id: 'read_only_false' |
| 58 | + }, |
| 59 | + ] |
| 60 | + }; |
| 61 | + }, |
| 62 | +
|
| 63 | + provide: { |
| 64 | + PdfViewer: [ Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView, |
| 65 | + Print, TextSelection, TextSearch, Annotation, FormFields, FormDesigner, PageOrganizer ] |
| 66 | + }, |
| 67 | + methods: { |
| 68 | + documentLoad: function () { |
| 69 | + var viewer = this.$refs.pdfviewer.ej2Instances; |
| 70 | + viewer.addCustomMenu(this.menuItems, false, false); |
| 71 | + }, |
| 72 | + customContextMenuSelect: function (args) { |
| 73 | + var viewer = this.$refs.pdfviewer.ej2Instances; |
| 74 | + switch (args.id) { |
| 75 | + case 'search_in_google': |
| 76 | + for (var i = 0; i < viewer.textSelectionModule.selectionRangeArray.length; i++) { |
| 77 | + var content = viewer.textSelectionModule.selectionRangeArray[i].textContent; |
| 78 | + if ((viewer.textSelectionModule.isTextSelection) && (/\S/.test(content))) { |
| 79 | + window.open('http://google.com/search?q=' + content); |
| 80 | + } |
| 81 | + } |
| 82 | + break; |
| 83 | + case 'lock_annotation': |
| 84 | + this.lockAnnotations(args); |
| 85 | + break; |
| 86 | + case 'unlock_annotation': |
| 87 | + this.unlockAnnotations(args); |
| 88 | + break; |
| 89 | + case 'read_only_true': |
| 90 | + this.setReadOnlyTrue(args); |
| 91 | + break; |
| 92 | + case 'read_only_false': |
| 93 | + this.setReadOnlyFalse(args); |
| 94 | + break; |
| 95 | + default: |
| 96 | + break; |
| 97 | + } |
| 98 | + }, |
| 99 | +
|
| 100 | + customContextMenuBeforeOpen: function (args) { |
| 101 | + var viewer = this.$refs.pdfviewer.ej2Instances; |
| 102 | + for (var i = 0; i < args.ids.length; i++) { |
| 103 | + var search = document.getElementById(args.ids[i]); |
| 104 | + if (search) { |
| 105 | + search.style.display = 'none'; |
| 106 | + if (args.ids[i] === 'search_in_google' && (viewer.textSelectionModule) && viewer.textSelectionModule.isTextSelection) { |
| 107 | + search.style.display = 'block'; |
| 108 | + } else if (args.ids[i] === "lock_annotation" || args.ids[i] === "unlock_annotation") { |
| 109 | + var isLockOption = args.ids[i] === "lock_annotation"; |
| 110 | + for (var j = 0; j < viewer.selectedItems.annotations.length; j++) { |
| 111 | + var selectedAnnotation = viewer.selectedItems.annotations[j]; |
| 112 | + if (selectedAnnotation && selectedAnnotation.annotationSettings) { |
| 113 | + var shouldDisplay = (isLockOption && !selectedAnnotation.annotationSettings.isLock) || |
| 114 | + (!isLockOption && selectedAnnotation.annotationSettings.isLock); |
| 115 | + search.style.display = shouldDisplay ? 'block' : 'none'; |
| 116 | + } |
| 117 | + } |
| 118 | + } else if ((args.ids[i] === "read_only_true" || args.ids[i] === "read_only_false") && viewer.selectedItems.formFields.length !== 0) { |
| 119 | + var isReadOnlyOption = args.ids[i] === "read_only_true"; |
| 120 | + for (var k = 0; k < viewer.selectedItems.formFields.length; k++) { |
| 121 | + var selectedFormFields = viewer.selectedItems.formFields[k]; |
| 122 | + if (selectedFormFields) { |
| 123 | + var selectedFormField = viewer.selectedItems.formFields[k].isReadonly; |
| 124 | + var displayMenu = (isReadOnlyOption && !selectedFormField) || (!isReadOnlyOption && selectedFormField); |
| 125 | + search.style.display = displayMenu ? 'block' : 'none'; |
| 126 | + } |
| 127 | + } |
| 128 | + } else if (args.ids[i] === 'formfield properties' && viewer.selectedItems.formFields.length !== 0) { |
| 129 | + search.style.display = 'block'; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + }, |
| 134 | +
|
| 135 | + lockAnnotations: function (args) { |
| 136 | + var viewer = this.$refs.pdfviewer.ej2Instances; |
| 137 | + for (var i = 0; i < viewer.annotationCollection.length; i++) { |
| 138 | + if (viewer.annotationCollection[i].uniqueKey === viewer.selectedItems.annotations[0].id) { |
| 139 | + viewer.annotationCollection[i].annotationSettings.isLock = true; |
| 140 | + viewer.annotationCollection[i].isCommentLock = true; |
| 141 | + viewer.annotation.editAnnotation(viewer.annotationCollection[i]); |
| 142 | + } |
| 143 | + args.cancel = false; |
| 144 | + } |
| 145 | + }, |
| 146 | +
|
| 147 | + unlockAnnotations: function (args) { |
| 148 | + var viewer = this.$refs.pdfviewer.ej2Instances; |
| 149 | + for (var i = 0; i < viewer.annotationCollection.length; i++) { |
| 150 | + if (viewer.annotationCollection[i].uniqueKey === viewer.selectedItems.annotations[0].id) { |
| 151 | + viewer.annotationCollection[i].annotationSettings.isLock = false; |
| 152 | + viewer.annotationCollection[i].isCommentLock = false; |
| 153 | + viewer.annotation.editAnnotation(viewer.annotationCollection[i]); |
| 154 | + } |
| 155 | + args.cancel = false; |
| 156 | + } |
| 157 | + }, |
| 158 | +
|
| 159 | + setReadOnlyTrue: function (args) { |
| 160 | + var viewer = this.$refs.pdfviewer.ej2Instances; |
| 161 | + var selectedFormFields = viewer.selectedItems.formFields; |
| 162 | + for (var i = 0; i < selectedFormFields.length; i++) { |
| 163 | + var selectedFormField = selectedFormFields[i]; |
| 164 | + if (selectedFormField) { |
| 165 | + viewer.formDesignerModule.updateFormField(selectedFormField, { |
| 166 | + isReadOnly: true, |
| 167 | + }); |
| 168 | + } |
| 169 | + args.cancel = false; |
| 170 | + } |
| 171 | + }, |
| 172 | +
|
| 173 | + setReadOnlyFalse: function (args) { |
| 174 | + var viewer = this.$refs.pdfviewer.ej2Instances; |
| 175 | + var selectedFormFields = viewer.selectedItems.formFields; |
| 176 | + for (var i = 0; i < selectedFormFields.length; i++) { |
| 177 | + var selectedFormField = selectedFormFields[i]; |
| 178 | + if (selectedFormField) { |
| 179 | + viewer.formDesignerModule.updateFormField(selectedFormField, { |
| 180 | + isReadOnly: false, |
| 181 | + }); |
| 182 | + } |
| 183 | + args.cancel = false; |
| 184 | + } |
| 185 | + }, |
| 186 | +
|
| 187 | + contextmenuHelper: function () { |
| 188 | + var viewer = this.$refs.pdfviewer.ej2Instances; |
| 189 | + viewer.addCustomMenu(this.menuItems, enable.checked, position.checked); |
| 190 | + }, |
| 191 | + } |
| 192 | +} |
| 193 | +</script> |
| 194 | +<style> |
| 195 | + @import '../node_modules/@syncfusion/ej2-base/styles/material.css'; |
| 196 | + @import '../node_modules/@syncfusion/ej2-buttons/styles/material.css'; |
| 197 | + @import '../node_modules/@syncfusion/ej2-dropdowns/styles/material.css'; |
| 198 | + @import '../node_modules/@syncfusion/ej2-inputs/styles/material.css'; |
| 199 | + @import '../node_modules/@syncfusion/ej2-navigations/styles/material.css'; |
| 200 | + @import '../node_modules/@syncfusion/ej2-popups/styles/material.css'; |
| 201 | + @import '../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css'; |
| 202 | + @import '../node_modules/@syncfusion/ej2-lists/styles/material.css'; |
| 203 | + @import '../node_modules/@syncfusion/ej2-vue-pdfviewer/styles/material.css'; |
| 204 | + #pdfViewer { |
| 205 | + height: 640px; |
| 206 | + } |
| 207 | +</style> |
| 208 | + |
| 209 | + |
0 commit comments