Distance Annotation in ASP.NET Core PDF Viewer
14 Aug 20267 minutes to read
Distance is a measurement annotation used to measure the length between two points on a PDF page. Use it for precise reviews, markups, or engineering measurements.

Enable Distance Annotation
To enable Distance annotation, inject the following modules into the ASP.NET Core PDF Viewer:
<div style="width:100%;height:600px">
<ejs-pdfviewer id="pdfviewer"
style="height:600px"
documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"
resourceUrl="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib">
</ejs-pdfviewer>
</div>Add Distance Annotation
Add Distance Using the Toolbar
- Open the Annotation Toolbar.
- Select Measurement → Distance.
- Click to set the start point, then click again to set the end point.

If Pan mode is active, choosing a measurement tool switches the viewer into the appropriate interaction mode for a smoother workflow.
Enable Distance Mode
Programmatically switch the viewer into Distance mode.
<script>
function enableDistanceMode() {
var viewer = document.getElementById('pdfviewer').ej2_instances[0];
viewer.annotation.setAnnotationMode('Distance');
}
</script>Exit Distance Mode
<script>
function exitDistanceMode() {
var viewer = document.getElementById('pdfviewer').ej2_instances[0];
viewer.annotation.setAnnotationMode('None');
}
</script>Add Distance Programmatically
Use the addAnnotation API to draw a Distance measurement by providing two vertexPoints.
<script>
function addDistance() {
var viewer = document.getElementById('pdfviewer').ej2_instances[0];
viewer.annotation.addAnnotation('Distance', {
offset: { x: 200, y: 230 },
pageNumber: 1,
vertexPoints: [
{ x: 200, y: 230 },
{ x: 350, y: 230 }
]
});
}
</script>Customize Distance Appearance
Configure default properties using the Distance Settings property (for example, default fill color, stroke color, opacity).
<div style="width:100%;height:650px">
<ejs-pdfviewer id="pdfviewer"
style="height:650px"
documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"
resourceUrl="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib">
</ejs-pdfviewer>
</div>
<script>
window.onload = function () {
var viewer = document.getElementById('pdfviewer').ej2_instances[0];
viewer.distanceSettings = { fillColor: 'blue', strokeColor: 'green', opacity: 0.6 };
}
</script>Manage Distance (Move, Resize, Edit, Delete)
- Move: Drag the measurement to reposition it.
- Resize: Drag the end handles to adjust the length.
Edit Distance
Edit Distance (UI)
Change stroke color, thickness, and opacity using the annotation toolbar tools.
- Edit the fill color using the Edit Color tool.
- Edit the stroke color using the Edit Stroke Color tool.
- Edit the border thickness using the Edit Thickness tool.
- Edit the opacity using the Edit Opacity tool.
- Open Right Click → Properties for additional line-based options.
Edit Distance Programmatically
Update properties and call editAnnotation().
<script>
function editDistanceProgrammatically() {
var viewer = document.getElementById('pdfviewer').ej2_instances[0];
for (var i = 0; i < viewer.annotationCollection.length; i++) {
var ann = viewer.annotationCollection[i];
if (ann.subject === 'Distance calculation') {
ann.strokeColor = '#0000FF';
ann.thickness = 2;
ann.opacity = 0.8;
viewer.annotation.editAnnotation(ann);
break;
}
}
}
</script>Delete Distance Annotation
Delete Distance Annotation via UI (toolbar/context menu) or programmatically. For supported workflows and APIs, see Delete Annotation.
Set Default Properties During Initialization
Apply defaults for Distance using the distanceSettings property.
<PdfViewerComponent
id="container"
documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"
resourceUrl="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib"
distanceSettings=
style=
>
<Inject services={[Toolbar, Annotation]} />
</PdfViewerComponent>Set Properties While Adding Individual Annotation
Pass per-annotation values directly when calling addAnnotation.
<script>
function addStyledDistance() {
var viewer = document.getElementById('pdfviewer').ej2_instances[0];
viewer.annotation.addAnnotation('Distance', {
offset: { x: 220, y: 260 },
pageNumber: 1,
vertexPoints: [
{ x: 220, y: 260 },
{ x: 360, y: 260 }
],
strokeColor: '#059669',
opacity: 0.9,
fillColor: '#D1FAE5',
thickness: 2
});
}
</script>Scale Ratio and Units
- Use Scale Ratio from the context menu to set the actual-to-page scale.
- Supported units include Inch, Millimeter, Centimeter, Point, Pica, Feet.
Set Default Scale Ratio During Initialization
Configure scale defaults using measurementSettings.
<div style="width:100%;height:650px">
<ejs-pdfviewer id="pdfviewer"
style="height:650px"
documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"
resourceUrl="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib">
</ejs-pdfviewer>
</div>
<script>
var viewer = document.getElementById('pdfviewer').ej2_instances[0];
viewer.measurementSettings = { scaleRatio: 2, conversionUnit: 'cm', displayUnit: 'cm' };
</script>Handle Distance Events
Listen to annotation life-cycle events (add/modify/select/remove). For the full list and parameters, see Annotation Events.
Export and Import
Distance measurements can be exported or imported with other annotations. For workflows and supported formats, see Export and Import annotations.