0

I'm fairly new to objective c and Apple's PdfKit framework and i'm unable to draw anotations on top of my pdf.

I get no errors on the console. This is my code :

PDFAnnotation  * observation = [[PDFAnnotation alloc] init];
CGRect cgRect = CGRectMake(20, 20, 120, 120);
                
observation.widgetFieldType = PDFAnnotationWidgetSubtypeButton;
observation.bounds = cgRect;
observation.shouldDisplay = true;
observation.backgroundColor = UIColor.redColor;
observation.widgetFieldType= PDFAnnotationWidgetSubtypeButton;
                
[page addAnnotation:observation];

Does someone know why my pdfanotation is not drawn on my pdf ? I'm also wondering if the PdfKit framework is fully supported on objective c as the apple's documentation for it just has examples that are made using swift.

Thank you for your help !

Michel Melhem
  • 551
  • 1
  • 8
  • 22

1 Answers1

1

Your annotation is not drawn because you forgot to set the type. It's probably a mistake, because you're setting widgetFieldType twice. Here's the correct button widget setup:

PDFAnnotation  *observation = [[PDFAnnotation alloc] init];
observation.bounds = CGRectMake(20, 20, 200, 100);

observation.type = PDFAnnotationSubtypeWidget;
observation.widgetFieldType = PDFAnnotationWidgetSubtypeButton;
observation.widgetControlType = kPDFWidgetCheckBoxControl;

observation.backgroundColor = UIColor.redColor;
[page addAnnotation:observation];

To avoid future mistakes like this one, use the following initializer:

- (instancetype)initWithBounds:(CGRect)bounds 
                       forType:(PDFAnnotationSubtype)annotationType 
                withProperties:(NSDictionary *)properties;

And change the setup code to:

PDFAnnotation  *observation = [[PDFAnnotation alloc] initWithBounds:CGRectMake(20, 20, 200, 100)
                                                            forType:PDFAnnotationSubtypeWidget
                                                     withProperties:nil];
observation.widgetFieldType = PDFAnnotationWidgetSubtypeButton;
observation.widgetControlType = kPDFWidgetCheckBoxControl;
observation.backgroundColor = UIColor.redColor;
    
[page addAnnotation:observation];

I'd highly recommend to setup appearance (like backgroundColor) as the last thing. Because all these values are modified by PDFKit when you change the type.

Also be aware that 0, 0 is bottom/left corner (bounds).

zrzka
  • 20,249
  • 5
  • 47
  • 73
  • Thank you ! Do you know if there is a way to draw a picture instead of a button as an annotation ? – Michel Melhem Aug 05 '20 at 11:59
  • IIRC you can't add it as an annotation, but you can draw it (with some drawbacks) - see [Inserting an Image into a PDF](https://pspdfkit.com/blog/2019/insert-image-into-pdf-with-swift/) for example. It's in Swift, but you can easily rewrite it to Obj-C. – zrzka Aug 05 '20 at 12:17
  • ah. I want to place clickable pointers on top of my PDF do you know if i could instead of drawing a clickable picture draw a custom clickable annotation ? Thanks – Michel Melhem Aug 05 '20 at 15:08