> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/web/get-started/samples/showcase-demo-measurement-tools.md).

# Measurement Tools Showcase Demo Code Sample

Easily calculate area dimensions, measure between lines, or trace perimeters in engineering drawings in browser or app.

{% hint style="info" %}
**Requirements**

*These packages are required to use these features in production. Trial keys have unlimited access to all features*

<a href="/web/get-started/readme.md" class="button primary">Web SDK</a><a href="https://showcase.apryse.com/measurement-tools" class="button primary">Live demo</a>
{% endhint %}

Easily calculate area dimensions, measure between lines or trace perimeters in engineering drawings in browser or app.

This demo allows you to:

* Load PDFs containing engineering drawings
* Add measurement tools and measure objects directly within the document
* Download the updated PDF with your measurements included

### **Implementation steps**

To add measurement tools capability to WebViewer: Step 1: [Get started with WebViewer](/web/get-started/readme.md) in your preferred web stack Step 2: Add the ES6 JavaScript sample code provided in this guide

Once you generate your license key, it will automatically be included in your sample code below.

{% @apryse-license-key/apryse-license-key platform="WEB\_VIEWER" variant="compact" %}

<pre class="language-js" data-line-numbers><code class="lang-js">
// ES6 Compliant Syntax
// GitHub Copilot - GPT-4 Model - August 17, 2025
// File: index.js

import WebViewer from '@pdftron/webviewer';

const licenseKey = '<code class="expression">visitor.claims.wvKey || "YOUR_WEBVIEWER_LICENSE_KEY"</code>';

const DEFAULT_TOOL = 'AnnotationCreateDistanceMeasurement';

const MEASUREMENT_TOOLS = [
  'AnnotationCreateDistanceMeasurement',
  'AnnotationCreatePerimeterMeasurement',
  'AnnotationCreateAreaMeasurement',
  'AnnotationCreateRectangularAreaMeasurement',
  'AnnotationCreateEllipseMeasurement',
  'AnnotationCreateCountMeasurement',
  'AnnotationCreateArcMeasurement',
];

// IMPORTANT:
// The order in this BUTTONS_TEXT array should be similar to the MEASUREMENT_TOOLS array to ensure
// that the buttons correspond to the correct measurement tools when they're created later.
const BUTTONS_TEXT = [
  'Distance',
  'Perimeter',
  'Area',
  'Rectangular Area',
  'Ellipse Area',
  'Count',
  'Arc',
];

const HIDDEN_TOOLBARS = [
  'toolbarGroup-Shapes',
  'toolbarGroup-Edit',
  'toolbarGroup-Insert',
  'toolbarGroup-Forms',
  'toolbarGroup-View',
  'toolbarGroup-Annotate',
  'toolbarGroup-FillAndSign',
  'toolbarGroup-Redact',
];

const MEASUREMENT_TOOLS_DEFAULT_COLORS = [
  [225, 0, 0],
  [0, 255, 0],
  [0, 0, 255],
  [0, 255, 225],
  [255, 0, 255],
  [255, 255, 0],
  [255, 165, 0],
];

const DEFAULT_FONT_SIZE = 16;
const DEFAULT_STROKE_THICKNESS = 2;

// Function to set WebViewer 'loading' state
function setLoading(isLoading) {
  if (isLoading) {
      theInstance.UI.openElements(['loadingModal']);
  } else {
      theInstance.UI.closeElements(['loadingModal']);
  }
}

let initializing = true;
let snapState = false;

function onDocumentLoaded(){
  setLoading(true);
  // initialization logic executed when the very first document is loaded
  if(initializing) {
    initializing = false;
    snapState = true; // default snap state
    setSnapMode({ snap: true, toolName: DEFAULT_TOOL });
    // set snap color and size
    theInstance.Core.annotationManager.setSnapDefaultOptions({
      indicatorColor: '#00a5e4',
      indicatorSize: 18,
      radiusThreshold: 20,
    });

    theInstance.UI.disableElements(HIDDEN_TOOLBARS);
    theInstance.UI.enableFeatures([theInstance.UI.Feature.Measurement]);

    const { documentViewer, annotationManager } = theInstance.Core;
    
    annotationManager.addEventListener('annotationChanged', annotationChanged);

    // update default tool styles
    const Annotations = theInstance.Core.Annotations;
    MEASUREMENT_TOOLS.forEach((tool, index) => {
      const currentTool = documentViewer.getTool(tool);
      currentTool.setStyles({
        StrokeThickness: DEFAULT_STROKE_THICKNESS / documentViewer.getZoomLevel(),
        StrokeColor: new Annotations.Color(...MEASUREMENT_TOOLS_DEFAULT_COLORS[index]),
      });

      if (currentTool.setDrawMode) {
        currentTool.setDrawMode(theInstance.Core.Tools.LineCreateTool.DrawModes.TWO_CLICKS);
      }
    });
    // update font size to be larger
    Annotations.LineAnnotation.prototype['constant']['FONT_SIZE'] =
      DEFAULT_FONT_SIZE / documentViewer.getZoomLevel() + 'px';
    Annotations.LineAnnotation.prototype['constant']['TEXT_COLOR'] = '#FF0000';

    documentViewer.addEventListener('zoomUpdated', zoomUpdated);
  }
  // Wait a couple of seconds to let snapping points completely load
  setTimeout(() => {
    setLoading(false);
    theInstance.UI.setToolbarGroup('toolbarGroup-Measure');
    theInstance.UI.enableTools(MEASUREMENT_TOOLS);
    theInstance.UI.setToolMode(DEFAULT_TOOL);
  }, 2500);
}

const element = document.getElementById('viewer');
let theInstance = null;
const onLoad = async (instance) => {
  theInstance = instance;
  initializing = true;
  theInstance.Core.documentViewer.addEventListener('documentLoaded', () => {
    onDocumentLoaded();
  });
};

function zoomUpdated(zoom) {
  if (!theInstance) return;
  const { Annotations, documentViewer } = theInstance.Core;
  Annotations.LineAnnotation.prototype['constant']['FONT_SIZE'] =
    DEFAULT_FONT_SIZE / zoom + 'px';

  MEASUREMENT_TOOLS.forEach((tool) => {
    documentViewer.getTool(tool).setStyles({
      StrokeThickness: DEFAULT_STROKE_THICKNESS / zoom,
    });
  });
}

function annotationChanged(ann, action, { imported }) {
  console.log('annotationChanged', ann, action, imported);
  if (action === 'add' &#x26;&#x26; !imported &#x26;&#x26; ann.length === 1 &#x26;&#x26; ann[0].Measure) {
    theInstance.UI.openElements(['notesPanel']);
  }
}

// Initialize WebViewer and load default document
WebViewer(
  {
    path: '/lib',
    licenseKey: licenseKey, 
    initialDoc: 'https://apryse.s3.amazonaws.com/public/files/samples/floorplan.pdf',
    fullAPI: true, // Enable full API for measurement tools
    enableFilePicker: true, // Enable file picker to open files. In WebViewer -> menu icon -> Open File
  },
  element
).then((instance) => {
  onLoad(instance);
});
console.log('before activeTool');
let activeTool = DEFAULT_TOOL;

function setActiveHelper(active) {
  activeTool = active;
  theInstance.UI.setToolMode(active);
  setSnapMode({ snap: snapState, toolName: active });
  buttonAddScale.disabled = (activeTool === 'AnnotationCreateCountMeasurement');
}

function setSnapMode({ snap, toolName }) {
  if (!theInstance) return;
  const defaultMode = theInstance.Core.Tools.SnapModes.DEFAULT;
  const snapMode = snap ? defaultMode : null;
  const tool = theInstance.Core.documentViewer.getTool(toolName);
  if (tool.setSnapMode) {
    tool.setSnapMode(snapMode);
  }
}

// UI section
//
// Helper code to add controls to the viewer holding the buttons and dropdown

// Create a container for all controls (label, checkbox and buttons)
const controlsContainer = document.createElement('div');

// Create a button to add new scale
const buttonAddScale = document.createElement('button');
buttonAddScale.textContent = 'Add New Scale';
buttonAddScale.className = 'btn-style';
buttonAddScale.onclick = async () => {
  theInstance.UI.openElements(['scaleModal']);
};

controlsContainer.appendChild(buttonAddScale);

// Create a checkbox to toggle snapping
const snapCheckbox = document.createElement('input');
snapCheckbox.type = 'checkbox';
snapCheckbox.id = 'snapCheckbox';
snapCheckbox.checked = true;
snapCheckbox.onchange = (e) => {
  snapState = e.target.checked;
  setSnapMode({ snap: snapState, toolName: activeTool });
};
controlsContainer.appendChild(snapCheckbox);

const snapLabel = document.createElement('label');
snapLabel.textContent = 'Enable Snapping';
snapLabel.htmlFor = 'snapCheckbox';
controlsContainer.appendChild(snapLabel);

// Create buttons for each measurement tool
MEASUREMENT_TOOLS.forEach((tool, index) => {
  const button = document.createElement('button');
  // IMPORTANT: The order in BUTTONS_TEXT array is the same as in MEASUREMENT_TOOLS array
  button.textContent = BUTTONS_TEXT[index];
  button.className = 'btn-style';
  button.onclick = async () => {
    console.log('Button clicked for tool:', tool);
    setActiveHelper(tool);
  };
  controlsContainer.appendChild(button);
});

const uploadLabel = document.createElement('label');
uploadLabel.textContent = 'Use the Open File command in the WebViewer UI menu to upload a document';

// Apply classes for styling using CSS
uploadLabel.className = 'label-class';
controlsContainer.className = 'control-container';

// Append elements to the controls container
controlsContainer.appendChild(uploadLabel);
element.insertBefore(controlsContainer, element.firstChild);

</code></pre>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/web/get-started/samples/showcase-demo-measurement-tools.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
