1
+ var pdfviewer = new ej . pdfviewer . PdfViewer ( {
2
+ documentPath : 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf' ,
3
+ resourceUrl :'https://cdn.syncfusion.com/ej2/24.2.3/dist/ej2-pdfviewer-lib'
4
+ } ) ;
5
+ ej . pdfviewer . PdfViewer . Inject ( ej . pdfviewer . TextSelection , ej . pdfviewer . TextSearch , ej . pdfviewer . Print , ej . pdfviewer . Navigation , ej . pdfviewer . Toolbar ,
6
+ ej . pdfviewer . Magnification , ej . pdfviewer . Annotation , ej . pdfviewer . FormDesigner , ej . pdfviewer . FormFields , ej . pdfviewer . PageOrganizer ) ;
7
+
8
+ var menuItems = [
9
+ {
10
+ text : 'Search In Google' ,
11
+ id : 'search_in_google' ,
12
+ iconCss : 'e-icons e-search'
13
+ } ,
14
+ {
15
+ text : 'Lock Annotation' ,
16
+ iconCss : 'e-icons e-lock' ,
17
+ id : 'lock_annotation'
18
+ } ,
19
+ {
20
+ text : 'Unlock Annotation' ,
21
+ iconCss : 'e-icons e-unlock' ,
22
+ id : 'unlock_annotation'
23
+ } ,
24
+ {
25
+ text : 'Lock Form Field' ,
26
+ iconCss : 'e-icons e-lock' ,
27
+ id : 'read_only_true'
28
+ } ,
29
+ {
30
+ text : 'UnLock Form Field' ,
31
+ iconCss : 'e-icons e-unlock' ,
32
+ id : 'read_only_false'
33
+ } ,
34
+ ] ;
35
+
36
+ pdfviewer . appendTo ( '#PdfViewer' ) ;
37
+
38
+ pdfviewer . documentLoad = function ( args ) {
39
+ pdfviewer . addCustomMenu ( menuItems , false , false ) ;
40
+ }
41
+
42
+ pdfviewer . customContextMenuSelect = function ( args ) {
43
+ switch ( args . id ) {
44
+ case 'search_in_google' :
45
+ for ( var i = 0 ; i < pdfviewer . textSelectionModule . selectionRangeArray . length ; i ++ ) {
46
+ var content = pdfviewer . textSelectionModule . selectionRangeArray [ i ] . textContent ;
47
+ if ( ( pdfviewer . textSelectionModule . isTextSelection ) && ( / \S / . test ( content ) ) ) {
48
+ window . open ( 'http://google.com/search?q=' + content ) ;
49
+ }
50
+ }
51
+ break ;
52
+ case 'lock_annotation' :
53
+ lockAnnotations ( args ) ;
54
+ break ;
55
+ case 'unlock_annotation' :
56
+ unlockAnnotations ( args ) ;
57
+ break ;
58
+ case 'read_only_true' :
59
+ setReadOnlyTrue ( args ) ;
60
+ break ;
61
+ case 'read_only_false' :
62
+ setReadOnlyFalse ( args ) ;
63
+ break ;
64
+ case 'formfield properties' :
65
+ break ;
66
+ default :
67
+ break ;
68
+ }
69
+ } ;
70
+
71
+ pdfviewer . customContextMenuBeforeOpen = function ( args ) {
72
+ for ( var i = 0 ; i < args . ids . length ; i ++ ) {
73
+ var search = document . getElementById ( args . ids [ i ] ) ;
74
+ if ( search ) {
75
+ search . style . display = 'none' ;
76
+ if ( args . ids [ i ] === 'search_in_google' && ( pdfviewer . textSelectionModule ) && pdfviewer . textSelectionModule . isTextSelection ) {
77
+ search . style . display = 'block' ;
78
+ } else if ( args . ids [ i ] === "lock_annotation" || args . ids [ i ] === "unlock_annotation" ) {
79
+ var isLockOption = args . ids [ i ] === "lock_annotation" ;
80
+ for ( var j = 0 ; j < pdfviewer . selectedItems . annotations . length ; j ++ ) {
81
+ var selectedAnnotation = pdfviewer . selectedItems . annotations [ j ] ;
82
+ if ( selectedAnnotation && selectedAnnotation . annotationSettings ) {
83
+ var shouldDisplay = ( isLockOption && ! selectedAnnotation . annotationSettings . isLock ) ||
84
+ ( ! isLockOption && selectedAnnotation . annotationSettings . isLock ) ;
85
+ search . style . display = shouldDisplay ? 'block' : 'none' ;
86
+ }
87
+ }
88
+ } else if ( ( args . ids [ i ] === "read_only_true" || args . ids [ i ] === "read_only_false" ) && pdfviewer . selectedItems . formFields . length !== 0 ) {
89
+ var isReadOnlyOption = args . ids [ i ] === "read_only_true" ;
90
+ for ( var k = 0 ; k < pdfviewer . selectedItems . formFields . length ; k ++ ) {
91
+ var selectedFormFields = pdfviewer . selectedItems . formFields [ k ] ;
92
+ if ( selectedFormFields ) {
93
+ var selectedFormField = pdfviewer . selectedItems . formFields [ k ] . isReadonly ;
94
+ var displayMenu = ( isReadOnlyOption && ! selectedFormField ) || ( ! isReadOnlyOption && selectedFormField ) ;
95
+ search . style . display = displayMenu ? 'block' : 'none' ;
96
+ }
97
+ }
98
+ } else if ( args . ids [ i ] === 'formfield properties' && pdfviewer . selectedItems . formFields . length !== 0 ) {
99
+ search . style . display = 'block' ;
100
+ }
101
+ }
102
+ }
103
+ } ;
104
+
105
+ function lockAnnotations ( args ) {
106
+ for ( var i = 0 ; i < pdfviewer . annotationCollection . length ; i ++ ) {
107
+ if ( pdfviewer . annotationCollection [ i ] . uniqueKey === pdfviewer . selectedItems . annotations [ 0 ] . id ) {
108
+ pdfviewer . annotationCollection [ i ] . annotationSettings . isLock = true ;
109
+ pdfviewer . annotationCollection [ i ] . isCommentLock = true ;
110
+ pdfviewer . annotation . editAnnotation ( pdfviewer . annotationCollection [ i ] ) ;
111
+ }
112
+ args . cancel = false ;
113
+ }
114
+ }
115
+
116
+ function unlockAnnotations ( args ) {
117
+ for ( var i = 0 ; i < pdfviewer . annotationCollection . length ; i ++ ) {
118
+ if ( pdfviewer . annotationCollection [ i ] . uniqueKey === pdfviewer . selectedItems . annotations [ 0 ] . id ) {
119
+ pdfviewer . annotationCollection [ i ] . annotationSettings . isLock = false ;
120
+ pdfviewer . annotationCollection [ i ] . isCommentLock = false ;
121
+ pdfviewer . annotation . editAnnotation ( pdfviewer . annotationCollection [ i ] ) ;
122
+ }
123
+ args . cancel = false ;
124
+ }
125
+ }
126
+
127
+ function setReadOnlyTrue ( args ) {
128
+ var selectedFormFields = pdfviewer . selectedItems . formFields ;
129
+ for ( var i = 0 ; i < selectedFormFields . length ; i ++ ) {
130
+ var selectFormFields = selectedFormFields [ i ] ;
131
+ if ( selectFormFields ) {
132
+ pdfviewer . formDesignerModule . updateFormField ( selectFormFields , {
133
+ isReadOnly : true ,
134
+ } ) ;
135
+ }
136
+ args . cancel = false ;
137
+ }
138
+ }
139
+
140
+ function setReadOnlyFalse ( args ) {
141
+ var selectedFormFields = pdfviewer . selectedItems . formFields ;
142
+ for ( var i = 0 ; i < selectedFormFields . length ; i ++ ) {
143
+ var selectFormFields = selectedFormFields [ i ] ;
144
+ if ( selectFormFields ) {
145
+ pdfviewer . formDesignerModule . updateFormField ( selectFormFields , {
146
+ isReadOnly : false ,
147
+ } ) ;
148
+ }
149
+ args . cancel = false ;
150
+ }
151
+ }
152
+
153
+ var defaultCheckBoxObj = new ej . buttons . CheckBox ( {
154
+ change : contextmenuHelper ,
155
+ label : "Hide Default Context Menu"
156
+ } ) ;
157
+ defaultCheckBoxObj . appendTo ( '#hide' ) ;
158
+
159
+ var positionCheckBoxObj = new ej . buttons . CheckBox ( {
160
+ change : contextmenuHelper ,
161
+ label : "Add Custom option at bottom"
162
+
163
+ } ) ;
164
+ positionCheckBoxObj . appendTo ( '#toolbar' ) ;
165
+
166
+ function contextmenuHelper ( args ) {
167
+ pdfviewer . addCustomMenu ( menuItems , defaultCheckBoxObj . checked , positionCheckBoxObj . checked ) ;
168
+ }
0 commit comments