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.
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
JavaScript
1WebViewer({
2 ... // other options
3 enableMeasurement: true
4}, 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
.
JavaScript
1window.addEventListener('viewerLoaded', () => {
2 instance.UI.enableFeatures([instance.Feature.Measurement]);
3}
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.
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:
JavaScript
1window.addEventListener('viewerLoaded', () => {
2 const { annotationManager } = instance.Core;
3
4 annotationManager.addEventListener('annotationChanged', (annotations, action) => {
5 if (action === 'add') {
6 // An annotation is an measurement annotation if it contains a Measure property
7 const measurementAnnotations = annotations.filter(annotation => annotation.Measure);
8
9 measurementAnnotations.forEach(annotation => {
10 console.log(annotation.Scale);
11 console.log(annotation.Precision);
12 });
13 }
14 });
15 });
JavaScript
1window.addEventListener('viewerLoaded', () => {
2 const { docViewer, annotManager } = instance;
3
4 annotManager.on('annotationChanged', (annotations, action) => {
5 if (action === 'add') {
6 // An annotation is an measurement annotation if it contains a Measure property
7 const measurementAnnotations = annotations.filter(annotation => annotation.Measure);
8
9 measurementAnnotations.forEach(annotation => {
10 console.log(annotation.Scale);
11 console.log(annotation.Precision);
12 });
13 }
14 });
15 });
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.
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:
JavaScript
1window.addEventListener('viewerLoaded', () => {
2 const { documentViewer } = instance.Core;
3 const distanceMeasurementTool = documentViewer.getTool('AnnotationCreateDistanceMeasurement');
4
5 distanceMeasurementTool.setStyles(() => ({
6 // value of Scale is an array that is consisted of two arrays
7 // the first element in each array is the scale ratio and the second element is the unit.
8 // valid units are: mm, cm, m, km, mi, yd, ft, in and pt
9 // 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
10 Scale: [[0.25, 'in'], [1, 'in']],
11
12 // value of Precision is a number that means how many decimal places the calculated value should have
13 Precision: 0.001
14 });
15 });
JavaScript
1window.addEventListener('viewerLoaded', () => {
2 const { docViewer } = instance;
3 const distanceMeasurementTool = docViewer.getTool('AnnotationCreateDistanceMeasurement');
4
5 distanceMeasurementTool.setStyles(() => ({
6 // value of Scale is an array that is consisted of two arrays
7 // the first element in each array is the scale ratio and the second element is the unit.
8 // valid units are: mm, cm, m, km, mi, yd, ft, in and pt
9 // 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
10 Scale: [[0.25, 'in'], [1, 'in']],
11
12 // value of Precision is a number that means how many decimal places the calculated value should have
13 Precision: 0.001
14 });
15 });
The following example logs the precision and scale of the distance measurement tool:
JavaScript
1window.addEventListener('viewerLoaded', () => {
2 const { documentViewer } = instance.Core;
3 const distanceMeasurementTool = documentViewer.getTool('AnnotationCreateDistanceMeasurement');
4
5 console.log(distanceMeasurementTool.defaults.Scale);
6 console.log(distanceMeasurementTool.defaults.Precision);
7 });
8});
JavaScript
1window.addEventListener('viewerLoaded', () => {
2 const { docViewer } = instance;
3 const distanceMeasurementTool = docViewer.getTool('AnnotationCreateDistanceMeasurement');
4
5 console.log(distanceMeasurementTool.defaults.Scale);
6 console.log(distanceMeasurementTool.defaults.Precision);
7 });
8});