Skip to content

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Get Measurement Properties ​

Get measurement properties for annotations and tools ​

javascript
ComPDFKitViewer(...)
  .then(instance => {
    const { documentViewer, Tools } = instance.Core;

    documentViewer.addEventListener('documentLoaded', () => {
      const { DISTANCE_MEASUREMENT, PERIMETER_MEASUREMENT, CURVE_MEASUREMENT, ARC_MEASUREMENT, ELLIPSE_MEASUREMENT, RECTANGULAR_AREA_MEASUREMENT, AREA_MEASUREMENT } = Tools.ToolNames;
      const [lineTool, polylineTool, curveTool, arcTool, circleTool, rectangleTool, polygonTool] = [DISTANCE_MEASUREMENT, PERIMETER_MEASUREMENT, CURVE_MEASUREMENT, ARC_MEASUREMENT, ELLIPSE_MEASUREMENT, RECTANGULAR_AREA_MEASUREMENT, AREA_MEASUREMENT].map(name => documentViewer.getTool(name));
      console.log(lineTool.Measure); // The default scale and precision of all measurement creation tools are universal.
      console.log(lineTool.defaults); // The default properties of the style are not universal.
    });
  });


Configure Measurement Properties ​

Configure measurement properties for annotations and tools ​

You can use the setStyles method of each measurement tool to set properties such as measurement scale (Scale) and precision (Precision) of newly created measurement annotations, and the setAnnotationStyles method of annotationManager to change the measurement properties of the currently selected annotation (if any). Whether the perimeter, area, radius, arc length and angle are displayed in the annotation appearance is set with the setDefaultMeasurementDisplayOptions method of the tool and the setMeasurementDisplayOptions method of the annotation.

This example shows how to configuring measurement properties:

javascript
const scale = [[1, 'in'], [10, 'ft']]; // Set the measurement scale in the PDF file to 1.0 inch, corresponding to 10.0 feet in the actual object.
const precision = 0.01; // Set the precision to 0.01.

const { DISTANCE_MEASUREMENT, PERIMETER_MEASUREMENT, CURVE_MEASUREMENT, ARC_MEASUREMENT, ELLIPSE_MEASUREMENT, RECTANGULAR_AREA_MEASUREMENT, AREA_MEASUREMENT } = Tools.ToolNames;
// Default measurement properties of newly created measurement annotations.
for (const name of [DISTANCE_MEASUREMENT, PERIMETER_MEASUREMENT, CURVE_MEASUREMENT, ARC_MEASUREMENT, ELLIPSE_MEASUREMENT, RECTANGULAR_AREA_MEASUREMENT, AREA_MEASUREMENT]) {
  documentViewer.getTool(name).setStyles({ Scale: scale, Precision: precision });
}
// (For enclosed graphics with measurable perimeter and area) Set to display the perimeter and the area in the annotation appearance.
documentViewer.getTool(RECTANGULAR_AREA_MEASUREMENT).setDefaultMeasurementDisplayOptions({ perimeter: true, area: true });
documentViewer.getTool(AREA_MEASUREMENT).setDefaultMeasurementDisplayOptions({ perimeter: true, area: true });
// (For graphics that can measure radius) Set to display the radius in the annotation appearance.
documentViewer.getTool(ELLIPSE_MEASUREMENT).setDefaultMeasurementDisplayOptions({ radius: true, perimeter: true, area: true });
// (For graphics that can measure arc length and angle) Set to display the radius, the arc length and the angle in the annotation appearance.
documentViewer.getTool(ARC_MEASUREMENT).setDefaultMeasurementDisplayOptions({ radius: true, arcLength: true, angle: true });

// The currently selected measurement annotations (if any).
for (const annotation of annotationManager.getSelectedAnnotations()) {
  if (annotation.Measure) {
    annotation.setMeasurementDisplayOptions?.({ perimeter: true, area: true, radius: true, arcLength: true, angle: true });
    await annotationManager.setAnnotationStyles(annotation, { Scale: scale, Precision: precision });
  }
}

Supported Measurement Units ​

You can set the units for the measurement ruler on the PDF file and the size units for the actual object in the scale (for example, [[1, 'in'], [10, 'ft']], where the first item is the measurement ruler on the PDF file and the second item is the actual object). The supported units and corresponding parameter values are listed in the table below:

UnitValue
Pointpt
Inchin
Millimetermm
Centimetercm
Meterm
Kilometerkm
Footft
Yardyd
Milemi