Search code examples
swiftios-pdfkit

Swift PDFKit - PDFAnnotation button not displaying text


I am trying to build a table of contents in a PDF in swift. My button is not displaying any text, though the bounds and background color is displaying.

let anno: PDFAnnotation  = PDFAnnotation(bounds: CGRect(x: xPosition, y: yPosition, width: 300, height: 40), forType: .widget, withProperties: nil)

anno.widgetFieldType = .button
anno.backgroundColor = UIColor.white
anno.fontColor = UIColor.black
anno.alignment = NSTextAlignment.center
anno.destination = PDFDestination()
anno.fieldName = "Button Third try"
anno.widgetStringValue = "button hello?"
anno.caption = "This is a test button"
            
anno.draw(with: PDFDisplayBox.mediaBox, in: context.cgContext)

Ultimately I want to click on a link which goes to a page in my Pdf Document


Solution

  • You need to set the widgetControlType to .pushButtonControl:

    anno.widgetControlType = .pushButtonControl

    There's Apple sample code demonstrating adding widgets to a PDF that's worth checking out.