{
  "$schema": "https://ai.syncfusion.com/schema/registry-item.json",
  "name": "pdf-viewer-programmatic-annotations",
  "framework": "react",
  "type": "registry:component",
  "component": "PdfViewer",
  "variant": "programmatic-annotations",
  "dependencies": [
    "@syncfusion/ej2-react-dropdowns",
    "@syncfusion/ej2-react-pdfviewer"
  ],
  "description": "PDF viewer adds selected text, shape, measurement, stamp, and free-text annotations to the current page using a type selector.",
  "meta": {
    "essentialStudioRelease": "2026 Volume 3 (v35.1.37)",
    "license": "commercial-or-community",
    "licenseNote": "The Syncfusion package is licensed. The composition in this file is source you own and edit. See https://ai.syncfusion.com/licensing.md",
    "requiresLicenseKey": true,
    "requiresServerEndpoint": false,
    "platformIndex": "https://ai.syncfusion.com/react/llms.txt",
    "skillPack": "syncfusion/react-ui-components-skills",
    "docs": "https://ai.syncfusion.com/gallery/react/ts/pdf-viewer-programmatic-annotations.md"
  },
  "files": [
    {
      "target": "src/components/pdf-viewer-programmatic-annotations/PdfViewer.tsx",
      "content": "import { useRef, useState } from 'react';\nimport { DropDownListComponent } from '@syncfusion/ej2-react-dropdowns';\nimport {\n    Annotation, BookmarkView, DynamicStampItem, FormDesigner, FormFields, Inject, LinkAnnotation,\n    Magnification, Navigation, PageOrganizer, PdfViewerComponent, Print, TextSearch, TextSelection,\n    ThumbnailView, Toolbar\n} from '@syncfusion/ej2-react-pdfviewer';\n\ntype AnnotationChoice =\n    | 'Highlight' | 'Underline' | 'Strikethrough' | 'Squiggly'\n    | 'Line' | 'Arrow' | 'Rectangle' | 'Circle' | 'Polygon'\n    | 'Distance' | 'Perimeter' | 'Area' | 'Radius' | 'Volume'\n    | 'StickyNotes' | 'Ink' | 'Stamp' | 'CustomStamp' | 'FreeText';\n\ntype AnnotationSettings = NonNullable<Parameters<PdfViewerComponent['annotation']['addAnnotation']>[1]>;\n\nconst annotationTypes: { value: AnnotationChoice; label: string }[] = [\n    { value: 'Highlight', label: 'Highlight' },\n    { value: 'Underline', label: 'Underline' },\n    { value: 'Strikethrough', label: 'Strikethrough' },\n    { value: 'Squiggly', label: 'Squiggly' },\n    { value: 'Line', label: 'Line' },\n    { value: 'Arrow', label: 'Arrow' },\n    { value: 'Rectangle', label: 'Rectangle' },\n    { value: 'Circle', label: 'Circle' },\n    { value: 'Polygon', label: 'Polygon' },\n    { value: 'Distance', label: 'Distance' },\n    { value: 'Perimeter', label: 'Perimeter' },\n    { value: 'Area', label: 'Area' },\n    { value: 'Radius', label: 'Radius' },\n    { value: 'Volume', label: 'Volume' },\n    { value: 'StickyNotes', label: 'Sticky Notes' },\n    { value: 'Ink', label: 'Ink' },\n    { value: 'Stamp', label: 'Stamp' },\n    { value: 'CustomStamp', label: 'Custom Stamp' },\n    { value: 'FreeText', label: 'Free Text' }\n];\n\nconst annotationTypeFields = { text: 'label', value: 'value' };\nconst 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>';\n\nfunction ProgrammaticAnnotations() {\n    const viewerRef = useRef<PdfViewerComponent | null>(null);\n    const [isDocumentLoaded, setIsDocumentLoaded] = useState(false);\n    const [annotationType, setAnnotationType] = useState<AnnotationChoice>('Highlight');\n    const annotationCount = useRef(0);\n\n    const getNextPosition = () => {\n        const index = annotationCount.current++ % 16;\n        return {\n            x: 100 + (index % 4) * 115,\n            y: 120 + Math.floor(index / 4) * 75\n        };\n    };\n\n    const addSelectedAnnotation = () => {\n        const viewer = viewerRef.current;\n        if (!viewer) {\n            return;\n        }\n\n        const offset = getNextPosition();\n        const pageNumber = viewer.currentPageNumber;\n        const baseSettings = { offset, pageNumber, author: 'Guest', opacity: 1 };\n        const endPoint = { x: offset.x + 120, y: offset.y + 45 };\n        let settings: object = baseSettings;\n\n        switch (annotationType) {\n            case 'Highlight':\n            case 'Underline':\n            case 'Strikethrough':\n            case 'Squiggly':\n                settings = {\n                    ...baseSettings,\n                    bounds: [{ x: offset.x, y: offset.y, width: 180, height: 16 }],\n                    color: annotationType === 'Highlight' ? '#FFDF56' : '#FF0000'\n                };\n                break;\n            case 'Line':\n            case 'Arrow':\n            case 'Distance':\n                settings = {\n                    ...baseSettings,\n                    offset,\n                    vertexPoints: [offset, endPoint],\n                    lineHeadEndStyle: annotationType === 'Arrow' ? 'Closed' : 'None'\n                };\n                break;\n            case 'Rectangle':\n                settings = { ...baseSettings, width: 100, height: 70, strokeColor: '#2563eb' };\n                break;\n            case 'Circle':\n            case 'Radius':\n                settings = { ...baseSettings, width: 80, height: 80, strokeColor: '#2563eb' };\n                break;\n            case 'Polygon':\n                settings = {\n                    ...baseSettings,\n                    vertexPoints: [\n                        offset,\n                        { x: offset.x + 45, y: offset.y - 20 },\n                        { x: offset.x + 90, y: offset.y + 10 },\n                        { x: offset.x + 75, y: offset.y + 60 },\n                        { x: offset.x + 15, y: offset.y + 60 },\n                        offset\n                    ],\n                    width: 90,\n                    height: 80,\n                    strokeColor: '#2563eb'\n                };\n                break;\n            case 'Perimeter':\n                settings = {\n                    ...baseSettings,\n                    vertexPoints: [\n                        offset,\n                        { x: offset.x + 75, y: offset.y },\n                        { x: offset.x + 80, y: offset.y + 55 }\n                    ],\n                    strokeColor: '#2563eb'\n                };\n                break;\n            case 'Area':\n                settings = {\n                    ...baseSettings,\n                    vertexPoints: [\n                        offset,\n                        { x: offset.x + 90, y: offset.y },\n                        { x: offset.x + 45, y: offset.y + 55 },\n                        offset\n                    ],\n                    width: 90,\n                    height: 55,\n                    fillColor: '#dbeafe',\n                    strokeColor: '#2563eb'\n                };\n                break;\n            case 'Volume':\n                settings = {\n                    ...baseSettings,\n                    vertexPoints: [\n                        offset,\n                        { x: offset.x, y: offset.y + 55 },\n                        { x: offset.x + 75, y: offset.y + 55 },\n                        { x: offset.x + 75, y: offset.y },\n                        offset,\n                        { x: offset.x + 25, y: offset.y - 20 },\n                        { x: offset.x + 100, y: offset.y - 20 },\n                        { x: offset.x + 100, y: offset.y + 35 },\n                        { x: offset.x + 75, y: offset.y + 55 }\n                    ],\n                    width: 100,\n                    height: 75,\n                    strokeColor: '#2563eb'\n                };\n                break;\n            case 'StickyNotes':\n                settings = { ...baseSettings, subject: 'Programmatic review note' };\n                break;\n            case 'Ink':\n                settings = {\n                    ...baseSettings,\n                    width: 100,\n                    height: 45,\n                    strokeColor: '#2563eb',\n                    path: JSON.stringify([\n                        { command: 'M', x: offset.x, y: offset.y + 35 },\n                        { command: 'L', x: offset.x + 25, y: offset.y + 5 },\n                        { command: 'L', x: offset.x + 50, y: offset.y + 35 },\n                        { command: 'L', x: offset.x + 75, y: offset.y + 5 }\n                    ])\n                };\n                break;\n            case 'Stamp':\n                viewer.annotation.addAnnotation(\n                    'Stamp',\n                    { ...baseSettings, width: 140, height: 55 } as AnnotationSettings,\n                    DynamicStampItem.Approved\n                );\n                return;\n            case 'CustomStamp':\n                viewer.annotation.addAnnotation('Stamp', {\n                    ...baseSettings,\n                    width: 150,\n                    height: 50,\n                    customStamps: [{\n                        customStampName: 'Reviewed',\n                        customStampImageSource: `data:image/svg+xml,${encodeURIComponent(customStampSvg)}`\n                    }]\n                } as AnnotationSettings);\n                return;\n            case 'FreeText':\n                settings = {\n                    ...baseSettings,\n                    width: 190,\n                    height: 32,\n                    fontSize: 14,\n                    fontColor: '#1f2937',\n                    defaultText: 'Added programmatically'\n                };\n                break;\n        }\n\n        viewer.annotation.addAnnotation(annotationType, settings as AnnotationSettings);\n    };\n\n    return (\n        <div className='control-section'>\n            <div\n                role='group'\n                aria-label='Programmatic annotation controls'\n                style={{ display: 'flex', alignItems: 'flex-end', gap: '12px', marginBottom: '12px' }}\n            >\n                <div style={{ width: '220px' }}>\n                    <label htmlFor='annotation-type' style={{ display: 'block', marginBottom: '4px' }}>\n                        Annotation Type\n                    </label>\n                    <DropDownListComponent\n                        id='annotation-type'\n                        dataSource={annotationTypes}\n                        fields={annotationTypeFields}\n                        value={annotationType}\n                        placeholder='Select annotation type'\n                        floatLabelType='Never'\n                        change={(event) => {\n                            if (typeof event.value === 'string') {\n                                setAnnotationType(event.value as AnnotationChoice);\n                            }\n                        }}\n                        enabled={isDocumentLoaded}\n                        width='100%'\n                    />\n                </div>\n                <button\n                    className='e-btn e-primary'\n                    type='button'\n                    onClick={addSelectedAnnotation}\n                    disabled={!isDocumentLoaded}\n                >\n                    Add Annotation\n                </button>\n            </div>\n            <p role='status'>\n                {isDocumentLoaded\n                    ? 'Adds the selected annotation to the currently displayed page.'\n                    : 'Loading PDF document...'}\n            </p>\n            <PdfViewerComponent\n                id='container'\n                ref={viewerRef}\n                documentPath='https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf'\n                resourceUrl='https://cdn.syncfusion.com/ej2/35.1.37/dist/ej2-pdfviewer-lib'\n                documentLoad={() => setIsDocumentLoaded(true)}\n                style={{ height: '640px' }}\n            >\n                <Inject services={[\n                    Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView,\n                    Print, TextSelection, TextSearch, Annotation, FormFields, FormDesigner, PageOrganizer\n                ]} />\n            </PdfViewerComponent>\n        </div>\n    );\n}\n\nexport default ProgrammaticAnnotations;\n"
    }
  ]
}