|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Area annotation in TypeScript PDF Viewer | Syncfusion |
| 4 | +description: Learn to add, edit, and customize Area measurement annotations in Syncfusion TypeScript PDF Viewer with UI and programmatic examples. |
| 5 | +platform: document-processing |
| 6 | +control: PDF Viewer |
| 7 | +documentation: ug |
| 8 | +domainurl: ##DomainURL## |
| 9 | +--- |
| 10 | + |
| 11 | +# Area annotation in TypeScript PDF Viewer |
| 12 | + |
| 13 | +Area is a measurement annotation used to measure the surface of a closed region in the PDF. |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## Add Area Annotation |
| 18 | + |
| 19 | +### Add area annotation via UI |
| 20 | + |
| 21 | +Use the annotation toolbar: |
| 22 | +- Click the **Edit Annotation** button in the PDF Viewer toolbar. |
| 23 | +- Click the **Measurement Annotation** dropdown. |
| 24 | +- Choose **Area**, then draw the region on the page. |
| 25 | + |
| 26 | +N> When in pan mode, selecting a measurement annotation switches the viewer to text select mode. |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +### Add an area annotation programmatically |
| 31 | + |
| 32 | +#### Enable area mode |
| 33 | + |
| 34 | +The PDF Viewer library allows drawing measurement annotations programmatically after enabling area mode in button clicks. |
| 35 | + |
| 36 | +```html |
| 37 | +<button id="areaMode">Area</button> |
| 38 | +``` |
| 39 | +{% tabs %} |
| 40 | +{% highlight ts tabtitle="Standalone" %} |
| 41 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer } from '@syncfusion/ej2-pdfviewer'; |
| 42 | + |
| 43 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer); |
| 44 | + |
| 45 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 46 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"; |
| 47 | +pdfviewer.resourceUrl = "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"; |
| 48 | +pdfviewer.appendTo('#PdfViewer'); |
| 49 | + |
| 50 | +document.getElementById('areaMode')?.addEventListener('click', function () { |
| 51 | + pdfviewer.annotationModule.setAnnotationMode('Area'); |
| 52 | +}); |
| 53 | +{% endhighlight %} |
| 54 | +{% highlight ts tabtitle="Server-Backed" %} |
| 55 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer } from '@syncfusion/ej2-pdfviewer'; |
| 56 | + |
| 57 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer); |
| 58 | + |
| 59 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 60 | +pdfviewer.serviceUrl = 'https://document.syncfusion.com/web-services/pdf-viewer/api/pdfviewer/'; |
| 61 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"; |
| 62 | +pdfviewer.appendTo('#PdfViewer'); |
| 63 | + |
| 64 | +document.getElementById('areaMode')?.addEventListener('click', function () { |
| 65 | + pdfviewer.annotationModule.setAnnotationMode('Area'); |
| 66 | +}); |
| 67 | +{% endhighlight %} |
| 68 | +{% endtabs %} |
| 69 | + |
| 70 | +#### Add Area Annotation |
| 71 | + |
| 72 | +The PDF Viewer library allows adding measurement annotations programmatically using the [addAnnotation()](https://ej2.syncfusion.com/documentation/api/pdfviewer/annotation#annotation) method. |
| 73 | + |
| 74 | +```html |
| 75 | +<button id="addAreaAnnotation">Add Area annotation Programmatically</button> |
| 76 | +``` |
| 77 | +{% tabs %} |
| 78 | +{% highlight ts tabtitle="Standalone" %} |
| 79 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer, AreaSettings } from '@syncfusion/ej2-pdfviewer'; |
| 80 | + |
| 81 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer); |
| 82 | + |
| 83 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 84 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"; |
| 85 | +pdfviewer.resourceUrl = "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"; |
| 86 | + |
| 87 | +pdfviewer.appendTo('#PdfViewer'); |
| 88 | + |
| 89 | +document.getElementById('addAreaAnnotation')?.addEventListener('click', function () { |
| 90 | + pdfviewer.annotation.addAnnotation('Area', { |
| 91 | + offset: { x: 200, y: 500 }, |
| 92 | + pageNumber: 1, |
| 93 | + vertexPoints: [ |
| 94 | + { x: 200, y: 500 }, { x: 288, y: 499 }, { x: 289, y: 553 }, { x: 200, y: 500 } |
| 95 | + ] |
| 96 | + } as AreaSettings); |
| 97 | +}); |
| 98 | +{% endhighlight %} |
| 99 | +{% highlight ts tabtitle="Server-Backed" %} |
| 100 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer, AreaSettings } from '@syncfusion/ej2-pdfviewer'; |
| 101 | + |
| 102 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer); |
| 103 | + |
| 104 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 105 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"; |
| 106 | +pdfviewer.serviceUrl = 'https://document.syncfusion.com/web-services/pdf-viewer/api/pdfviewer/'; |
| 107 | + |
| 108 | +pdfviewer.appendTo('#PdfViewer'); |
| 109 | + |
| 110 | +document.getElementById('addAreaAnnotation')?.addEventListener('click', function () { |
| 111 | + pdfviewer.annotation.addAnnotation('Area', { |
| 112 | + offset: { x: 200, y: 500 }, |
| 113 | + pageNumber: 1, |
| 114 | + vertexPoints: [ |
| 115 | + { x: 200, y: 500 }, { x: 288, y: 499 }, { x: 289, y: 553 }, { x: 200, y: 500 } |
| 116 | + ] |
| 117 | + } as AreaSettings); |
| 118 | +}); |
| 119 | +{% endhighlight %} |
| 120 | +{% endtabs %} |
| 121 | + |
| 122 | +## Edit Area Annotation |
| 123 | + |
| 124 | +### Edit Area Annotation in UI |
| 125 | + |
| 126 | +You can select, move, and resize Area annotations directly in the viewer: |
| 127 | +- Select an Area to show its vertex handles. |
| 128 | +- Move: drag inside the shape to reposition it on the page. |
| 129 | +- Resize/reshape: drag any vertex handle to adjust the polygon points and size. |
| 130 | +- Delete or access more options from the context menu. |
| 131 | + |
| 132 | +Use the toolbar to change appearance: |
| 133 | +- Edit Color, Edit Stroke Color, Edit Thickness, and Edit Opacity tools. |
| 134 | + |
| 135 | +See the sections below for screenshots and details. |
| 136 | + |
| 137 | +### Edit the properties of area annotations |
| 138 | + |
| 139 | +The fill color, stroke color, thickness, and opacity can be edited using the Edit Color, Edit Stroke Color, Edit Thickness, and Edit Opacity tools in the annotation toolbar. |
| 140 | + |
| 141 | +#### Edit fill color |
| 142 | + |
| 143 | +The fill color of the annotation can be edited using the color palette provided in the Edit Color tool. |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +#### Edit stroke color |
| 148 | + |
| 149 | +The stroke color of the annotation can be edited using the color palette provided in the Edit Stroke Color tool. |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +#### Edit thickness |
| 154 | + |
| 155 | +Edit border thickness using the range slider provided in the Edit Thickness tool. |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +#### Edit opacity |
| 160 | + |
| 161 | +The opacity of the annotation can be edited using the range slider provided in the Edit Opacity tool. |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | +### Edit an existing area annotation programmatically |
| 166 | + |
| 167 | +To modify an existing area annotation programmatically, use the editAnnotation() method. |
| 168 | + |
| 169 | +Here is an example of using editAnnotation(): |
| 170 | + |
| 171 | +```html |
| 172 | +<button id="editAreaAnnotation">Edit Area annotation Programmatically</button> |
| 173 | +``` |
| 174 | + |
| 175 | +{% tabs %} |
| 176 | +{% highlight ts tabtitle="Standalone" %} |
| 177 | + |
| 178 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer} from '@syncfusion/ej2-pdfviewer'; |
| 179 | + |
| 180 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer); |
| 181 | + |
| 182 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 183 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"; |
| 184 | +pdfviewer.resourceUrl = "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"; |
| 185 | +pdfviewer.appendTo('#PdfViewer'); |
| 186 | + |
| 187 | +let editAreaAnnotation = document.getElementById('editAreaAnnotation'); |
| 188 | +if (editAreaAnnotation) { |
| 189 | + editAreaAnnotation.addEventListener('click', function () { |
| 190 | + if (pdfviewer) { |
| 191 | + for (let i = 0; i < pdfviewer.annotationCollection.length; i++) { |
| 192 | + if (pdfviewer.annotationCollection[i].subject === "Area calculation") { |
| 193 | + pdfviewer.annotationCollection[i].annotationSelectorSettings.resizerShape = "Circle"; |
| 194 | + pdfviewer.annotationCollection[i].strokeColor = "#0000FF"; |
| 195 | + pdfviewer.annotationCollection[i].thickness = 2; |
| 196 | + pdfviewer.annotationCollection[i].fillColor = "#FFFF00"; |
| 197 | + pdfviewer.annotation.editAnnotation(pdfviewer.annotationCollection[i]); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + }); |
| 202 | +} |
| 203 | + |
| 204 | +{% endhighlight %} |
| 205 | +{% highlight ts tabtitle="Server-Backed" %} |
| 206 | + |
| 207 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer} from '@syncfusion/ej2-pdfviewer'; |
| 208 | + |
| 209 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer); |
| 210 | + |
| 211 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 212 | +pdfviewer.serviceUrl = 'https://document.syncfusion.com/web-services/pdf-viewer/api/pdfviewer/'; |
| 213 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"; |
| 214 | +pdfviewer.appendTo('#PdfViewer'); |
| 215 | + |
| 216 | +let editAreaAnnotation = document.getElementById('editAreaAnnotation'); |
| 217 | +if (editAreaAnnotation) { |
| 218 | + editAreaAnnotation.addEventListener('click', function () { |
| 219 | + if (pdfviewer) { |
| 220 | + for (let i = 0; i < pdfviewer.annotationCollection.length; i++) { |
| 221 | + if (pdfviewer.annotationCollection[i].subject === "Area calculation") { |
| 222 | + pdfviewer.annotationCollection[i].annotationSelectorSettings.resizerShape = "Circle"; |
| 223 | + pdfviewer.annotationCollection[i].strokeColor = "#0000FF"; |
| 224 | + pdfviewer.annotationCollection[i].thickness = 2; |
| 225 | + pdfviewer.annotationCollection[i].fillColor = "#FFFF00"; |
| 226 | + pdfviewer.annotation.editAnnotation(pdfviewer.annotationCollection[i]); |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + }); |
| 231 | +} |
| 232 | + |
| 233 | +{% endhighlight %} |
| 234 | +{% endtabs %} |
| 235 | + |
| 236 | +## Default area settings during initialization |
| 237 | + |
| 238 | +Set default [areaSettings](https://ej2.syncfusion.com/documentation/api/pdfviewer/index-default#areasettings) before creating the control. |
| 239 | + |
| 240 | +{% tabs %} |
| 241 | +{% highlight ts tabtitle="Standalone" %} |
| 242 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer } from '@syncfusion/ej2-pdfviewer'; |
| 243 | + |
| 244 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer); |
| 245 | + |
| 246 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 247 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"; |
| 248 | +pdfviewer.resourceUrl = "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"; |
| 249 | + |
| 250 | +pdfviewer.areaSettings = { fillColor: 'yellow', opacity: 0.6, strokeColor: 'orange' }; |
| 251 | +pdfviewer.appendTo('#PdfViewer'); |
| 252 | +{% endhighlight %} |
| 253 | +{% highlight ts tabtitle="Server-Backed" %} |
| 254 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer } from '@syncfusion/ej2-pdfviewer'; |
| 255 | + |
| 256 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer); |
| 257 | + |
| 258 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 259 | +pdfviewer.serviceUrl = 'https://document.syncfusion.com/web-services/pdf-viewer/api/pdfviewer/'; |
| 260 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"; |
| 261 | + |
| 262 | +pdfviewer.areaSettings = { fillColor: 'yellow', opacity: 0.6, strokeColor: 'orange' }; |
| 263 | +pdfviewer.appendTo('#PdfViewer'); |
| 264 | +{% endhighlight %} |
| 265 | +{% endtabs %} |
| 266 | + |
| 267 | +## Editing scale ratio and unit of the area measurement annotation |
| 268 | + |
| 269 | +The scale ratio and unit of measurement can be modified using the scale ratio option provided in the context menu of the PDF Viewer control. |
| 270 | + |
| 271 | +  |
| 272 | + |
| 273 | +The Units of measurements support for the measurement annotations in the PDF Viewer are |
| 274 | + |
| 275 | +* Inch |
| 276 | +* Millimeter |
| 277 | +* Centimeter |
| 278 | +* Point |
| 279 | +* Pica |
| 280 | +* Feet |
| 281 | + |
| 282 | + |
| 283 | + |
| 284 | +## Setting default scale ratio settings during control initialization |
| 285 | + |
| 286 | +The properties of scale ratio for measurement annotation can be set before creating the control using ScaleRatioSettings as shown in the following code snippet, |
| 287 | + |
| 288 | +{% tabs %} |
| 289 | +{% highlight ts tabtitle="Standalone" %} |
| 290 | + |
| 291 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer} from '@syncfusion/ej2-pdfviewer'; |
| 292 | + |
| 293 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer); |
| 294 | + |
| 295 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 296 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"; |
| 297 | +pdfviewer.resourceUrl = "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"; |
| 298 | +pdfviewer.measurementSettings={scaleRatio: 2, conversionUnit: 'cm', displayUnit: 'cm'}; |
| 299 | +pdfviewer.appendTo('#PdfViewer'); |
| 300 | + |
| 301 | +{% endhighlight %} |
| 302 | +{% highlight ts tabtitle="Server-Backed" %} |
| 303 | + |
| 304 | +import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer} from '@syncfusion/ej2-pdfviewer'; |
| 305 | + |
| 306 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer); |
| 307 | + |
| 308 | +let pdfviewer: PdfViewer = new PdfViewer(); |
| 309 | +pdfviewer.serviceUrl = 'https://document.syncfusion.com/web-services/pdf-viewer/api/pdfviewer/'; |
| 310 | +pdfviewer.documentPath = "https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"; |
| 311 | +pdfviewer.measurementSettings={scaleRatio: 2, conversionUnit: 'cm', displayUnit: 'cm'}; |
| 312 | +pdfviewer.appendTo('#PdfViewer'); |
| 313 | + |
| 314 | +{% endhighlight %} |
| 315 | +{% endtabs %} |
| 316 | + |
| 317 | +[View Sample on GitHub](https://github.com/SyncfusionExamples/typescript-pdf-viewer-examples/tree/master) |
0 commit comments