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

Configure Measurement Properties ​

Setting the measurement scale and precision by configuring the measureInfo property in CPDFLineAnnotation, CPDFPolylineAnnotation, and CPDFPolygonAnnotation annotations.

Below is an example code for configuring measurement properties:

swift
// Get the page object where the annotation needs to be created
if let page = document?.page(at: 0) {
 // Create a line annotation
  var anotation = CPDFLineAnnotation(page: page, document: document)
  
  // Set the start and end points
  anotation?.startPoint = CGPoint(x: 350, y: 270)
  anotation?.endPoint = CGPoint(x: 260, y: 370)

  // Create measurement properties, set measurement scale and precision (default scale 1 cm = 1 cm)
  var measureInfo = CPDFDistanceMeasureInfo()
  measureInfo.rulerBase = 1.0
  measureInfo.rulerBaseUnit = CPDFMeasureConstants.sharedInstance().cpdfCm
  measureInfo.rulerTranslate = 1.0;
  measureInfo.rulerTranslateUnit = CPDFMeasureConstants.sharedInstance().cpdfKm

  // Set precision to 0.01
  measureInfo.precision = CPDFMeasureConstants.sharedInstance().precisionValueTwo

  // Set the length of the leader line at both ends (this property is unique to CPDFDistanceMeasureInfo)
  measureInfo.leadLength = 5.0

  // Set the measurement properties for the line annotation
  // 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.updateAndAdd(anotation!)
}
objective-c
// Get the page object where the annotation needs to be created
CPDFPage *page = [document pageAtIndex:0];

// Create a line annotation
CPDFLineAnnotation *anotation = [[CPDFLineAnnotation alloc] initWithPage:page document:document];

// Set the start and end points
anotation.startPoint = CGPointMake(350, 270);
anotation.endPoint = CGPointMake(260, 370);

// Create measurement properties, set measurement scale and precision (default scale 1 cm = 1 cm)
CPDFDistanceMeasureInfo *measureInfo = [[CPDFDistanceMeasureInfo alloc] init];
measureInfo.rulerBase = 1.0;
measureInfo.rulerBaseUnit = [CPDFMeasureConstants sharedInstance].cpdfCm;
measureInfo.rulerTranslate = 1.0;
measureInfo.rulerTranslateUnit = [CPDFMeasureConstants sharedInstance].cpdfKm;

 // Set precision to 0.01
measureInfo.precision = [CPDFMeasureConstants sharedInstance].precisionValueTwo;

// Set the length of the leader line at both ends (this property is unique to CPDFDistanceMeasureInfo)
measureInfo.leadLength = 5.0;

// Set the measurement properties for the line annotation
// 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 updateAndAddAnnotation:anotation];

Supported Measurement Units

You can set the units for measuring distances on the PDF file and the units for real-world objects using measureInfo.rulerBaseUnit and measureInfo.rulerTranslateUnit. The supported units and their corresponding constant values are listed in the table below:

UnitConstantValue
Point[CPDFMeasureConstants sharedInstance].cpdfPtpt
Inch[CPDFMeasureConstants sharedInstance].cpdfInin
Millimeter[CPDFMeasureConstants sharedInstance].cpdfMmmm
Centimeter[CPDFMeasureConstants sharedInstance].cpdfCmcm
Meter[CPDFMeasureConstants sharedInstance].cpdfMm
Kilometer[CPDFMeasureConstants sharedInstance].cpdfKmkm
Foot[CPDFMeasureConstants sharedInstance].cpdfFtft
Yard[CPDFMeasureConstants sharedInstance].cpdfYdyd
Mile[CPDFMeasureConstants sharedInstance].cpdfMimi