In my app users will enter information on one screen that gets put into a database. On another screen I want to display a pdf with their information used to fill out the edit text fields (annotations). In the example below the user can click "John Smith" currently and type a new name.
Using ios PDFKit I am able to display the pdf and allow the user to edit. I am unable to update text in the text boxes or change the status of checkboxes. It looks as though I do have read/write privileges for the pdf.
With my current code I was able to get the number of pages of my pdf and print off the annotation names of every annotation so I can get the names of the fields I want to set values for but the setvalue function does not work and checking the contents of a filled textbox shows nil.
override func viewDidLoad() {
super.viewDidLoad()
displayPDF()
updatePDFText()
}
func displayPDF() {
let pdfView = PDFView()
pdfView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pdfView)
pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
guard let path = Bundle.main.url(forResource: MY_PDF, withExtension: "pdf") else { return }
if let document = PDFDocument(url: path) {
pdfView.document = document
}
}
func updatePDFText() {
guard let path = Bundle.main.url(forResource: MY_PDF, withExtension: "pdf") else { return }
if let document = PDFDocument(url: path) {
for index in 0..<document.pageCount {
if let page = document.page(at: index) {
let annotations = page.annotations
for annotation in annotations {
//print("Annotation Name :: \(annotation.fieldName ?? "")")
if annotation.fieldName == "SSN1" {
annotation.setValue("TEST", forAnnotationKey: .widgetValue)
page.removeAnnotation(annotation)
page.addAnnotation(annotation)
} else if annotation.fieldName == "District" {
annotation.buttonWidgetState = .onState
page.removeAnnotation(annotation)
page.addAnnotation(annotation)
}
}
}
}
}
}
Is there an issue with my implementation or is there another method of programmatically updating the pdf annotations?