This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /guides/web/measurements/measure-distance.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Measure distance in a PDF using JavaScript | Nutrient Web SDK

Use Nutrient Web SDK 2022.5 or later to measure distance in a PDF. To measure distance in your app, contact the Sales team to add the Measurement Tools component to your license. You can also run the SDK in trial mode.

Measure distance in these ways:

Measuring distance between two points

Measure the distance between two points with the built-in UI or with code.

Measuring distance between two points using the built-in UI

Use the distance tool to draw a straight line between two points.

  1. Click the measurement tools icon in the toolbar.
  2. Click Distance.
  3. Draw a straight line between the two points.

Measuring distance between two points programmatically

To measure the distance between two points with code, use the LineAnnotation shape constructor. Pass a measurementScale property and a measurementPrecision property as parameters:

let pspdfkitInstance; // Global or module-level variable.
NutrientViewer.load({
...baseOptions,
toolbarItems: [
...NutrientViewer.defaultToolbarItems,
{ type: "cloudy-rectangle", dropdownGroup: "shapes" },
{ type: "dashed-rectangle", dropdownGroup: "shapes" },
{ type: "cloudy-ellipse", dropdownGroup: "shapes" },
{ type: "dashed-ellipse", dropdownGroup: "shapes" },
{ type: "dashed-polygon", dropdownGroup: "shapes" },
{ type: "content-editor", dropdownGroup: "editor" },
{ type: "form-creator", dropdownGroup: "editor" },
{ type: "measure", dropdownGroup: "editor" },
{ type: "document-comparison", dropdownGroup: "editor" },
],
measurementValueConfiguration: (documentScales) => {
const customScale = {
name: "myCustomScale",
scale: {
unitFrom: NutrientViewer.MeasurementScaleUnitFrom.POINTS, // Using the enum value.
unitTo: NutrientViewer.MeasurementScaleUnitTo.CENTIMETERS, // Using the enum value.
fromValue: 1,
toValue: 0.05, // 1 pt = 0.05 cm
},
precision: NutrientViewer.MeasurementPrecision.TWO, // Using the enum value
selected: true, // Make this the default scale.
};
return [customScale, ...documentScales];
},
})
.then(async (instance) => {
try {
// Create measurement annotation.
const lineAnnotation = new NutrientViewer.Annotations.LineAnnotation({
strokeColor: NutrientViewer.Color.RED,
pageIndex: 0,
startPoint: new NutrientViewer.Geometry.Point({ x: 100, y: 100 }),
endPoint: new NutrientViewer.Geometry.Point({ x: 300, y: 300 }),
boundingBox: new NutrientViewer.Geometry.Rect({
left: 100,
top: 100,
width: 200,
height: 200,
}),
measurementPrecision: "threeDp",
measurementScale: new NutrientViewer.MeasurementScale({
unitFrom: NutrientViewer.MeasurementScaleUnitFrom.INCHES,
unitTo: NutrientViewer.MeasurementScaleUnitTo.INCHES,
fromValue: 1,
toValue: 1,
}),
lineCaps: { start: "butt", end: "butt" },
});
// Add the annotation to the document.
await instance.create(lineAnnotation);
console.log("Measurement annotation created and added to the document.");
console.log((await instance.getAnnotations(0)).toJS());
} catch (error) {
console.error("Error creating or adding annotation:", error);
}
})
.catch(function (error) {
console.error("Error loading PDF:", error.message);
});

You can try the code snippet above in the two-point distance playground(opens in a new tab). For more information on scale and precision, refer to the configure measurements guide.

Measuring distance between multiple connected points

Measure the distance between multiple connected points with the built-in UI or with code.

Measuring distance between multiple connected points using the built-in UI

Use the perimeter tool to draw connected lines between multiple points.

  1. Click the measurement tools icon in the toolbar.
  2. Click Perimeter.
  3. Draw lines between the points to measure.

Measuring distance between multiple connected points programmatically

To measure the distance between multiple connected points with code, use the PolylineAnnotation shape constructor. Pass a measurementScale property and a measurementPrecision property as parameters:

let pspdfkitInstance; // Global or module-level variable.
NutrientViewer.load({
...baseOptions,
theme: NutrientViewer.Theme.DARK,
toolbarItems: [
...NutrientViewer.defaultToolbarItems,
{ type: "cloudy-rectangle", dropdownGroup: "shapes" },
{ type: "dashed-rectangle", dropdownGroup: "shapes" },
{ type: "cloudy-ellipse", dropdownGroup: "shapes" },
{ type: "dashed-ellipse", dropdownGroup: "shapes" },
{ type: "dashed-polygon", dropdownGroup: "shapes" },
{ type: "content-editor", dropdownGroup: "editor" },
{ type: "form-creator", dropdownGroup: "editor" },
{ type: "measure", dropdownGroup: "editor" },
{ type: "document-comparison", dropdownGroup: "editor" },
],
measurementValueConfiguration: (documentScales) => {
const customDistanceScale = {
name: "myCustomDistanceScale",
scale: {
unitFrom: "pt", // Simple string for points.
unitTo: "cm", // Simple string for centimeters.
fromValue: 1,
toValue: 0.05,
},
precision: NutrientViewer.MeasurementPrecision.TWO,
selected: true,
};
return [customDistanceScale, ...documentScales];
},
})
.then(async (instance) => {
console.log("Nutrient loaded!");
try {
// Define the points for the polyline (forming a zigzag).
const points = NutrientViewer.Immutable.List([
new NutrientViewer.Geometry.Point({ x: 100, y: 100 }),
new NutrientViewer.Geometry.Point({ x: 200, y: 150 }),
new NutrientViewer.Geometry.Point({ x: 150, y: 200 }),
new NutrientViewer.Geometry.Point({ x: 250, y: 250 }),
]);
// Calculate the bounding box (approximate).
const minX = 100;
const minY = 100;
const maxX = 250;
const maxY = 250;
const boundingBox = new NutrientViewer.Geometry.Rect({
left: minX,
top: minY,
width: maxX - minX,
height: maxY - minY,
});
// Providing a `measurementScale` makes this a measurement annotation.
const polylineAnnotation = new NutrientViewer.Annotations.PolylineAnnotation({
strokeColor: NutrientViewer.Color.GREEN,
pageIndex: 0,
points: points,
strokeWidth: 3,
boundingBox: boundingBox,
measurementPrecision: "twoDp", // Using string format.
measurementScale: new NutrientViewer.MeasurementScale({
unitFrom: NutrientViewer.MeasurementScaleUnitFrom.POINTS,
unitTo: NutrientViewer.MeasurementScaleUnitTo.CENTIMETERS,
fromValue: 1,
toValue: 0.05,
}),
});
// Add the annotation to the document.
await instance.create(polylineAnnotation);
console.log("Polyline distance measurement annotation created and added.");
console.log((await instance.getAnnotations(0)).toJS());
} catch (error) {
console.error("Error creating or adding polyline measurement annotation:", error);
}
})
.catch(function (error) {
console.error("Error loading PDF:", error.message);
});

You can try the code snippet above in the connected-point distance playground(opens in a new tab). For more information on scale and precision, refer to the configure measurements guide.

Configuring measurements

For more information on configuring measurements, refer to the configure measurements guide.

Distance and perimeter measurements can show a chain of smaller units. For example, they can show 6 ft 3 in instead of 6.25 ft. For more information, refer to the compound mixed-unit measurements section.