Skip to content

Commit 356bff7

Browse files
Merge pull request #2271 from syncfusion-content/EJ2-1010807-react-diataxis
1010807: Added custom context menu docs
2 parents 68ad3ff + 78271b5 commit 356bff7

9 files changed

Lines changed: 355 additions & 331 deletions

File tree

Document-Processing-toc.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,12 @@
10261026
<li><a href="/document-processing/pdf/pdf-viewer/react/text-selection">Text Selection</a></li>
10271027
<li><a href="/document-processing/pdf/pdf-viewer/react/globalization">Globalization</a></li>
10281028
<li><a href="/document-processing/pdf/pdf-viewer/react/accessibility">Accessibility</a></li>
1029+
<li><a href="/document-processing/pdf/pdf-viewer/react/context-menu/context-menu">Context Menu</a>
1030+
<ul>
1031+
<li><a href="/document-processing/pdf/pdf-viewer/react/context-menu/builtin-context-menu">Built-in Context Menu</a></li>
1032+
<li><a href="/document-processing/pdf/pdf-viewer/react/context-menu/custom-context-menu">Customize Context Menu</a></li>
1033+
</ul>
1034+
</li>
10291035
<li><a href="/document-processing/pdf/pdf-viewer/react/how-to-overview">How to</a>
10301036
<ul>
10311037
<li><a href="/document-processing/pdf/pdf-viewer/react/how-to/load-document">Load the PDF documents dynamically</a></li>
@@ -1049,7 +1055,6 @@
10491055
<li><a href="/document-processing/pdf/pdf-viewer/react/how-to/retry-timeout">Retry Timeout</a></li>
10501056
<li><a href="/document-processing/pdf/pdf-viewer/react/how-to/load-n-number-page">Load N number of pages on initial loading</a></li>
10511057
<li><a href="/document-processing/pdf/pdf-viewer/react/how-to/conformance">Supported conformance documents</a></li>
1052-
<li><a href="/document-processing/pdf/pdf-viewer/react/how-to/custom-context-menu">Customize context menu</a></li>
10531058
<li><a href="/document-processing/pdf/pdf-viewer/react/how-to/pagerenderstarted-pagerendercompleted-event">PageRenderInitiate and PageRenderComplete event</a></li>
10541059
<li><a href="/document-processing/pdf/pdf-viewer/react/how-to/open-bookmark">Open and Close Bookmark pane programmatically</a></li>
10551060
<li><a href="/document-processing/pdf/pdf-viewer/react/how-to/signatureselect-signatureunselect">SignatureSelect and SignatureUnselect event</a></li>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
layout: post
3+
title: Built-in Context Menu in React PDF Viewer | Syncfusion
4+
description: Explore the default context menu items in the React PDF Viewer, including options for text selection, annotations, and form fields.
5+
control: PDF Viewer
6+
platform: document-processing
7+
documentation: ug
8+
domainurl: ##DomainURL##
9+
---
10+
11+
# Built-in Context Menu Items in React PDF Viewer
12+
13+
The React PDF Viewer includes a context-sensitive menu that updates dynamically based on the right-clicked element within the document. This page lists the default menu items available for different document elements.
14+
15+
## Context Menu Scenarios
16+
17+
Menu items vary depending on the target element:
18+
19+
* **Text**: Displays options to annotate and copy selected text.
20+
21+
![context menu on text](../images/context-menu-text.png)
22+
23+
* **Annotations**: Provides options to copy, cut, paste, or remove annotations, and add comments.
24+
25+
![context menu on annotation](../images/context-menu-annotation.png)
26+
27+
* **Form Fields**: Shows standard form field interactions, such as modifying properties. The context menu for form fields appears only when the viewer is in **designer mode**.
28+
29+
![context menu on form fields](../images/context-menu-forms.png)
30+
31+
* **Empty Space**: Displays the option to paste a previously copied annotation or form field.
32+
33+
![context menu on empty space](../images/context-menu-empty.png)
34+
35+
## Default Item Reference
36+
37+
### Text Menu Items
38+
39+
The following table describes the default items available when right-clicking selected text:
40+
41+
| Item | Description |
42+
| :--- | :--- |
43+
| **Copy** | Copies selected text to the clipboard. |
44+
| **Highlight** | Highlights selected text using the default highlight color. |
45+
| **Underline** | Applies an underline to the selected text. |
46+
| **Strikethrough** | Applies a strikethrough to the selected text. |
47+
| **Squiggly** | Applies a squiggly underline to the selected text. |
48+
| **Redact Text** | Redacts the selected text. |
49+
50+
### Annotation Menu Items
51+
52+
The following items are available when interacting with annotations:
53+
54+
| Item | Description |
55+
| :--- | :--- |
56+
| **Copy** | Copies the selected annotation for pasting within the same page. |
57+
| **Cut** | Removes the selected annotation and copies it to the clipboard. |
58+
| **Paste** | Pastes a previously copied or cut annotation. |
59+
| **Delete** | Permanently removes the selected annotation. |
60+
| **Comments** | Opens the comment panel to manage discussions on the annotation. |
61+
62+
### Form Field Menu Items
63+
64+
These items appear when the viewer is in designer mode and a form field is selected:
65+
66+
| Item | Description |
67+
| :--- | :--- |
68+
| **Copy** | Copies the selected form field for duplication. |
69+
| **Cut** | Removes the selected form field for relocation. |
70+
| **Paste** | Pastes a copied or cut form field. |
71+
| **Delete** | Removes the selected form field from the document. |
72+
| **Properties** | Launches the properties dialog for the specific form field. |
73+
74+
N> The availability of the context menu is a client-side feature and is independent of server configurations.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
layout: post
3+
title: Context Menu in React PDF Viewer | Syncfusion
4+
description: Learn about the contextual menu options in the Syncfusion React PDF Viewer, including default behavior and customization possibilities.
5+
control: PDF Viewer
6+
platform: document-processing
7+
documentation: ug
8+
domainurl: ##DomainURL##
9+
---
10+
11+
# Context Menu in React PDF Viewer
12+
13+
The React PDF Viewer provides a built-in context menu for interacting with text, annotations, form fields, and document elements. This feature enhances the user experience by offering quick access to relevant actions based on the current selection or the specific area of the document being interacted with.
14+
15+
## Understanding the Context Menu
16+
17+
The context menu is designed to be context-aware, meaning it dynamically updates its items based on the target element. For instance, right-clicking on selected text will show annotation options, while right-clicking on an annotation will display management options like copy, cut, and delete.
18+
19+
### Core Capabilities
20+
21+
The context menu supports several configurations:
22+
23+
* **Default Behavior**: Provides standard actions such as cut, copy, and annotation management.
24+
* **Customization**: Allows adding new menu items, removing default ones, or reordering them to meet specific application requirements.
25+
* **Granular Control**: Developers can fully disable the menu or replace it with custom logic using events.
26+
27+
### Client-side Interaction
28+
29+
The availability and behavior of the context menu are governed primarily by client-side logic. It is not affected by server-side configurations or cloud environments, ensuring consistent performance across different deployment scenarios.
30+
31+
## Further Reading
32+
33+
* [Built-in Context Menu](builtin-context-menu) – A technical reference for default menu behavior and items.
34+
* [Customize Context Menu](custom-context-menu) – A guide on how to implement custom menu items and dynamic updates.
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
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

Comments
 (0)