# pdf-viewer-programmatic-annotations

> Programmatically add text, shape, measurement, stamp, and ink annotations to the current PDF page.

**Framework:** angular  **Component:** PdfViewer  **Variant:** programmatic-annotations

## Get this item

**If you are an agent, fetch the JSON.** Source is inlined, so one request is enough and no tooling is required:

```
GET https://ai.syncfusion.com/r/angular/pdf-viewer-programmatic-annotations.json
```

**Install Package(s)**

```bash
npm install @syncfusion/ej2-angular-pdfviewer @syncfusion/ej2-angular-dropdowns
```

**Notes**

- Syncfusion release: 2026 Volume 3 (v35.1.37)
- The Syncfusion package is licensed. The composition in this file is source you own and edit. See https://ai.syncfusion.com/licensing.md

## Source files

### src/components/pdf-viewer-programmatic-annotations/pdf-viewer.component.html

```html
<div class="control-section">
    <div role="group" aria-label="Programmatic annotation controls"
        style="display:flex;align-items:flex-end;flex-wrap:wrap;gap:12px;margin-bottom:12px">
        <div style="width:220px">
            <label for="annotation-type" style="display:block;margin-bottom:4px">Annotation Type</label>
            <ejs-dropdownlist id="annotation-type" [dataSource]="annotationTypes" [fields]="annotationTypeFields"
                [value]="annotationType" placeholder="Select annotation type" [enabled]="isDocumentLoaded"
                (change)="onAnnotationTypeChange($event)">
            </ejs-dropdownlist>
        </div>
        <button class="e-btn e-primary" type="button" (click)="addSelectedAnnotation()"
            [disabled]="!isDocumentLoaded">
            Add Annotation
        </button>
    </div>
    <p role="status">
        {{ isDocumentLoaded ? 'Adds the selected annotation to the currently displayed page.' : 'Loading PDF document...' }}
    </p>
    <ejs-pdfviewer #pdfviewer id="programmatic-annotations-viewer" [documentPath]="document"
        [resourceUrl]="resource" (documentLoad)="onDocumentLoad()" style="height:640px;display:block">
    </ejs-pdfviewer>
</div>

```

### src/components/pdf-viewer-programmatic-annotations/pdf-viewer.component.ts

```typescript
import { Component, ViewChild } from '@angular/core';
import {
    AnnotationService,
    BookmarkViewService,
    DynamicStampItem,
    FormDesignerService,
    FormFieldsService,
    LinkAnnotationService,
    MagnificationService,
    NavigationService,
    PageOrganizerService,
    PdfViewerComponent,
    PdfViewerModule,
    PrintService,
    TextSearchService,
    TextSelectionService,
    ThumbnailViewService,
    ToolbarService
} from '@syncfusion/ej2-angular-pdfviewer';
import { ChangeEventArgs, DropDownListModule } from '@syncfusion/ej2-angular-dropdowns';

type AnnotationType =
    | 'Highlight' | 'Underline' | 'Strikethrough' | 'Squiggly'
    | 'Line' | 'Arrow' | 'Rectangle' | 'Circle' | 'Polygon'
    | 'Distance' | 'Perimeter' | 'Area' | 'Radius' | 'Volume'
    | 'StickyNotes' | 'Ink' | 'Stamp' | 'CustomStamp' | 'FreeText';
type AnnotationSettings = NonNullable<Parameters<PdfViewerComponent['annotation']['addAnnotation']>[1]>;

@Component({
    selector: 'control-content',
    templateUrl: 'pdf-viewer.component.html',
    providers: [
        LinkAnnotationService, BookmarkViewService, MagnificationService, ThumbnailViewService,
        ToolbarService, NavigationService, TextSearchService, TextSelectionService, PrintService,
        AnnotationService, FormFieldsService, FormDesignerService, PageOrganizerService
    ],
    standalone: true,
    imports: [PdfViewerModule, DropDownListModule]
})
export class ProgrammaticAnnotationsComponent {
    @ViewChild('pdfviewer')
    public pdfviewerControl!: PdfViewerComponent;

    public readonly resource = 'https://cdn.syncfusion.com/ej2/35.1.37/dist/ej2-pdfviewer-lib';
    public readonly document = 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf';
    public readonly annotationTypes: { value: AnnotationType; label: string }[] = [
        { value: 'Highlight', label: 'Highlight' },
        { value: 'Underline', label: 'Underline' },
        { value: 'Strikethrough', label: 'Strikethrough' },
        { value: 'Squiggly', label: 'Squiggly' },
        { value: 'Line', label: 'Line' },
        { value: 'Arrow', label: 'Arrow' },
        { value: 'Rectangle', label: 'Rectangle' },
        { value: 'Circle', label: 'Circle' },
        { value: 'Polygon', label: 'Polygon' },
        { value: 'Distance', label: 'Distance' },
        { value: 'Perimeter', label: 'Perimeter' },
        { value: 'Area', label: 'Area' },
        { value: 'Radius', label: 'Radius' },
        { value: 'Volume', label: 'Volume' },
        { value: 'StickyNotes', label: 'Sticky Notes' },
        { value: 'Ink', label: 'Ink' },
        { value: 'Stamp', label: 'Stamp' },
        { value: 'CustomStamp', label: 'Custom Stamp' },
        { value: 'FreeText', label: 'Free Text' }
    ];
    public readonly annotationTypeFields = { text: 'label', value: 'value' };
    public annotationType: AnnotationType = 'Highlight';
    public isDocumentLoaded = false;
    private annotationCount = 0;
    private readonly customStampSvg = '<svg xmlns="http://www.w3.org/2000/svg" width="180" height="60" viewBox="0 0 180 60"><rect x="2" y="2" width="176" height="56" rx="4" fill="white" stroke="#2563eb" stroke-width="4"/><text x="90" y="38" text-anchor="middle" font-family="Arial" font-size="21" font-weight="bold" fill="#2563eb">REVIEWED</text></svg>';

    public onDocumentLoad(): void {
        this.isDocumentLoaded = true;
    }

    public onAnnotationTypeChange(event: ChangeEventArgs): void {
        if (typeof event.value === 'string') {
            this.annotationType = event.value as AnnotationType;
        }
    }

    public addSelectedAnnotation(): void {
        if (!this.isDocumentLoaded) {
            return;
        }

        const index = this.annotationCount++ % 16;
        const offset = { x: 100 + (index % 4) * 115, y: 120 + Math.floor(index / 4) * 75 };
        const pageNumber = this.pdfviewerControl.currentPageNumber;
        const baseSettings = { offset, pageNumber, author: 'Guest', opacity: 1 };
        const endPoint = { x: offset.x + 120, y: offset.y + 45 };
        let settings: object = baseSettings;

        switch (this.annotationType) {
            case 'Highlight':
            case 'Underline':
            case 'Strikethrough':
            case 'Squiggly':
                settings = {
                    ...baseSettings,
                    bounds: [{ x: offset.x, y: offset.y, width: 180, height: 16 }],
                    color: this.annotationType === 'Highlight' ? '#FFDF56' : '#FF0000'
                };
                break;
            case 'Line':
            case 'Arrow':
            case 'Distance':
                settings = {
                    ...baseSettings,
                    vertexPoints: [offset, endPoint],
                    lineHeadEndStyle: this.annotationType === 'Arrow' ? 'Closed' : 'None'
                };
                break;
            case 'Rectangle':
                settings = { ...baseSettings, width: 100, height: 70, strokeColor: '#2563eb' };
                break;
            case 'Circle':
            case 'Radius':
                settings = { ...baseSettings, width: 80, height: 80, strokeColor: '#2563eb' };
                break;
            case 'Polygon':
                settings = {
                    ...baseSettings,
                    vertexPoints: [
                        offset,
                        { x: offset.x + 45, y: offset.y - 20 },
                        { x: offset.x + 90, y: offset.y + 10 },
                        { x: offset.x + 75, y: offset.y + 60 },
                        { x: offset.x + 15, y: offset.y + 60 },
                        offset
                    ],
                    width: 90,
                    height: 80,
                    strokeColor: '#2563eb'
                };
                break;
            case 'Perimeter':
                settings = {
                    ...baseSettings,
                    vertexPoints: [offset, { x: offset.x + 75, y: offset.y }, { x: offset.x + 80, y: offset.y + 55 }],
                    strokeColor: '#2563eb'
                };
                break;
            case 'Area':
                settings = {
                    ...baseSettings,
                    vertexPoints: [
                        offset,
                        { x: offset.x + 90, y: offset.y },
                        { x: offset.x + 45, y: offset.y + 55 },
                        offset
                    ],
                    width: 90,
                    height: 55,
                    fillColor: '#dbeafe',
                    strokeColor: '#2563eb'
                };
                break;
            case 'Volume':
                settings = {
                    ...baseSettings,
                    vertexPoints: [
                        offset,
                        { x: offset.x, y: offset.y + 55 },
                        { x: offset.x + 75, y: offset.y + 55 },
                        { x: offset.x + 75, y: offset.y },
                        offset,
                        { x: offset.x + 25, y: offset.y - 20 },
                        { x: offset.x + 100, y: offset.y - 20 },
                        { x: offset.x + 100, y: offset.y + 35 },
                        { x: offset.x + 75, y: offset.y + 55 }
                    ],
                    width: 100,
                    height: 75,
                    strokeColor: '#2563eb'
                };
                break;
            case 'StickyNotes':
                settings = { ...baseSettings, subject: 'Programmatic review note' };
                break;
            case 'Ink':
                settings = {
                    ...baseSettings,
                    width: 100,
                    height: 45,
                    strokeColor: '#2563eb',
                    path: JSON.stringify([
                        { command: 'M', x: offset.x, y: offset.y + 35 },
                        { command: 'L', x: offset.x + 25, y: offset.y + 5 },
                        { command: 'L', x: offset.x + 50, y: offset.y + 35 },
                        { command: 'L', x: offset.x + 75, y: offset.y + 5 }
                    ])
                };
                break;
            case 'Stamp':
                this.pdfviewerControl.annotation.addAnnotation(
                    'Stamp',
                    { ...baseSettings, width: 140, height: 55 } as AnnotationSettings,
                    DynamicStampItem.Approved
                );
                return;
            case 'CustomStamp':
                this.pdfviewerControl.annotation.addAnnotation('Stamp', {
                    ...baseSettings,
                    width: 150,
                    height: 50,
                    customStamps: [{
                        customStampName: 'Reviewed',
                        customStampImageSource: `data:image/svg+xml,${encodeURIComponent(this.customStampSvg)}`
                    }]
                } as AnnotationSettings);
                return;
            case 'FreeText':
                settings = {
                    ...baseSettings,
                    width: 190,
                    height: 32,
                    fontSize: 14,
                    fontColor: '#1f2937',
                    defaultText: 'Added programmatically'
                };
                break;
        }

        this.pdfviewerControl.annotation.addAnnotation(
            this.annotationType as Parameters<PdfViewerComponent['annotation']['addAnnotation']>[0],
            settings as AnnotationSettings
        );
    }
}

```
