简体   繁体   中英

How to Programmatically fill PDF form field using iOS PDFKit

在此处输入图片说明 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") 

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

它确实发生在我身上,我更改了PDF表单字段的字体大小并解决了问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM