Skip to content
DemoSampleAPI ReferenceFAQ

PDF Generation Template Editor

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

View on GitHub

Adjust Existing Measurement Annotations ​

For existing measurement annotations, when changing the measurement points (such as savePoints for CPDFPolylineAnnotation), it's necessary to refresh the measurement properties to obtain the new measurement values.

Note: You cannot remove the measurement properties by setting them to nil.

Below is an example code for modifying an existing 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(page: page, 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()
  anotation.measureInfo = measureInfo;

   // Update the appearance of the annotation to display it on the document
  page.updateAndAdd(anotation!)
  
  // Add a new vertex to the polyline
  let point4 = CGPoint(x: 200, y: 300)
  pointsArray.append(NSValue(cgPoint: point4))
  anotation?.setSavePoints(pointsArray)
  
  // Refresh the measurement properties to recalculate
  // Set MeasureInfo: This information will not be effective until it is added to the page
  anotation.measureInfo = measureInfo;
  
  // Refresh the appearance of the annotation
  anotation.updateAppearanceStream()
}
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] initWithPage:page document: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];
[anotation setMeasureInfo:measureInfo];

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

// Add a new vertex to the polyline
CGPoint point4 = CGPointMake(200, 300);
[pointsArray addObject:[NSValue valueWithCGPoint:point4]];
[anotation setSavePoints:savePoints];

// Refresh the measurement properties to recalculate
// Set MeasureInfo: This information will not be effective until it is added to the page
 anotation.measureInfo = measureInfo;

// Refresh the appearance of the annotation
[anotation updateAppearanceStream];