Skip to content
Guides

Measure Perimeter and Area

Polygon measurement annotations can be used to measure the perimeter and area of a polygonal region selected by the user.

2-13-2

Below is an example code for creating a polygon measurement annotation:

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

  // Create a polygon annotation
  var anotation = CPDFPolygonAnnotation(document: document)

   // Set the vertex of the polygon
  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 polygon annotation
  var measureInfo = CPDFAreaMeasureInfo()
  // (for closed graphics where the area can be measured) Setting displays the area and circumference of the graph in the comment appearance.
  measureInfo.captionType = [.length, .area]
  
  //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 polygon annotation
CPDFPolygonAnnotation *anotation = [[CPDFPolygonAnnotation alloc] initWithDocument:document];

// Set the vertex of the polygon
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 polygon annotation
CPDFAreaMeasureInfo *measureInfo = [[CPDFAreaMeasureInfo alloc] init];
// (for closed graphics where the area can be measured) Setting displays the area and circumference of the graph in the comment appearance.
measureInfo.captionType = CPDFCaptionTypeLength | CPDFCaptionTypeArea;

//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];