# pdf-viewer-programmatic-annotations

> PDF viewer adds selected text, shape, measurement, stamp, and free-text annotations to the current page using a type selector.

**Framework:** react  **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/react/js/pdf-viewer-programmatic-annotations.json
```

**Install Package(s)**

```bash
npm install @syncfusion/ej2-react-dropdowns @syncfusion/ej2-react-pdfviewer
```

**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/PdfViewer.jsx

```jsx
import { useRef, useState } from 'react';
import { DropDownListComponent } from '@syncfusion/ej2-react-dropdowns';
import {
    Annotation, BookmarkView, DynamicStampItem, FormDesigner, FormFields, Inject, LinkAnnotation,
    Magnification, Navigation, PageOrganizer, PdfViewerComponent, Print, TextSearch, TextSelection,
    ThumbnailView, Toolbar
} from '@syncfusion/ej2-react-pdfviewer';

const annotationTypes = [
    { 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' }
];

const annotationTypeFields = { text: 'label', value: 'value' };
const 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>';

function ProgrammaticAnnotations() {
    const viewerRef = useRef(null);
    const [isDocumentLoaded, setIsDocumentLoaded] = useState(false);
    const [annotationType, setAnnotationType] = useState('Highlight');
    const annotationCount = useRef(0);

    const getNextPosition = () => {
        const index = annotationCount.current++ % 16;
        return {
            x: 100 + (index % 4) * 115,
            y: 120 + Math.floor(index / 4) * 75
        };
    };

    const addSelectedAnnotation = () => {
        const viewer = viewerRef.current;
        if (!viewer) {
            return;
        }

        const offset = getNextPosition();
        const pageNumber = viewer.currentPageNumber;
        const baseSettings = { offset, pageNumber, author: 'Guest', opacity: 1 };
        const endPoint = { x: offset.x + 120, y: offset.y + 45 };
        let settings = baseSettings;

        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'
                };
                break;
            case 'Line':
            case 'Arrow':
            case 'Distance':
                settings = {
                    ...baseSettings,
                    offset,
                    vertexPoints: [offset, endPoint],
                    lineHeadEndStyle: 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':
                viewer.annotation.addAnnotation(
                    'Stamp',
                    { ...baseSettings, width: 140, height: 55 },
                    DynamicStampItem.Approved
                );
                return;
            case 'CustomStamp':
                viewer.annotation.addAnnotation('Stamp', {
                    ...baseSettings,
                    width: 150,
                    height: 50,
                    customStamps: [{
                        customStampName: 'Reviewed',
                        customStampImageSource: `data:image/svg+xml,${encodeURIComponent(customStampSvg)}`
                    }]
                });
                return;
            case 'FreeText':
                settings = {
                    ...baseSettings,
                    width: 190,
                    height: 32,
                    fontSize: 14,
                    fontColor: '#1f2937',
                    defaultText: 'Added programmatically'
                };
                break;
        }

        viewer.annotation.addAnnotation(annotationType, settings);
    };

    return (
        <div className='control-section'>
            <div
                role='group'
                aria-label='Programmatic annotation controls'
                style={{ display: 'flex', alignItems: 'flex-end', gap: '12px', marginBottom: '12px' }}
            >
                <div style={{ width: '220px' }}>
                    <label htmlFor='annotation-type' style={{ display: 'block', marginBottom: '4px' }}>
                        Annotation Type
                    </label>
                    <DropDownListComponent
                        id='annotation-type'
                        dataSource={annotationTypes}
                        fields={annotationTypeFields}
                        value={annotationType}
                        placeholder='Select annotation type'
                        floatLabelType='Never'
                        change={(event) => {
                            if (typeof event.value === 'string') {
                                setAnnotationType(event.value);
                            }
                        }}
                        enabled={isDocumentLoaded}
                        width='100%'
                    />
                </div>
                <button
                    className='e-btn e-primary'
                    type='button'
                    onClick={addSelectedAnnotation}
                    disabled={!isDocumentLoaded}
                >
                    Add Annotation
                </button>
            </div>
            <p role='status'>
                {isDocumentLoaded
                    ? 'Adds the selected annotation to the currently displayed page.'
                    : 'Loading PDF document...'}
            </p>
            <PdfViewerComponent
                id='container'
                ref={viewerRef}
                documentPath='https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf'
                resourceUrl='https://cdn.syncfusion.com/ej2/35.1.37/dist/ej2-pdfviewer-lib'
                documentLoad={() => setIsDocumentLoaded(true)}
                style={{ height: '640px' }}
            >
                <Inject services={[
                    Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView,
                    Print, TextSelection, TextSearch, Annotation, FormFields, FormDesigner, PageOrganizer
                ]} />
            </PdfViewerComponent>
        </div>
    );
}

export default ProgrammaticAnnotations;

```
