2

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

C. Skjerdal
  • 2,750
  • 3
  • 25
  • 50

1 Answers1

2

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.

samaitch
  • 1,172
  • 7
  • 9
  • So my mistake was I tried that before and it didn't work, but I hadn't realized I needed to set BOTH widgetFieldType to button, and widgetControlType to pushButtonControl. Thanks for your help. Edit: Now the text is upside down, but I've seen that solution else where. – C. Skjerdal Jul 08 '21 at 22:35