PDFAnnotation Radiobutton don't change background color or don't display value

I use PDFKit in iOS as PDF renderer and I have problem with background color in radio button.


I iterate all PDF annotations on all pages in the document.


override func viewDidAppear(_ animated: Bool) {
  //let pdfData = ... received data from file storage ...
  if let document = PDFDocument(data: pdfData) {
    for i in 0...document.pageCount {
      if let page = document.page(at: i) {
         for annot in page.annotations {
           if annot.widgetControlType == .radioButtonControl {
              annot.backgroundColor = UIColor.red
           }
         }        
       }
     }
   }
}


When I set the background color, nothing happens.

But when I try to set the background color to the type of Text annotation, the color changes.


The difference between the Radio button and the Text annotation is in its type.

The Radio button has a widgetFieldType == .button and the Text has a widgetFieldType == .text.

I think this is the reason why it doesn't work.


Next I try to remove annotation, change his background color and add again. This also doesn't work.


if annot.widgetControlType == .radioButtonControl {
  page.removeAnnotation(annot)
  annot.backgroundColor = UIColor.red
  page.addAnnotation(annot)
}


But when I create a new instance of PDFAnnotaion, and I add it to the page, it works.


if annot.widgetControlType == .radioButtonControl {
  page.removeAnnotation(annot)
  let newAnnot = PDFAnnotation(bounds: annot.bounds, forType: .widget, withProperties: nil)
  newAnnot.backgroundColor = UIColor.red
  newAnnot.widgetStringValue = annot.widgetStringValue
  page.addAnnotation(newAnnot)
}


The big problem is that the value of this Radio button is not displayed, even though it contains the value itself.

If in another method I get all the values from PDF annotations, there is also the right value from this radio button.

The Radio button contains the value, only it is displayed.


I tried to copy all the properties PDF annotation from the annot to newAnnot, but it didn't help.


How to properly change the background color of the Radio button and display its value?

Replies

Did you try using


func setValue(_ value: Any, forAnnotationKey key: PDFAnnotationKey) -> Bool

with key

widgetBackgroundColor

static let widgetBackgroundColor: PDFAnnotationKey


to set the color ?

Hi Claude31,


I try it, but it doesn't work.

Radio button hasn't changed the background color.