> 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/salesforce/annotations/measure-documents-salesforce.md).

# Measure area, distance & perimeter in PDFs in Salesforce

Learn how to use measurement annotations in WebViewer with Apryse SDK. Create and customize line, polyline, and polygon annotations for PDF documents. Get started today! Salesforce WebViewer adds accu

## Getting Started

We recommend the [Overview](/salesforce/get-started/readme.md) page to learn how to correctly use a `config.js` before getting started with using Webviewer.

<a href="https://github.com/ApryseSDK/salesforce-webviewer-document-converter" class="button primary">Sample Project</a>

PDF documents allow line, polyline and polygon annotations to contain measurement information to measure the distance, perimeter and area of parts of a document. A scale is defined so that for example, one inch on the document is defined as one foot, allowing you determine the actual size of things in an architectural diagram once you know the scale.

Apryse SDK has built-in tools allowing you to create these annotations and provides a UI to modify common properties. There is also a more advanced programmatic API for fine grained control.

### Setting Up

When creating a new instance of WebViewer, the `enableMeasurement` property needs to be set to true to display the measurement annotation tools in the UI

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
WebViewer({
  ... // other options
  enableMeasurement: true
}, viewerElement);
```

{% endcode %}
{% endtab %}
{% endtabs %}

Besides passing in a constructor option, measurement can be toggled using the WebViewer API with the `enableFeatures` and `disableFeatures` functions in the `config.js`.

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
window.addEventListener('viewerLoaded', () => {
    instance.UI.enableFeatures([instance.Feature.Measurement]);
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Measurement Annotations

To create measurement annotations in WebViewer, click on the measurement tool icon, select one of the tools. Next click and drag on the document to create a measurement annotation. An overlay with measurement information will show up when you are creating or selecting a measurement annotation.

![](https://306473577-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfmJo9lQOEYOFBgOyFp26%2Fuploads%2Fgit-blob-997a7d301ac55e3e077af79791d3bf54fede1695%2F00688390f80bf8eff35dda30e73759a8d42d653b-1220x1082.gif?alt=media)

The following example shows how you can determine if an annotation is a measurement annotation and logs the precision and scale of it after it's added:

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
window.addEventListener('viewerLoaded', () => {
    const { annotationManager } = instance.Core;

    annotationManager.addEventListener('annotationChanged', (annotations, action) => {
      if (action === 'add') {
        // An annotation is an measurement annotation if it contains a Measure property
        const measurementAnnotations = annotations.filter(annotation => annotation.Measure);

        measurementAnnotations.forEach(annotation => {
          console.log(annotation.Scale);
          console.log(annotation.Precision);
        });
      }
    });
  });
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
window.addEventListener('viewerLoaded', () => {
    const { docViewer, annotManager } = instance;

    annotManager.on('annotationChanged', (annotations, action) => {
      if (action === 'add') {
        // An annotation is an measurement annotation if it contains a Measure property
        const measurementAnnotations = annotations.filter(annotation => annotation.Measure);

        measurementAnnotations.forEach(annotation => {
          console.log(annotation.Scale);
          console.log(annotation.Precision);
        });
      }
    });
  });
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Customization

### Setting scale and precision with the UI

To set measurement properties for a tool, click on the measurement tool icon and select the tool. A style menu will pop up in which you can find and change the properties.

![](https://306473577-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfmJo9lQOEYOFBgOyFp26%2Fuploads%2Fgit-blob-6e46320731321f7200c2d134baa5599525c76989%2Fd718903f3ce4df6a69d11efbb02ec14831f26320-1220x1082.gif?alt=media)

### Setting scale and precision programmatically

You can take the same approach as shown in the [Customizing tools](/web/annotation/customizing-tools.md) guide to set measurement properties. Measurement related tool names are: `AnnotationCreateDistanceMeasurement`, `AnnotationCreatePerimeterMeasurement` and `AnnotationCreateAreaMeasurement`. You can view the list of [valid tool names](/web/annotation/annotations-and-tools.md#list-of-tool-names). The following example sets the scale and precision of the distance measurement tool:

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
window.addEventListener('viewerLoaded', () => {
    const { documentViewer } = instance.Core;
    const distanceMeasurementTool = documentViewer.getTool('AnnotationCreateDistanceMeasurement');

    distanceMeasurementTool.setStyles(() => ({
      // value of Scale is an array that is consisted of two arrays
      // the first element in each array is the scale ratio and the second element is the unit.
      // valid units are: mm, cm, m, km, mi, yd, ft, in and pt
      // the following array means that for the annotations created by the distance measurement tool, 0.25 inches on the document is equal to 1 inch in the real world
      Scale: [[0.25, 'in'], [1, 'in']],

      // value of Precision is a number that means how many decimal places the calculated value should have
      Precision: 0.001
    });
  });
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
window.addEventListener('viewerLoaded', () => {
    const { docViewer } = instance;
    const distanceMeasurementTool = docViewer.getTool('AnnotationCreateDistanceMeasurement');

    distanceMeasurementTool.setStyles(() => ({
      // value of Scale is an array that is consisted of two arrays
      // the first element in each array is the scale ratio and the second element is the unit.
      // valid units are: mm, cm, m, km, mi, yd, ft, in and pt
      // the following array means that for the annotations created by the distance measurement tool, 0.25 inches on the document is equal to 1 inch in the real world
      Scale: [[0.25, 'in'], [1, 'in']],

      // value of Precision is a number that means how many decimal places the calculated value should have
      Precision: 0.001
    });
  });
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Getting scale and precision programmatically

The following example logs the precision and scale of the distance measurement tool:

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
window.addEventListener('viewerLoaded', () => {
    const { documentViewer } = instance.Core;
    const distanceMeasurementTool = documentViewer.getTool('AnnotationCreateDistanceMeasurement');

    console.log(distanceMeasurementTool.defaults.Scale);
    console.log(distanceMeasurementTool.defaults.Precision);
  });
});
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
window.addEventListener('viewerLoaded', () => {
    const { docViewer } = instance;
    const distanceMeasurementTool = docViewer.getTool('AnnotationCreateDistanceMeasurement');

    console.log(distanceMeasurementTool.defaults.Scale);
    console.log(distanceMeasurementTool.defaults.Precision);
  });
});
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# 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/salesforce/annotations/measure-documents-salesforce.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.
