Resize and Move Annotations in Angular PDF Viewer

16 Nov 20265 minutes to read

Annotations can be resized and moved after they are added to a document. The PDF Viewer supports these operations both from the built-in UI and programmatically through the editAnnotation API.

Resize annotations

Resize via UI

  1. Add an annotation to the page, or select an existing annotation.
  2. When the annotation is selected, resize handles appear on its corners and edges.
  3. Drag a handle to change the width and height of the annotation.

Resize annotations in PDF Viewer UI

Resize programmatically

The size of an annotation is defined by its bounds. To resize an annotation programmatically, update the width and height of the respective annotation bounds and call the editAnnotation API to apply the change dynamically.

const resizeAnnotation = () => {
  //get recently added annotation
  const annotation =
    viewer.annotationCollection[viewer.annotationCollection.length - 1];
  //programmatically resize the annotation
  annotation.bounds.width += 100;
  annotation.bounds.height += 100;
  viewer.annotation.editAnnotation(annotation);
};

Move annotations

Move via UI

  1. Select an annotation.
  2. Drag the annotation to reposition it within the same page, or drag it to a different page in the document.

Move annotations between pages in PDF Viewer UI

Move programmatically

To move an annotation programmatically, update the relevant property of the annotation and call the editAnnotation API.

  • To move an annotation to a different page, update the pageNumber property of the annotation.
  • To move an annotation within the same page, update the left and top values (or x and y, depending on the bounds object structure) of the annotation’s bounds object.
const moveAnnotation = () => {
  //get recently added annotation
  const annotation =
    viewer.annotationCollection[viewer.annotationCollection.length - 1];
  //programmatically change page number of annotation
  annotation.pageNumber += 1;
  viewer.annotation.editAnnotation(annotation);
};

To move an annotation to a new position on the same page, update the bounds of the annotation instead.

const moveAnnotationInPage = () => {
  //get recently added annotation
  const annotation =
    viewer.annotationCollection[viewer.annotationCollection.length - 1];
  // Update the annotation's coordinates (using left/top or x/y depending on the bounds object)
  annotation.bounds.left += 50;
  annotation.bounds.top += 50;
  viewer.annotation.editAnnotation(annotation);
};

Sample

The following sample shows both the resize and move operations with a single viewer instance.

import { Component } from '@angular/core';
import { PdfViewerModule, ToolbarService, AnnotationService } from '@syncfusion/ej2-angular-pdfviewer';

@Component({
  selector: 'app-root',
  imports: [PdfViewerModule],
  providers: [ToolbarService, AnnotationService],
  template: `
    <button (click)="moveAnnotation()">Move annotation</button>
    <button (click)="resizeAnnotation()">Resize annotation</button>
    <ejs-pdfviewer
      id="pdfViewer"
      [documentPath]="documentPath"
      [resourceUrl]="resourceUrl"
      style="height:650px;display:block">
    </ejs-pdfviewer>
  `
})
export class AppComponent {
  public documentPath: string = 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf';
  public resourceUrl: string = 'https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib';

  getViewer() {
    return (document.getElementById('pdfViewer') as any).ej2_instances[0];
  }

  moveAnnotation() {
    //get recently added annotation
    const annotation = this.getViewer().annotationCollection[
      this.getViewer().annotationCollection.length - 1
    ];
    //programmatically change page number of annotation
    annotation.pageNumber += 1;
    this.getViewer().annotation.editAnnotation(annotation);
  }

  resizeAnnotation() {
    //get recently added annotation
    const annotation = this.getViewer().annotationCollection[
      this.getViewer().annotationCollection.length - 1
    ];
    //programmatically resize the annotation
    annotation.bounds.width += 100;
    annotation.bounds.height += 100;
    this.getViewer().annotation.editAnnotation(annotation);
  }
}

The editAnnotation API requires an existing annotation. Add an annotation to the document before calling the resize or move methods.

See also