Some test text!

Search
Hamburger Icon

Salesforce / Guides / Measurement

Measure area, distance & perimeter in PDFs in Salesforce

Getting Started

We recommend the Overview page to learn how to correctly use a config.js before getting started with using Webviewer.

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

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

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

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

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.

Creating Measurement Annotations

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:

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);
        });
      }
    });
  });
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);
        });
      }
    });
  });

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.

Setting Measurement Properties

Setting scale and precision programmatically

You can take the same approach as shown in the Customizing tools guide to set measurement properties. Measurement related tool names are: AnnotationCreateDistanceMeasurement, AnnotationCreatePerimeterMeasurement and AnnotationCreateAreaMeasurement. You can view the list of valid tool names . The following example sets the scale and precision of the distance measurement tool:

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
    });
  });
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
    });
  });

Getting scale and precision programmatically

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

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

    console.log(distanceMeasurementTool.defaults.Scale);
    console.log(distanceMeasurementTool.defaults.Precision);
  });
});
window.addEventListener('viewerLoaded', () => {
    const { docViewer } = instance;
    const distanceMeasurementTool = docViewer.getTool('AnnotationCreateDistanceMeasurement');

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

Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales