|
1 | 1 | --- |
2 | 2 | layout: post |
3 | | -title: Annotation Toolbar in React PDF viewer control | Syncfusion |
4 | | -description: Learn here all about annotation toolbar customization in Syncfusion React PDF viewer control of Syncfusion Essential JS 2 and more. |
| 3 | +title: Customize the Annotation Toolbar in React PDF Viewer | Syncfusion |
| 4 | +description: Show or hide and customize the annotation toolbar in the EJ2 React PDF Viewer with runnable examples. |
5 | 5 | platform: document-processing |
6 | | -control: PDF Viewer |
| 6 | +control: PDF Viewer |
7 | 7 | documentation: ug |
8 | 8 | domainurl: ##DomainURL## |
9 | 9 | --- |
10 | 10 |
|
11 | | -# Annotation Toolbar Customization in React |
| 11 | +# Customize the Annotation Toolbar in React PDF Viewer |
12 | 12 |
|
13 | | -The annotation toolbar can be customized by showing or hiding default items and by controlling the order in which they appear. |
| 13 | +## Overview |
14 | 14 |
|
15 | | -## Show or hide the annotation toolbar |
| 15 | +This guide shows how to show or hide the annotation toolbar and how to choose which tools appear and their order. |
16 | 16 |
|
17 | | -Show or hide the annotation toolbar programmatically during initialization or at runtime. |
| 17 | +**Outcome:** A working React example that toggles the annotation toolbar and uses [`annotationToolbarItems`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer/toolbarsettings#annotationtoolbaritems) to customize the toolbar. |
18 | 18 |
|
19 | | -Use the [EnableAnnotationToolbar](https://ej2.syncfusion.com/react/documentation/api/pdfviewer/pdfViewerModel#enableannotationtoolbar) property or the [showAnnotationToolbar](https://ej2.syncfusion.com/react/documentation/api/pdfviewer/toolbar#showannotationtoolbar) method to toggle visibility. |
| 19 | +## Prerequisites |
20 | 20 |
|
21 | | -The following example shows how to show or hide the annotation toolbar using the `showAnnotationToolbar` method. |
| 21 | +- EJ2 React PDF Viewer installed and added in your project. See [getting started guide](../getting-started) |
| 22 | +- A valid [`resourceUrl`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer#resourceurl) or [`serviceUrl`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer#serviceurl) for viewer assets when running locally |
22 | 23 |
|
23 | | -{% tabs %} |
24 | | -{% highlight ts tabtitle="app.tsx" %} |
25 | | -import * as React from 'react'; |
26 | | -import * as ReactDOM from 'react-dom'; |
27 | | -import { PdfViewerComponent, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, |
28 | | - ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner } from '@syncfusion/ej2-react-pdfviewer'; |
29 | | - |
30 | | -PdfViewerComponent.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, |
31 | | - BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner); |
32 | | - |
33 | | -const App = () => { |
34 | | - let pdfviewer; |
35 | | - |
36 | | - React.useEffect(() => { |
37 | | - const buttonElement = document.getElementById('showAnnotationToolbar'); |
38 | | - const handleClick = () => { |
39 | | - if (pdfviewer) { |
40 | | - pdfviewer.toolbar.showAnnotationToolbar(false); |
41 | | - } |
42 | | - }; |
43 | | - buttonElement?.addEventListener('click', handleClick); |
44 | | - return () => buttonElement?.removeEventListener('click', handleClick); |
45 | | - }, []); |
| 24 | +## Steps |
46 | 25 |
|
47 | | - return ( |
48 | | - <div> |
49 | | - <button id='showAnnotationToolbar'>showAnnotationToolbar</button> |
| 26 | +**Step 1:** Show or hide the annotation toolbar |
| 27 | + |
| 28 | + - Use the [`showAnnotationToolbar`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer/toolbar#showannotationtoolbar) method on the viewer toolbar to control visibility. |
| 29 | + |
| 30 | +{% tabs %} |
| 31 | +{% highlight ts tabtitle="App.tsx" %} |
| 32 | +{% raw %} |
| 33 | +const viewer: RefObject<PdfViewerComponent> = useRef(null); |
| 34 | +const [show, setShow] = useState(true); |
| 35 | +const hideToolbar = () => { |
| 36 | + viewer.current.toolbar.showAnnotationToolbar(show); |
| 37 | + setShow(!show); |
| 38 | +}; |
| 39 | +return ( |
| 40 | + <div> |
| 41 | + <button onClick={hideToolbar}>Hide Annotation Toolbar</button> |
50 | 42 | <PdfViewerComponent |
51 | 43 | id="PdfViewer" |
52 | | - ref={(scope) => { pdfviewer = scope; }} |
53 | | - documentPath='https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf' |
| 44 | + ref={viewer} |
| 45 | + documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" |
54 | 46 | resourceUrl="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib" |
55 | | - height="500px" |
56 | | - width="100%" |
57 | | - /> |
58 | | - </div> |
59 | | - ); |
60 | | -}; |
61 | | - |
62 | | -ReactDOM.render(<App />, document.getElementById('container')); |
63 | | -{% endhighlight %} |
64 | | -{% highlight html tabtitle="index.html" %} |
65 | | - |
66 | | -<!DOCTYPE html> |
67 | | -<html lang="en"> |
68 | | - |
69 | | -<head> |
70 | | - <title>EJ2 PDF Viewer</title> |
71 | | - <meta charset="utf-8" /> |
72 | | - <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
73 | | - <meta name="description" content="React PDF Viewer Control" /> |
74 | | - <meta name="author" content="Syncfusion" /> |
75 | | - <link href="index.css" rel="stylesheet" /> |
76 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-base/styles/material.css" rel="stylesheet" /> |
77 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-pdfviewer/styles/material.css" rel="stylesheet" /> |
78 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-buttons/styles/material.css" rel="stylesheet" /> |
79 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-popups/styles/material.css" rel="stylesheet" /> |
80 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-navigations/styles/material.css" rel="stylesheet" /> |
81 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-dropdowns/styles/material.css" rel="stylesheet" /> |
82 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-lists/styles/material.css" rel="stylesheet" /> |
83 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-inputs/styles/material.css" rel="stylesheet" /> |
84 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-splitbuttons/styles/material.css" rel="stylesheet" /> |
85 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-notifications/styles/material.css" rel="stylesheet" /> |
86 | | - |
87 | | - |
88 | | - <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script> |
89 | | - <script src="systemjs.config.js"></script> |
90 | | -</head> |
91 | | -<body> |
92 | | - <div id='loader'>Loading....</div> |
93 | | - <button id="set">Change Annotation Toolbar Visibility</button> |
94 | | - <div id='container'> |
95 | | - <div id='PdfViewer' style="height:500px;width:100%;"></div> |
| 47 | + height="600px" |
| 48 | + width="100%" > |
| 49 | + <Inject services={[Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, |
| 50 | + BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer]} /> |
| 51 | + </PdfViewerComponent> |
96 | 52 | </div> |
97 | | -</body> |
98 | | -</html> |
99 | | - |
| 53 | +); |
| 54 | +{% endraw %} |
100 | 55 | {% endhighlight %} |
101 | 56 | {% endtabs %} |
102 | 57 |
|
103 | | -## How to customize the annotation toolbar |
| 58 | +**Step 2:** Customize which tools appear |
104 | 59 |
|
105 | | -Choose which tools appear and control their order in the annotation toolbar. |
| 60 | + - Use [`annotationToolbarItems`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer/toolbarsettings#annotationtoolbaritems) with a list of [`AnnotationToolbarItem`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer/annotationtoolbaritem) values. The toolbar shows only items in this list. |
106 | 61 |
|
107 | | -Use [`PdfViewerToolbarSettings`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer/toolbarSettings) with the [`AnnotationToolbarItems`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer/toolbarSettings#annotationtoolbaritems) property to choose which tools are displayed in the annotation toolbar. The property accepts a list of [`AnnotationToolbarItem`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer/annotationToolbarItem) values. Only the items included in this list are shown; any item not listed is hidden. The rendered order follows the sequence of items in the list. |
| 62 | +{% tabs %} |
| 63 | +{% highlight ts tabtitle="App.tsx" %} |
| 64 | +{% raw %} |
| 65 | +const annotationToolbarItems: AnnotationToolbarItem[] = [ |
| 66 | + 'HighlightTool', 'UnderlineTool', 'StrikethroughTool', 'ColorEditTool', 'OpacityEditTool', 'AnnotationDeleteTool', 'CommentPanelTool' |
| 67 | +]; |
| 68 | +<PdfViewerComponent |
| 69 | + id="PdfViewer" |
| 70 | + ref={viewer} |
| 71 | + documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" |
| 72 | + resourceUrl="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib" |
| 73 | + toolbarSettings={{ annotationToolbarItems }} |
| 74 | + width="100%" > |
| 75 | + <Inject services={[Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, |
| 76 | + BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer]} /> |
| 77 | +</PdfViewerComponent> |
| 78 | +{% endraw %} |
| 79 | +{% endhighlight %} |
| 80 | +{% endtabs %} |
108 | 81 |
|
109 | | -The annotation toolbar is presented when entering annotation mode in PDF Viewer and adapts responsively based on the available width. Include the Close tool to allow users to exit the annotation toolbar when needed. |
| 82 | +**Complete example** |
110 | 83 |
|
111 | | -The following example demonstrates how to customize the annotation toolbar by specifying a selected set of tools using `AnnotationToolbarItem`. |
| 84 | +The following is a complete, runnable example. It wires a toggle button and a viewer with a custom annotation toolbar list. |
112 | 85 |
|
113 | 86 | {% tabs %} |
114 | | -{% highlight ts tabtitle="app.tsx" %} |
115 | | -import * as React from 'react'; |
116 | | -import * as ReactDOM from 'react-dom'; |
117 | | -import { PdfViewerComponent, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, |
118 | | - ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, AnnotationToolbarItem } from '@syncfusion/ej2-react-pdfviewer'; |
119 | | - |
120 | | -PdfViewerComponent.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, |
121 | | - BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner); |
122 | | - |
123 | | -const App = () => { |
124 | | - let pdfviewer; |
125 | | - const annotationToolbarItems = [ |
126 | | - "HighlightTool", |
127 | | - "UnderlineTool", |
128 | | - "StrikethroughTool", |
129 | | - "ColorEditTool", |
130 | | - "OpacityEditTool", |
131 | | - "AnnotationDeleteTool", |
132 | | - "StampAnnotationTool", |
133 | | - "HandWrittenSignatureTool", |
134 | | - "InkAnnotationTool", |
135 | | - "ShapeTool", |
136 | | - "CalibrateTool", |
137 | | - "StrokeColorEditTool", |
138 | | - "ThicknessEditTool", |
139 | | - "FreeTextAnnotationTool", |
140 | | - "FontFamilyAnnotationTool", |
141 | | - "FontSizeAnnotationTool", |
142 | | - "FontStylesAnnotationTool", |
143 | | - "FontAlignAnnotationTool", |
144 | | - "FontColorAnnotationTool", |
145 | | - "CommentPanelTool" |
146 | | - ]; |
147 | | - |
| 87 | +{% highlight ts tabtitle="App.tsx" %} |
| 88 | +{% raw %} |
| 89 | +import { useRef, RefObject, useState } from 'react'; |
| 90 | +import { |
| 91 | + PdfViewerComponent, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, |
| 92 | + ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, |
| 93 | + AnnotationToolbarItem, PageOrganizer, Inject |
| 94 | +} from '@syncfusion/ej2-react-pdfviewer'; |
| 95 | +const annotationToolbarItems: AnnotationToolbarItem[] = [ |
| 96 | + 'HighlightTool', 'UnderlineTool', 'StrikethroughTool', 'ColorEditTool', 'OpacityEditTool', 'AnnotationDeleteTool', 'CommentPanelTool' |
| 97 | +]; |
| 98 | +export default function App() { |
| 99 | + const viewer: RefObject<PdfViewerComponent> = useRef(null); |
| 100 | + const [show, setShow] = useState(true); |
| 101 | + const hideToolbar = () => { |
| 102 | + viewer.current.toolbar.showAnnotationToolbar(show); |
| 103 | + setShow(!show); |
| 104 | + }; |
148 | 105 | return ( |
149 | | - <PdfViewerComponent |
150 | | - id="PdfViewer" |
151 | | - ref={(scope) => { pdfviewer = scope; }} |
152 | | - documentPath='https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf' |
153 | | - resourceUrl="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib" |
154 | | - height="500px" |
155 | | - width="100%" |
156 | | - toolbarSettings={{ annotationToolbarItems }} |
157 | | - /> |
| 106 | + <div> |
| 107 | + <button onClick={hideToolbar}>Hide Annotation Toolbar</button> |
| 108 | + <PdfViewerComponent |
| 109 | + id="PdfViewer" |
| 110 | + ref={viewer} |
| 111 | + documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" |
| 112 | + resourceUrl="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib" |
| 113 | + toolbarSettings={{ annotationToolbarItems }} |
| 114 | + height="600px" |
| 115 | + width="100%" > |
| 116 | + <Inject services={[Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, |
| 117 | + BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer]} /> |
| 118 | + </PdfViewerComponent> |
| 119 | + </div> |
158 | 120 | ); |
159 | 121 | }; |
160 | | - |
161 | | -ReactDOM.render(<App />, document.getElementById('container')); |
162 | | -{% endhighlight %} |
163 | | -{% highlight html tabtitle="index.html" %} |
164 | | - |
165 | | -<!DOCTYPE html> |
166 | | -<html lang="en"> |
167 | | - |
168 | | -<head> |
169 | | - <title>EJ2 PDF Viewer</title> |
170 | | - <meta charset="utf-8" /> |
171 | | - <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
172 | | - <meta name="description" content="React PDF Viewer Control" /> |
173 | | - <meta name="author" content="Syncfusion" /> |
174 | | - <link href="index.css" rel="stylesheet" /> |
175 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-base/styles/material.css" rel="stylesheet" /> |
176 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-pdfviewer/styles/material.css" rel="stylesheet" /> |
177 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-buttons/styles/material.css" rel="stylesheet" /> |
178 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-popups/styles/material.css" rel="stylesheet" /> |
179 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-navigations/styles/material.css" rel="stylesheet" /> |
180 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-dropdowns/styles/material.css" rel="stylesheet" /> |
181 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-lists/styles/material.css" rel="stylesheet" /> |
182 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-inputs/styles/material.css" rel="stylesheet" /> |
183 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-splitbuttons/styles/material.css" rel="stylesheet" /> |
184 | | - <link href="https://cdn.syncfusion.com/ej2/23.1.40/ej2-notifications/styles/material.css" rel="stylesheet" /> |
185 | | - |
186 | | - |
187 | | - <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script> |
188 | | - <script src="systemjs.config.js"></script> |
189 | | -</head> |
190 | | -<body> |
191 | | - <div id='loader'>Loading....</div> |
192 | | - <div id='container'> |
193 | | - <div id='PdfViewer' style="height:500px;width:100%;"></div> |
194 | | - </div> |
195 | | -</body> |
196 | | -</html> |
197 | | - |
| 122 | +{% endraw %} |
198 | 123 | {% endhighlight %} |
199 | 124 | {% endtabs %} |
| 125 | + |
| 126 | +## Troubleshooting |
| 127 | + |
| 128 | +- Annotation toolbar tools do not appear. |
| 129 | + - **Cause**: The items are not valid [`AnnotationToolbarItem`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer/annotationtoolbaritem) strings or the viewer is not injected with [`Annotation`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer/annotation) / [`Toolbar`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer/toolbar) modules. |
| 130 | + - **Solution**: Confirm services in `Inject` includes [`Toolbar`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer/toolbar) and [`Annotation`](https://ej2.syncfusion.com/react/documentation/api/pdfviewer/annotation) and use valid item names. |
| 131 | + |
| 132 | +## Related topics |
| 133 | + |
| 134 | +- [Customize form designer toolbar](./form-designer-toolbar) |
| 135 | +- [customize primary toolbar](./primary-toolbar) |
0 commit comments