Skip to content
Guides

Measure Distance

Distance measurement annotations allow your users to measure the distance between two points representing objects on a plane, such as buildings, streets, or walls. With this annotation, users can simply provide the starting and ending points to obtain the distance between the two points.

There are two types of distance measurements: line segment measurement annotations and polyline measurement annotations. Line segment measurement annotations measure the distance between the starting and ending points, while polyline measurement annotations measure the distance between all adjacent points along the polyline and calculate the total length.

2-13-1

Here is an example code for creating a polyline distance measurement annotation:

swift
// Get the page object where the annotation needs to be created
if let page = document?.page(at: 0) {

  // Create a polyline annotation
  var anotation = CPDFPolylineAnnotation(document: document)

  // Set the vertex of the polyline
  var pointsArray: [NSValue] = []
  let point1 = CGPoint(x: 100, y: 100)
  pointsArray.append(NSValue(cgPoint: point1))
  let point2 = CGPoint(x: 100, y: 200)
  pointsArray.append(NSValue(cgPoint: point2))
  let point3 = CGPoint(x: 200, y: 200)
  pointsArray.append(NSValue(cgPoint: point3))
  anotation?.setSavePoints(pointsArray)

  // Set the measurement properties for the polyline annotation
  var measureInfo = CPDFPerimeterMeasureInfo()
  //Set MeasureInfo: This information will not be effective until it is added to the page
  anotation.measureInfo = measureInfo;

   // Update the appearance of the annotation to display it on the document
  page.addAnnotation(anotation!)
}
objective-c
// Get the page object where the annotation needs to be created
CPDFPage *page = [document pageAtIndex:0];

// Create a polyline annotation
CPDFPolylineAnnotation *anotation = [[CPDFPolylineAnnotation alloc] initWithDocument:document];

 // Set the vertex of the polyline
NSMutableArray<NSValue *> *pointsArray = [NSMutableArray array];
CGPoint point1 = CGPointMake(100, 100);
[pointsArray addObject:[NSValue valueWithCGPoint:point1]];
CGPoint point2 = CGPointMake(100, 200);
[pointsArray addObject:[NSValue valueWithCGPoint:point2]];
CGPoint point3 = CGPointMake(200, 200);
[pointsArray addObject:[NSValue valueWithCGPoint:point3]];
[anotation setSavePoints:savePoints];

 // Set the measurement properties for the polyline annotation
CPDFPerimeterMeasureInfo *measureInfo = [[CPDFPerimeterMeasureInfo alloc] init];
//Set MeasureInfo: This information will not be effective until it is added to the page
[anotation setMeasureInfo:measureInfo];

// Update the appearance of the annotation to display it on the document
[page addAnnotation:anotation];