# pdf-viewer-programmatic-annotations

> PDF viewer sample selecting annotation types and adding them programmatically.

**Framework:** javascript  **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/javascript/pdf-viewer-programmatic-annotations.json
```

**Install Package(s)**

```bash
npm install @syncfusion/ej2-pdfviewer @syncfusion/ej2-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.html

```html
<div class="control-section">
    <div class="content-wrapper">
        <div id="annotation-controls" role="group" aria-label="Programmatic annotation controls"
            style="display:flex; align-items:flex-end; gap:12px; margin-bottom:12px;">
            <div style="width:220px;">
                <label for="annotationType" style="display:block; margin-bottom:4px;">Annotation Type</label>
                <input id="annotationType" type="text" />
            </div>
            <button id="addAnnotation" class="e-btn e-primary" type="button" disabled>Add Annotation</button>
        </div>
        <p id="annotationStatus" role="status">Loading PDF document...</p>
        <div id="container" style="height:calc(100vh - 280px); width:100%;"></div>
    </div>
</div>
```

### src/components/pdf-viewer-programmatic-annotations/pdf-viewer.ts

```typescript
import { DropDownList } from '@syncfusion/ej2-dropdowns';
import {
    Annotation, BookmarkView, CircleSettings, FormDesigner, FormFields,
    HighlightSettings, InkAnnotationSettings, LinkAnnotation, Magnification, Navigation,
    PageOrganizer, PdfViewer, Print, RectangleSettings, TextSearch, TextSelection,
    ThumbnailView, Toolbar, UnderlineSettings
} from '@syncfusion/ej2-pdfviewer';
import type { AnnotationType } from '@syncfusion/ej2-pdfviewer';

PdfViewer.Inject(
    Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView,
    Print, TextSelection, TextSearch, Annotation, FormFields, FormDesigner, PageOrganizer
);

type AnnotationChoice =
    | 'Highlight' | 'Underline' | 'Strikethrough' | 'Squiggly'
    | 'Line' | 'Arrow' | 'Rectangle' | 'Circle' | 'Polygon'
    | 'Distance' | 'Perimeter' | 'Area' | 'Radius' | 'Volume'
    | 'StickyNotes' | 'Ink' | 'Stamp' | 'CustomStamp' | 'FreeText';

const annotationTypes = [
    { text: 'Highlight', value: 'Highlight' },
    { text: 'Underline', value: 'Underline' },
    { text: 'Strikethrough', value: 'Strikethrough' },
    { text: 'Squiggly', value: 'Squiggly' },
    { text: 'Line', value: 'Line' },
    { text: 'Arrow', value: 'Arrow' },
    { text: 'Rectangle', value: 'Rectangle' },
    { text: 'Circle', value: 'Circle' },
    { text: 'Polygon', value: 'Polygon' },
    { text: 'Distance', value: 'Distance' },
    { text: 'Perimeter', value: 'Perimeter' },
    { text: 'Area', value: 'Area' },
    { text: 'Radius', value: 'Radius' },
    { text: 'Volume', value: 'Volume' },
    { text: 'Sticky notes', value: 'StickyNotes' },
    { text: 'Ink', value: 'Ink' },
    { text: 'Stamp', value: 'Stamp' },
    { text: 'Custom stamp', value: 'CustomStamp' },
    { text: 'Free text', value: 'FreeText' }
];

type AnnotationSettings = Parameters<Annotation['addAnnotation']>[1];

export function initPdfViewerProgrammaticAnnotations(): void {
    const viewer: PdfViewer = new PdfViewer({
        documentPath: 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf',
        resourceUrl: 'https://cdn.syncfusion.com/ej2/35.1.37/dist/ej2-pdfviewer-lib'
    });
    const dropDown = new DropDownList({
        dataSource: annotationTypes,
        fields: { text: 'text', value: 'value' },
        value: 'Highlight',
        width: '100%'
    });
    const addButton = document.getElementById('addAnnotation') as HTMLButtonElement;
    const status = document.getElementById('annotationStatus') as HTMLElement;
    let annotationCount = 0;

    viewer.documentLoad = () => {
        addButton.disabled = false;
        status.textContent = 'Adds the selected annotation to the currently displayed page.';
    };
    viewer.appendTo('#container');
    dropDown.appendTo('#annotationType');

    addButton.addEventListener('click', () => {
        const annotationType = dropDown.value as AnnotationChoice;
        const index = annotationCount++ % 16;
        const offset = { x: 100 + (index % 4) * 115, y: 120 + Math.floor(index / 4) * 75 };
        const endPoint = { x: offset.x + 120, y: offset.y + 45 };
        const baseSettings = { offset, pageNumber: viewer.currentPageNumber, author: 'Guest', opacity: 1 };
        let settings: AnnotationSettings = baseSettings as AnnotationSettings;

        switch (annotationType) {
            case 'Highlight':
            case 'Underline':
            case 'Strikethrough':
            case 'Squiggly':
                settings = {
                    ...baseSettings,
                    bounds: [{ x: offset.x, y: offset.y, width: 180, height: 16 }],
                    color: annotationType === 'Highlight' ? '#FFDF56' : '#FF0000'
                } as unknown as AnnotationSettings;
                break;
            case 'Line':
            case 'Arrow':
            case 'Distance':
                settings = {
                    ...baseSettings,
                    vertexPoints: [offset, endPoint],
                    lineHeadEndStyle: annotationType === 'Arrow' ? 'Closed' : 'None'
                } as AnnotationSettings;
                break;
            case 'Rectangle':
                settings = { ...baseSettings, width: 100, height: 70, strokeColor: '#2563eb' } as RectangleSettings;
                break;
            case 'Circle':
            case 'Radius':
                settings = { ...baseSettings, width: 80, height: 80, strokeColor: '#2563eb' } as CircleSettings;
                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 }
                    ])
                } as InkAnnotationSettings;
                break;
            case 'FreeText':
                settings = {
                    ...baseSettings, width: 190, height: 32, fontSize: 14,
                    fontColor: '#1f2937', defaultText: 'Added programmatically'
                } as AnnotationSettings;
                break;
            default:
                settings = { ...baseSettings, width: 100, height: 60 } as AnnotationSettings;
        }

        viewer.annotation.addAnnotation(annotationType as AnnotationType, settings);
    });
}

```
