Search results

Shape Annotation in Angular PDF Viewer component

22 Jul 2021 / 4 minutes to read

The PDF Viewer control provides the options to add, edit, and delete the shape annotations. The shape annotation types supported in the PDF Viewer control are:

  • Line
  • Arrow
  • Rectangle
  • Circle
  • Polygon

ShapeAnnotation

Adding a shape annotation to the PDF document

Shape annotations can be added to the PDF document using the annotation toolbar.

  • Click the Edit Annotation button in the PDF Viewer toolbar. A toolbar appears below it.
  • Click the Shape Annotation drop-down button. A drop-down pop-up will appear and shows the shape annotations to be added.
  • Select the shape types to be added to the page in the drop-down pop-up. It enables the selected shape annotation mode.
  • You can add the shapes over the pages of the PDF document.

In the pan mode, if the shape annotation mode is entered, the PDF Viewer control will switch to text select mode.

ShapeTool

Refer to the following code sample to switch to the circle annotation mode.

Copied to clipboard
import { ViewChild } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import {
  PdfViewerComponent, LinkAnnotationService, BookmarkViewService, MagnificationService, ThumbnailViewService,
  ToolbarService, NavigationService, TextSearchService, TextSelectionService, PrintService, AnnotationService
} from '@syncfusion/ej2-angular-pdfviewer';

@Component({
  selector: 'app-root',
  // Specifies the template string for the PDF Viewer component.
  template: `<button (click)="addAnnot()">Circle</button>
  <div class="content-wrapper">
  <ejs-pdfviewer id="pdfViewer" [serviceUrl]='service' [documentPath]='document' style="height:640px;display:block"></ejs-pdfviewer>
  </div>`,
  providers: [LinkAnnotationService, BookmarkViewService, MagnificationService, ThumbnailViewService, ToolbarService,
NavigationService, TextSearchService, TextSelectionService, PrintService, AnnotationService]
})
export class AppComponent implements OnInit {
  @ViewChild('pdfviewer')
  public pdfviewerControl: PdfViewerComponent;
  public service: string = 'https://ej2services.syncfusion.com/production/web-services/api/pdfviewer';
  public document: string = 'PDF_Succinctly.pdf';
  addAnnot() {
var pdfviewer = (<any>document.getElementById("pdfViewer"))
  .ej2_instances[0];
pdfviewer.annotationModule.setAnnotationMode("Circle");
  }
  ngOnInit(): void {
  }
}

Editing the properties of the shape annotation

The fill color, stroke color, thickness, and opacity of the shape annotation can be edited using the Edit color tool, Edit stroke color tool, Edit thickness tool, and Edit opacity tool in the annotation toolbar.

Editing fill color

The fill color of the annotation can be edited using the color palette provided in the Edit Color tool.

ShapeFillColor

Editing stroke color

The stroke color of the annotation can be edited using the color palette provided in the Edit Stroke Color tool.

ShapeStrokeColor

Editing thickness

The thickness of the border of the annotation can be edited using the range slider provided in the Edit Thickness tool.

ShapeThickness

Editing opacity

The opacity of the annotation can be edited using the range slider provided in the Edit Opacity tool.

ShapeOpacity

Editing the line properties

The properties of the line shapes such as line and arrow annotations can be edited using the Line Properties window. It can be opened by selecting the Properties option in the context menu that appears on right-clicking the line and arrow annotations.

Refer to the following code sample to set the default annotation settings.

ShapeProperty

Setting default properties during the control initialization

The properties of the shape annotations can be set before creating the control using LineSettings, ArrowSettings, RectangleSettings, CircleSettings, and PolygonSettings.

Copied to clipboard
import { ViewChild } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import {
  PdfViewerComponent, LinkAnnotationService, BookmarkViewService, MagnificationService, ThumbnailViewService,
  ToolbarService, NavigationService, TextSearchService, TextSelectionService, PrintService, AnnotationService
} from '@syncfusion/ej2-angular-pdfviewer';

@Component({
  selector: 'app-root',
  // Specifies the template string for the PDF Viewer component.
  template: `<div class="content-wrapper">
  <ejs-pdfviewer id="pdfViewer" [serviceUrl]='service' [documentPath]='document' [lineSettings]='lineSettings' [arrowSettings]='arrowSettings' [rectangleSettings] = 'rectangleSettings' [circleSettings] = 'circleSettings' [polygonSettings] = 'polygonSettings' style="height:640px;display:block"></ejs-pdfviewer>
  </div>`,
  providers: [LinkAnnotationService, BookmarkViewService, MagnificationService, ThumbnailViewService, ToolbarService,
NavigationService, TextSearchService, TextSelectionService, PrintService, AnnotationService]
})
export class AppComponent implements OnInit {
  @ViewChild('pdfviewer')
  public pdfviewerControl: PdfViewerComponent;
  public service: string = 'https://ej2services.syncfusion.com/production/web-services/api/pdfviewer';
  public document: string = 'PDF_Succinctly.pdf';
  public lineSettings = {fillColor: 'blue', opacity: 0.6, strokeColor: 'green'};
  public arrowSettings = {fillColor: 'green', opacity: 0.6, strokeColor: 'blue'};
  public rectangleSettings = {fillColor: 'yellow', opacity: 0.6, strokeColor: 'orange'};
  public circleSettings = {fillColor: 'orange', opacity: 0.6, strokeColor: 'pink'};
  public polygonSettings = {fillColor: 'pink', opacity: 0.6, strokeColor: 'yellow'};
  ngOnInit(): void {
  }
}