PDFKit Annotation PDFAnnotationTextIconType doesn't work

I want to add Note Annotation into PDF file by using PDFKit.

But it doesn't work when I using PDFAnnotationTextIconType/iconType to select the display icon.

below is my code.


PDFAnnotation* annotation = [[PDFAnnotation alloc] initWithBounds:bounds forType:PDFAnnotationSubtypeText withProperties:nil];

annotation.color = UIColor.blueColor;

annotation.modificationDate = [NSDate date];

annotation.iconType = kPDFTextAnnotationIconInsert;

annotation.contents = text;

[page addAnnotation:annotation];


is there any step that i missed?

Replies

I have the same problem as well. The new API doesn't appear to work at all.


As a workaround, I'm using the older API call but that has problems, too. I get a fixed-size 40x40 pixel yellow square. It seems to ignore the passed-in bounds.size value. If I try to set an iconType that appears to be ignored. If I try to hide the square with shouldDisplay = NO that doesn't work either.


#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
  pdfNoteAnnotation = [[PDFAnnotationText alloc] initWithBounds:noteIconBounds];
  pdfNoteAnnotation.iconType = kPDFTextAnnotationIconNote; // this doesn't seem to do anything in 10.12+
  pdfNoteAnnotation.shouldDisplay = NO; // otherwise we see an ugly icon here <<< this doesn't appear to do work correctly in 10.12+
#pragma clang diagnostic pop
  pdfNoteAnnotation.contents = asset.note.string;
  [pdfPage addAnnotation:pdfNoteAnnotation];

Have you got any solution for this. I am still facing the same issue. The note icon is not showing. A light yellow box always appears.


let annotationBounds = CGRect(x: 169, y: 500, width: 30, height: 30)

let commentAnnotation = PDFAnnotation(bounds: annotationBounds, forType: .text, withProperties: nil)

commentAnnotation.iconType = .note

pdfView.currentPage?.addAnnotation(commentAnnotation)


Any help from ios developer is much appreciated.


Thanks

Just a Workaround - Create a subclass of PDFAnnotation and override the draw method


override func draw(with box: PDFDisplayBox, in context: CGContext) {

guard let cgImage = self.image.cgImage else {

return

}

context.draw(cgImage, in: self.bounds)

}


you need to pass the image from your view controller to the PDFAnnotation subclass through its init method