Skip to content

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Add Annotations to PDF ​

This sample shows how to set the annotations (including markup, notes, ink, free text, shapes, stamp, and so on), and delete them

javascript
// Import the JS file of ComPDFKit Web Demo.
import ComPDFKitViewer from "@compdfkit_pdf_sdk/webviewer";

const viewer = document.getElementById('webviewer');
ComPDFKitViewer({
  path: '/',
  initialDoc: 'Your PDF Url',
  licenseKey: 'Input your license here'
}, viewer)
.then((instance) => {
  const { documentViewer } = instance.Core;
  documentViewer.addEventListener('documentLoaded', () => {
    console.log('ComPDFKit Web Demo loaded');

    // Sample1: Create annotations.
    CreateAnnots(instance.Core);

    // Sample2: Delete Annotations.
    DeleteAnnotations(instance.Core);
  })
})

function CreateAnnots(core) {
  CreateNoteAnnotation(core);
  CreateLinkAnnotation(core);
  CreateFreetextAnnotation(core);
  CreateShapeAnnotation(core);
  CreateMarkupAnnotation(core);
  CreateInkAnnotation(core);
  CreateStampAnnotation(core);
}

// Create note annotation.
function CreateNoteAnnotation({ annotationManager, Annotations }) {
  const text = new Annotations.StickyAnnotation({
    PageNumber: 1,
    X: 100,
    Y: 30,
    Width: 24,
    Height: 24,
    StrokeColor: new Annotations.Color(255, 0, 0),
    Opacity: 1
  });
  text.setContents('test');
  annotationManager.addAnnotation(text);
}

// Create link annotation.
function CreateLinkAnnotation({ annotationManager, Annotations, Actions }) {
  // Add a hyperlink.
  const link = new Annotations.Link({
    PageNumber: 1,
    X: 92,
    Y: 200,
    Width: 75,
    Height: 30
  });
  link.addAction('U', new Actions.URI({ uri: 'https://example.com' }));
  annotationManager.addAnnotation(link);

  // Add a intra-document link.
  const pageLink = new Annotations.Link({
    PageNumber: 1,
    X: 92,
    Y: 200,
    Width: 75,
    Height: 30
  });
  pageLink.addAction('U', new Actions.GoTo({ dest: { page: 2 } }));
  annotationManager.addAnnotation(pageLink);
}

// Create freetext annotation.
function CreateFreetextAnnotation({ annotationManager, Annotations }) {
  const freeText = new Annotations.FreeTextAnnotation({
    PageNumber: 1,
    X: 100,
    Y: 200,
    Width: 60,
    Height: 40,
    TextColor: new Annotations.Color(0, 0, 0),
    FontSize: '16pt',
    Font: 'Helvetica',
    Opacity: 1,
    TextAlign: 'left'
  });
  freeText.setContents('test');
  annotationManager.addAnnotation(freeText);
}

// Create shape annotations.
function CreateShapeAnnotation({ annotationManager, Annotations }) {
  const red = new Annotations.Color(255, 0, 0);

  // Square.
  const square = new Annotations.RectangleAnnotation({
    PageNumber: 1,
    X: 50,
    Y: 50,
    Width: 50,
    Height: 50,
    StrokeThickness: 2,
    Opacity: 1,
    FillColor: null, // No fill.
    StrokeColor: red,
    Style: 'solid'
  });
  annotationManager.addAnnotation(square);

  // Circle.
  const circle = new Annotations.EllipseAnnotation({
    PageNumber: 1,
    X: 50,
    Y: 50,
    Width: 50,
    Height: 50,
    StrokeThickness: 2,
    Opacity: 1,
    FillColor: null, // No fill.
    StrokeColor: red,
    Style: 'solid'
  });
  annotationManager.addAnnotation(circle);

  // Line.
  const line = new Annotations.LineAnnotation({
    PageNumber: 1,
    StrokeThickness: 2,
    Opacity: 1,
    StrokeColor: red,
    Style: 'solid'
  });
  line.setStartPoint(50, 50);
  line.setEndPoint(100, 100);
  annotationManager.addAnnotation(line);

  // Polygon.
  const polygon = new Annotations.PolygonAnnotation({
    PageNumber: 1,
    StrokeThickness: 2,
    Opacity: 1,
    FillColor: null, // No fill.
    StrokeColor: red,
    Style: 'solid'
  });
  polygon.setPath([
    new Annotations.Point(156, 328),
    new Annotations.Point(230, 302),
    new Annotations.Point(300, 362),
    new Annotations.Point(310, 436),
    new Annotations.Point(180, 447),
    new Annotations.Point(114, 395)
  ]);
  annotationManager.addAnnotation(polygon);

  // Multiline.
  const polyline = new Annotations.PolylineAnnotation({
    PageNumber: 1,
    StrokeThickness: 2,
    Opacity: 1,
    StrokeColor: red,
    Style: 'solid'
  });
  polyline.setPath([
    new Annotations.Point(130, 156),
    new Annotations.Point(202, 120),
    new Annotations.Point(263, 223),
    new Annotations.Point(157, 232)
  ]);
  annotationManager.addAnnotation(polyline);

  // Arc.
  const arc = new Annotations.ArcAnnotation({
    PageNumber: 1,
    StrokeThickness: 2,
    Opacity: 1,
    StrokeColor: red
  });
  arc.setPath([
    new Annotations.Point(92, 225),
    new Annotations.Point(233, 169),
    new Annotations.Point(143, 98)
  ]);
  annotationManager.addAnnotation(arc);
}

// Create markup annotation.
function CreateMarkupAnnotation({ annotationManager, Annotations }) {
  // Classes include TextHighlightAnnotation, TextUnderlineAnnotation, TextStrikeoutAnnotation, TextSquigglyAnnotation.
  const highlight = new Annotations.TextHighlightAnnotation({
    PageNumber: 1,
    Opacity: 0.8,
    StrokeColor: new Annotations.Color(255, 0, 0)
  });
  highlight.setQuads([
    new Annotations.Quad(116, 360, 360, 360, 360, 300, 116, 300)
  ]);
  highlight.setContents('test');
  annotationManager.addAnnotation(highlight);
}

// Create ink annotation.
function CreateInkAnnotation({ annotationManager, Annotations }) {
  const ink = new Annotations.FreeHandAnnotation({
    PageNumber: 1,
    StrokeThickness: 2,
    Opacity: 0.8,
    StrokeColor: new Annotations.Color(255, 0, 0)
  });
  ink.setPath([
    new Annotations.Point(9.20, 34),
    new Annotations.Point(9.20, 34),
    new Annotations.Point(9.20, 33),
    new Annotations.Point(9.20, 31),
    new Annotations.Point(10.20, 31),
    new Annotations.Point(11.20, 30),
    new Annotations.Point(12.20, 28),
    new Annotations.Point(13.2, 27),
    new Annotations.Point(13.2, 26),
    new Annotations.Point(15.2, 24),
    new Annotations.Point(17.2, 23),
    new Annotations.Point(22.2, 19)
  ]);
  annotationManager.addAnnotation(ink);
}

// Create stamp annotation.
function CreateStampAnnotation({ annotationManager, Annotations }) {
  // Standard stamp.
  const standardStamp = new Annotations.StampAnnotation({
    PageNumber: 1,
    X: 205,
    Y: 379,
    Width: 230,
    Height: 52
  });
  standardStamp.Icon = 'NotApproved';
  annotationManager.addAnnotation(standardStamp);

  // Text stamp.
  const textStamp = new Annotations.StampAnnotation({
    PageNumber: 1,
    X: 220,
    Y: 367,
    Width: 100,
    Height: 45
  });
  textStamp.setStampText('REVISED', {
    date: '28/07/2023 07:28:28',
    color: 'red',
    shape: 'rectangle'
  });
  annotationManager.addAnnotation(textStamp);
}

// Delete all annotations.
async function DeleteAnnotations({ annotationManager }) {
  annotationManager.deleteAnnotations(annotationManager.getAnnotationsList());
}