如何使用iOS PDFKit以编程方式填写PDF表单字段

Cod*_*cil 4 annotations pdfkit ios swift

在此处输入图片说明 在我的应用程序中,我有一个基于用户输入的PDF表单。我在PDF表单中强制输入该值。我ILPDFKit在我的应用程序中使用了pod来执行此操作(swift 3,但是由于PDFKit在iOS 11中已经存在,所以我想知道使用PDFKit是否可以实现)。我使用的代码ILPDFKit是:

// 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") 
Run Code Online (Sandbox Code Playgroud)

Sau*_*ati 6

更新的答案

@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)
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

doc是PDFDocument的对象

  • 我知道我有点晚了,但是使用 `annotation.widgetStringValue = "heeeeeloo"` 而不是 `setValue` 进行文本输入注释适用于 iOS 13。 (2认同)