EmbedPDF

Your own annotation look

The annotation layer draws each annotation the way the PDF describes it. You can draw some of them with your own component instead: a note that looks like your app’s comment bubble, a stamp that shows live data from your server.

This changes the screen only. The PDF keeps the annotation’s own look, so other PDF apps, printing and downloads show the original:

WhereWhat people see
Your viewerYour component
The saved PDF, other apps, printingThe annotation’s own appearance

Its look, or UI on it#

Ask whether it should zoom and turn with the PDF:

You wantUseIt
A different look: a comment bubble instead of the note icon, a stamp with live dataA rendererIs part of the page: it zooms and turns with it, and sits under the selection handles
A mark that stays on the annotation: a ✓, a status, a count of repliesPinned anchored UIFloats over the pages, upright and the same size at every zoom, exactly where you put it
A card or a menu next to itAnchored UIFloats over the pages, and moves to stay in view

Draw an annotation your way#

Give the layer a list of renderers. Each one says which annotations it draws and with which component:
renderers.vue
<script setup lang="ts">
import { AnnotationLayer, type AnnotationRenderer } from '@embedpdf/vue/annotation';
import CommentBubble from './CommentBubble.vue';

const renderers: AnnotationRenderer[] = [
  { for: (annotation) => annotation.subtype === 'text', component: CommentBubble },
];
</script>

<template>
  <AnnotationLayer :renderers />
</template>

for gets the annotation, with all its fields, so you can pick by kind, name, author or anything else. The first renderer that matches wins; an annotation no renderer matches is drawn as usual.

Your component draws into a frame: a box the layer places, turns and scales exactly like the annotation’s own drawing, also while it’s dragged, resized or turned. Fill it, with width and height at 100%, and draw at the annotation’s 100% size: the layer scales your drawing with the page, text and borders included, as it does the PDF’s own:

bubble.vue
<!-- CommentBubble.vue -->
<script setup lang="ts">
import type { AnnotationRendererProps } from '@embedpdf/vue/annotation';

defineProps<AnnotationRendererProps>();
</script>

<template>
  <!-- It fills its frame (`width` and `height` 100% in your CSS), so the selection
       outline and the click area match it, and it turns with the annotation. -->
  <div :class="['bubble', { 'bubble--hover': hovered }]">
    {{ annotation.author?.[0] ?? '?' }}
  </div>
</template>

The selection outline, the click area and the hover all use the frame, so a look that fills it matches them. A note keeps its size on screen as people zoom in and stays upright when the page is turned, and so does a look that fills its frame. To make the bubble bigger, make the note bigger: a size in points is a third more in pixels at 100% zoom, so a 24-point note is 32 pixels.

Your component gets:
PropWhat it is
annotationThe annotation, as get() returns it.
frameThe box you draw into: its width and height in pixels at the annotation’s 100% size, its rotation on screen in degrees clockwise, and the scale the layer draws it at. Turn your content by -rotation to keep it upright, such as a label on a turned stamp.
nativeThe layer’s own drawing of it, filling the frame. Render it to keep the original look and add to it.
appearanceThe engine’s picture of the annotation as an image (url), or null. native already draws it in place.
hoveredTrue while the pointer is over it.
selectedTrue while it’s selected, such as to make room for the selection handles.
interactiveTrue when what you draw takes the pointer (see below).

Loading live preview…

<script setup lang="ts">
import { ref } from 'vue';
import { DocumentGate, Viewer } from '@embedpdf/vue/runtime';
import type { OpenInput } from '@embedpdf/vue/runtime';
import { Stage, stagePlugin } from '@embedpdf/vue/stage';
import { RenderLayer, renderPlugin } from '@embedpdf/vue/render';
import { interactionPlugin } from '@embedpdf/vue/interaction';
import { AnnotationLayer, annotationPlugin } from '@embedpdf/vue/annotation';
import type { AnnotationRenderer } from '@embedpdf/vue/annotation';
import { localEngine } from '@embedpdf/engine';
import AddNotes from './AddNotes.vue';
import CommentBubble from './CommentBubble.vue';
import TurnButton from './TurnButton.vue';

import '../renderers.css';

const engine = localEngine();
const plugins = [stagePlugin(), renderPlugin(), interactionPlugin(), annotationPlugin()];

const ebook = async (): Promise<OpenInput> => {
  const response = await fetch('https://snippet.embedpdf.com/ebook.pdf');
  return { kind: 'bytes', id: 'ebook', bytes: new Uint8Array(await response.arrayBuffer()) };
};

// Defined once, here: the layer registers each entry.
const RENDERERS: AnnotationRenderer[] = [
  { for: (annotation) => annotation.subtype === 'text', component: CommentBubble },
];
const NONE: AnnotationRenderer[] = [];

const mine = ref(true);
</script>

<template>
  <Viewer
    :engine="engine"
    :plugins="plugins"
    :identity="{ userId: 'u_381', displayName: 'Dana Smith' }"
    :initial-documents="[{ source: ebook }]"
  >
    <DocumentGate>
      <template #fallback><p class="loading">Loading…</p></template>
      <AddNotes />
      <div class="toolbar">
        <div class="segmented" role="group" aria-label="Look">
          <button type="button" :aria-pressed="mine" @click="mine = true">Your look</button>
          <button type="button" :aria-pressed="!mine" @click="mine = false">The PDF's look</button>
        </div>
        <TurnButton />
      </div>
      <Stage class="stage">
        <template #page>
          <RenderLayer />
          <AnnotationLayer :renderers="mine ? RENDERERS : NONE" />
        </template>
      </Stage>
    </DocumentGate>
  </Viewer>
</template>

Try it: the notes are bubbles with their author’s initials. Click one: the selection fits it. Press Turn the view: they stay upright, like the PDF’s own notes. Switch to The PDF’s look: that’s what the saved PDF shows.

People can still select, move, resize and delete the annotation: your component draws, and the viewer keeps handling the pointer. That’s also why it gets no clicks of its own. For that, see Take over the pointer.

Show a card on hover#

A hover card sits next to an annotation while the pointer is over it. It needs two things: the annotation under the pointer, and an anchor that keeps a card attached to it:

hover-card.vue
<!-- StampCard.vue, for the Stage's #overlay slot -->
<script setup lang="ts">
import { Anchored } from '@embedpdf/vue/anchored';
import { useAnnotationAnchor, useAnnotationState } from '@embedpdf/vue/annotation';

const { hovered } = useAnnotationState(); // the annotation under the pointer, or null
const anchor = useAnnotationAnchor(() => hovered.value?.ref ?? null);
</script>

<template>
  <Anchored v-if="hovered?.subtype === 'stamp'" :anchor placement="top">
    <div class="card">Approved by {{ hovered.author }}</div>
  </Anchored>
</template>

Loading live preview…

<script setup lang="ts">
import { DocumentGate, Viewer } from '@embedpdf/vue/runtime';
import type { OpenInput } from '@embedpdf/vue/runtime';
import { Stage, stagePlugin } from '@embedpdf/vue/stage';
import { RenderLayer, renderPlugin } from '@embedpdf/vue/render';
import { interactionPlugin } from '@embedpdf/vue/interaction';
import { AnnotationLayer, annotationPlugin } from '@embedpdf/vue/annotation';
import { localEngine } from '@embedpdf/engine';
import AddAnnotations from './AddAnnotations.vue';
import HoverCard from './HoverCard.vue';

import '../hover-card.css';

const engine = localEngine();
const plugins = [stagePlugin(), renderPlugin(), interactionPlugin(), annotationPlugin()];

const ebook = async (): Promise<OpenInput> => {
  const response = await fetch('https://snippet.embedpdf.com/ebook.pdf');
  return { kind: 'bytes', id: 'ebook', bytes: new Uint8Array(await response.arrayBuffer()) };
};
</script>

<template>
  <Viewer
    :engine="engine"
    :plugins="plugins"
    :identity="{ userId: 'u_381', displayName: 'Dana Smith' }"
    :initial-documents="[{ source: ebook }]"
  >
    <DocumentGate>
      <template #fallback><p class="loading">Loading…</p></template>
      <AddAnnotations />
      <p class="hint">Point at a note or the green rectangle</p>
      <Stage class="stage">
        <template #page>
          <RenderLayer />
          <AnnotationLayer />
        </template>
        <template #overlay><HoverCard /></template>
      </Stage>
    </DocumentGate>
  </Viewer>
</template>

Mount it in the Stage’s #overlay slot, like a menu. The card stays next to the annotation while the user scrolls and zooms, and while the annotation is moved.

useAnnotationAnchor(ref) works for any annotation, not only the hovered one: see the next section.

Pin UI to an annotation#

A ✓ on an approved stamp, a status under it, a count of replies next to a note: UI that belongs to one annotation and stays on it. pinned keeps it exactly where you put it. It doesn’t flip or move to stay in view, and it scrolls away with the annotation:

pinned.vue
<!-- ApprovalStatus.vue -->
<script setup lang="ts">
import { Anchored } from '@embedpdf/vue/anchored';
import { annotationKey, useAnnotationAnchor, type Annotation } from '@embedpdf/vue/annotation';
import { useApproval } from './approvals'; // your own data

const props = defineProps<{ stamp: Annotation }>();

// Pinned under the stamp: it stays there while the stamp moves, and scrolls away with it.
const anchor = useAnnotationAnchor(() => props.stamp.ref);
const approval = useApproval(() => annotationKey(props.stamp.ref));
</script>

<template>
  <Anchored :anchor="anchor" placement="bottom" pinned>
    <div class="status">
      {{ approval.signedOff ? 'Signed off' : 'Waiting' }}
      <button @click="approval.toggle">{{ approval.signedOff ? 'Undo' : 'Sign off' }}</button>
    </div>
  </Anchored>
</template>

Mount one for each annotation that needs it in the Stage’s #overlay slot, from useAnnotationList(). Anchored UI on a page that isn’t on screen isn’t drawn, so a document with hundreds of them costs only the ones in view.

Loading live preview…

<script setup lang="ts">
import { DocumentGate, Viewer } from '@embedpdf/vue/runtime';
import type { OpenInput } from '@embedpdf/vue/runtime';
import { Stage, stagePlugin } from '@embedpdf/vue/stage';
import { RenderLayer, renderPlugin } from '@embedpdf/vue/render';
import { interactionPlugin } from '@embedpdf/vue/interaction';
import { AnnotationLayer, annotationPlugin } from '@embedpdf/vue/annotation';
import { localEngine } from '@embedpdf/engine';
import AddStamps from './AddStamps.vue';
import Approvals from './Approvals.vue';

import '../stamp-status.css';

const engine = localEngine();
const plugins = [stagePlugin(), renderPlugin(), interactionPlugin(), annotationPlugin()];

const ebook = async (): Promise<OpenInput> => {
  const response = await fetch('https://snippet.embedpdf.com/ebook.pdf');
  return { kind: 'bytes', id: 'ebook', bytes: new Uint8Array(await response.arrayBuffer()) };
};
</script>

<template>
  <Viewer :engine="engine" :plugins="plugins" :initial-documents="[{ source: ebook }]">
    <DocumentGate>
      <template #fallback><p class="loading">Loading…</p></template>
      <AddStamps />
      <p class="hint">Sign a stamp off, then move it: its status goes with it.</p>
      <Stage class="stage">
        <template #page>
          <RenderLayer />
          <AnnotationLayer />
        </template>
        <template #overlay><Approvals /></template>
      </Stage>
    </DocumentGate>
  </Viewer>
</template>

Try it: sign the second stamp off, then select a stamp and move it: its status goes with it. The second stamp’s status hangs over the next page, and stays on top.

  1. The stamps are placed like any stamp, so the PDF holds the real ones; see Stamps.
  2. Each one is picked out by its name, which the stamp keeps in every PDF app.
  3. The status sits under the stamp with placement="bottom", and the ✓ on its corner with placement="top-end" and a negative gap. See Where it goes.
  4. The stamp keeps its own look and stays selectable: the buttons are next to it, not in it.

To find your own data for an annotation, key it by annotationKey(annotation.ref), which stays the same after the document is saved and opened again, or by its name when you gave it one.

Take over the pointer#

Sometimes your component must take clicks itself: buttons, links, a text field. Mark the renderer interactive:

interactive.vue
<script setup lang="ts">
import { AnnotationLayer, type AnnotationRenderer } from '@embedpdf/vue/annotation';
import ApprovalWidget from './ApprovalWidget.vue';

const renderers: AnnotationRenderer[] = [
  {
    for: (annotation) => annotation.subtype === 'stamp' && annotation.name === 'Approved',
    component: ApprovalWidget,
    interactive: true,
  },
];
</script>

<template>
  <AnnotationLayer :renderers />
</template>

This is for when the annotation itself is the control, such as a stamp you tick like a checkbox. For buttons next to an annotation, pin them instead: the annotation stays selectable.

While a renderer is interactive, the pointer belongs to your component, and people can’t select, move or delete that annotation with it. Most apps want both: interactive while reading, editable while editing. Pass a function, and the viewer asks it whenever it matters:

interactive-when.vue
<script setup lang="ts">
import { AnnotationLayer, type AnnotationRenderer } from '@embedpdf/vue/annotation';
import ApprovalWidget from './ApprovalWidget.vue';

const renderers: AnnotationRenderer[] = [
  {
    for: (annotation) => annotation.subtype === 'stamp' && annotation.name === 'Approved',
    component: ApprovalWidget,
    interactive: ({ toolId }) => toolId === 'pan', // interactive in reading mode only
  },
];
</script>

<template>
  <AnnotationLayer :renderers />
</template>

Good to know#

Drawing at the size on screen#

A renderer with scale: false draws at the frame’s size on screen instead, and sizes its text and borders itself from frame.scale. Use it for something that must lay itself out at full resolution, such as a canvas.

Hover cards people can use#

A card closes when the pointer leaves the annotation, so the pointer can’t reach a button in it. Keep the card open while the pointer is over the card itself, for example with a short delay before it closes and a pointerenter on the card that cancels it.

Form fields#

A form field’s widgets aren’t annotations: they’re the form’s, and the <FormLayer> draws them, so your renderers never get them. See Forms and annotations.

Other layers#

A renderer replaces the drawing inside the annotation layer. Anything that isn’t tied to one annotation, such as a comments sidebar, belongs in your own UI, fed by useAnnotationList().

Was this page helpful?

Your feedback goes directly to the documentation team.