PDF.js Express Plusplay_arrow

Professional PDF.js Viewing & Annotations - Try for free

side menu

Get Started

play_arrow

Learn more

play_arrow

Common use cases

play_arrow

Open a document

play_arrow

Save a document

play_arrow

Viewer

play_arrow

UI Customization

play_arrow

Annotations

play_arrow

Collaboration

play_arrow

Forms

play_arrow

Signature

play_arrow

Searching

play_arrow

Measurement

play_arrow

Compare

play_arrow

Advanced Capabilities

play_arrow

PDF.js Express REST API

play_arrow

Migration Guides

play_arrow

Create custom annotation styles

The following features are available in:

check

PDF.js Express Viewer

help_outline

PDF.js Express Viewer is a free viewer with limited capabilities compared to PDF.js Express Plus

check

PDF.js Express Plus

help_outline

PDF.js Express Plus is a commercial PDF SDK for viewing, annotating, signing, form filling and more

An annotation's default style is often governed by a tool associated with it. For example, a tool will set stroke color, stroke thickness, fill color, text color, font size and opacity of the annotation, which becomes their 'default' style. To change these properties, you can use the setStyles function on the tool objects.

WebViewer({ ... }, viewerElement)
  .then(instance => {
    const { docViewer, Annotations } = instance;

    docViewer.getTool('AnnotationCreateTextHighlight').setStyles({
      StrokeColor: new Annotations.Color(0, 221, 255)
    });

    docViewer.getTool('AnnotationCreateFreeText').setStyles({
      StrokeThickness: 5,
      StrokeColor: new Annotations.Color(0, 0, 255),
      TextColor: new Annotations.Color(0, 0, 0),
      FontSize: '20pt'
    });
  });
WebViewer({ ... }, viewerElement)
  .then(instance => {
    const { documentViewer, Annotations } = instance.Core;

    documentViewer.getTool('AnnotationCreateTextHighlight').setStyles({
      StrokeColor: new Annotations.Color(0, 221, 255)
    });

    documentViewer.getTool('AnnotationCreateFreeText').setStyles({
      StrokeThickness: 5,
      StrokeColor: new Annotations.Color(0, 0, 255),
      TextColor: new Annotations.Color(0, 0, 0),
      FontSize: '20pt'
    });
  });

You can also update the styles of the annotation directly. This method is often used when creating annotations programatically.

WebViewer({...}).then(instance => {

  const { Annotations, docViewer, annotManager } = instance;

  docViewer.on('documentLoaded', () => {
    const annot = new Annotations.RectangleAnnotation();
    annot.Width = 100;
    annot.Height = 50;
    annot.X = 50;
    annot.Y = 50;
    annot.FillColor = new Annotations.Color(255, 0, 0);
    annot.StrokeColor = new Annotations.Color(0, 0, 0);
    annot.StrokeThickness = 5;

    annotManager.addAnnotation(annot);
  })
})
WebViewer({...}).then(instance => {

  const { 
    Annotations, 
    documentViewer, 
    annotationManager 
  } = instance.Core;

  documentViewer.addEventListener('documentLoaded', () => {
    const annot = new Annotations.RectangleAnnotation();
    annot.Width = 100;
    annot.Height = 50;
    annot.X = 50;
    annot.Y = 50;
    annot.FillColor = new Annotations.Color(255, 0, 0);
    annot.StrokeColor = new Annotations.Color(0, 0, 0);
    annot.StrokeThickness = 5;

    annotationManager.addAnnotation(annot);
  })
})

The code above adds a red, 100 x 50 rectangle with a 5pt black border. The style properties that are available to be set at listed in the API docs. Make sure to check what properties exist on the classes the annotation extends as well.