Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Programmatically fill PDF form field using iOS PDFKit

enter image description here In my application, I have a PDF form based on user input. I force the value in the PDF form. I was using the ILPDFKit pod for doing this in my application (swift 3, but since PDFKit is around in iOS 11, I would to know if this is possible using PDFKit). The code I was using with ILPDFKit was:

// set the pdf form as ILPDFDocument  
let document = ILPDFDocument(resource:"ExamplePDFfrom")

// Manually set a form value for field name (created in adobe acrobat)
document.forms.setValue("David", forFormWithName: "FirstName") 
like image 636
Codicil Avatar asked Jan 30 '23 12:01

Codicil


1 Answers

Updated Answer

@objc func pdfPageChanged() {
    for index in 0..<doc!.pageCount{
        if let page = doc?.page(at: index){
            let annotations = page.annotations
            for annotation in annotations{
                print("Annotation Name :: \(annotation.fieldName ?? "")")
                if annotation.fieldName == "firstName"{
                    annotation.setValue("David", forAnnotationKey: .widgetValue)
                    page.removeAnnotation(annotation)
                    page.addAnnotation(annotation)
                }else if annotation.fieldName == "checkBox"{
                    annotation.buttonWidgetState = .onState
                    page.removeAnnotation(annotation)
                    page.addAnnotation(annotation)
                }
            }
        }
    }
}

doc is Object of PDFDocument

like image 140
Saurabh Prajapati Avatar answered Feb 04 '23 04:02

Saurabh Prajapati