# pdf-viewer-programmatic-annotations

> PDF viewer sample selecting annotation types from a dropdown and adding them programmatically to the document.

**Framework:** vue  **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/vue/pdf-viewer-programmatic-annotations.json
```

**Install Package(s)**

```bash
npm install @syncfusion/ej2-vue-pdfviewer @syncfusion/ej2-vue-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/PdfViewer.vue

```vue
<template>
  <div class="control-section programmatic-annotations">
    <div class="controls">
      <label for="annotation-type">Annotation type</label>
      <DropDownListComponent
        id="annotation-type"
        ref="annotationType"
        :dataSource="annotationTypes"
        :fields="{ text: 'text', value: 'value' }"
        value="Highlight"
        width="220px"
      />
      <button class="e-btn e-primary" type="button" :disabled="!isDocumentLoaded" @click="addAnnotation">
        Add annotation
      </button>
    </div>
    <p role="status">{{ statusMessage }}</p>
    <PdfViewerComponent
      id="pdfViewer"
      ref="pdfviewer"
      :documentPath="documentPath"
      :resourceUrl="resourceUrl"
      :documentLoad="documentLoad"
    />
  </div>
</template>

<script setup lang="ts">
import { provide, ref } from 'vue';
import { DropDownListComponent } from '@syncfusion/ej2-vue-dropdowns';
import {
  Annotation, BookmarkView, FormDesigner, FormFields, LinkAnnotation, Magnification,
  Navigation, PageOrganizer, PdfViewerComponent, Print, TextSearch, TextSelection,
  ThumbnailView, Toolbar
} from '@syncfusion/ej2-vue-pdfviewer';

provide('PdfViewer', [
  Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView,
  Print, TextSelection, TextSearch, Annotation, FormFields, FormDesigner, PageOrganizer
]);

const pdfviewer = ref<InstanceType<typeof PdfViewerComponent> | null>(null);
const annotationType = ref<InstanceType<typeof DropDownListComponent> | null>(null);
const isDocumentLoaded = ref(false);
const statusMessage = ref('Loading PDF document...');
const annotationCount = ref(0);
const documentPath = 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf';
const resourceUrl = 'https://cdn.syncfusion.com/ej2/35.1.37/dist/ej2-pdfviewer-lib';
const annotationTypes = [
  'Highlight', 'Underline', 'Strikethrough', 'Squiggly', 'Line', 'Arrow',
  'Rectangle', 'Circle', 'Polygon', 'Distance', 'Perimeter', 'Area', 'Radius',
  'Volume', 'StickyNotes', 'Ink', 'Stamp', 'FreeText'
].map((value) => ({ text: value, value }));

const documentLoad = (): void => {
  isDocumentLoaded.value = true;
  statusMessage.value = 'Document loaded. Choose an annotation type and add it to the current page.';
};

const addAnnotation = (): void => {
  const viewer = pdfviewer.value?.ej2Instances;
  const type = annotationType.value?.ej2Instances.value as string | undefined;
  if (!viewer || !type) {
    return;
  }

  const index = annotationCount.value++ % 16;
  const offset = { x: 100 + (index % 4) * 115, y: 120 + Math.floor(index / 4) * 75 };
  const end = { x: offset.x + 120, y: offset.y + 45 };
  const base = { offset, pageNumber: viewer.currentPageNumber, author: 'Guest', opacity: 1 };
  let settings: Record<string, unknown> = base;

  if (['Highlight', 'Underline', 'Strikethrough', 'Squiggly'].includes(type)) {
    settings = { ...base, bounds: [{ x: offset.x, y: offset.y, width: 180, height: 16 }], color: type === 'Highlight' ? '#FFDF56' : '#FF0000' };
  } else if (['Line', 'Arrow', 'Distance'].includes(type)) {
    settings = { ...base, vertexPoints: [offset, end], lineHeadEndStyle: type === 'Arrow' ? 'Closed' : 'None' };
  } else if (type === 'Rectangle') {
    settings = { ...base, width: 100, height: 70, strokeColor: '#2563eb' };
  } else if (['Circle', 'Radius'].includes(type)) {
    settings = { ...base, width: 80, height: 80, strokeColor: '#2563eb' };
  } else if (type === 'FreeText') {
    settings = { ...base, width: 190, height: 32, fontSize: 14, fontColor: '#1f2937', defaultText: 'Added programmatically' };
  } else {
    settings = { ...base, width: 100, height: 60 };
  }

  viewer.annotation.addAnnotation(type as never, settings as never);
  statusMessage.value = `${type} annotation added to page ${viewer.currentPageNumber}.`;
};
</script>

<style scoped>
.controls { display: flex; align-items: end; gap: 12px; padding: 8px 0 12px; }
.controls label { font-weight: 600; }
#pdfViewer { height: 640px; }
</style>

```
