如何使用 iOS PDFKit 以编程方式填写 PDF 表单域
How to Programmatically fill PDF form field using iOS PDFKit
在我的应用程序中,我有一个基于用户输入的 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")
更新答案
@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 是 PDFDocument 的对象
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")
更新答案
@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 是 PDFDocument 的对象