Get annotation Id when selecting annotation in Swift

In iOS platform (swift):

How can I get the Id of an annotation when tapping it on screen?

I tried this so far:

When I create the annotation on page, I make sure that the setAsAnnotation is “true” and double check that the annotation.isValid is true. Also when I extract the Fdf string of doc, the annotation is written properly.

When I select the annotation, the selection box is displayed, however I always get the following false “isValid” condition.

`
// Event fired when user taps on the Document

func pdfScrollViewTap(_ gestureRecognizer: UITapGestureRecognizer!) {

// Get tap Coordinates in Page
var curPage = ctrl.getCurrentPage()

let location = gestureRecognizer.location(ofTouch: gestureRecognizer.numberOfTouches-1, in: ctrl)

var locatPTPDF: PTPDFPoint = PTPDFPoint.init(px: Double(location.x), py: Double(location.y))

let newCoord = ctrl.convScreenPt(toPagePt: locatPTPDF, page_num: curPage)

// Get annotation near the tap location

self.doc?.lockRead()

let AnnotSelect : PTAnnot = ctrl.getAnnotationAt(Int32((newCoord?.getX())!), y: Int32((newCoord?.getY())!), distanceThreshold: 22.0, minimumLineWeight: 10.0)

// Is there an annotation near the tap location? PROBLEM: ALWAYS FALSE ///////////////////

if AnnotSelect.isValid() == true {

print(“THIS IS A VALID ANNOT”)

print(AnnotSelect.getUniqueID().getAsPDFText())

}else{

print(“NOT A VALID ANNOT”)

}

`

Any help on this issue will be highly appreciated. It doesn’t have to be in Swift, it can be in any language.

Thanks in advance.

Apologies for the delayed reply.

The coordinates passed to GetAnnotationAt: must be in the control’s coordinate system (generically referred to as “screen coordinates” throughout PDFNet). If you take a look at the implementation of handleTap: in the file PanTool.m (part of the tools project), you will see how an annotation is successfully found. Summarizing:

  • (BOOL)handleTap:(UITapGestureRecognizer *)gestureRecognizer
    {

    CGPoint down = [gestureRecognizer locationInView:m_pdfViewCtrl];


@try
{
[m_pdfViewCtrl DocLockRead];

m_moving_annotation = [m_pdfViewCtrl GetAnnotationAt:down.x y:down.y distanceThreshold:GET_ANNOT_AT_DISTANCE_THRESHOLD minimumLineWeight:GET_ANNOT_AT_MINIMUM_LINE_WEIGHT];

if( ![m_moving_annotation IsValid] )
linkInfo = [m_pdfViewCtrl GetLinkAt:down.x y:down.y];
}
@catch (NSException *exception) {
NSLog(@“Exception: %@: %@”,exception.name, exception.reason);
}
@finally {
[m_pdfViewCtrl DocUnlockRead];
}

if( [m_moving_annotation IsValid] )
{

Awesome. It worked. Indeed I was with wrong coordinates.

Thanks Aaron.