在我的应用程序中,用户将在一个屏幕上输入信息,然后将其放入数据库。在另一个屏幕上,我想显示一个 pdf,其中包含用于填写编辑文本字段(注释)的信息。在下面的示例中,用户当前可以单击“John Smith”并输入新名称。
使用 ios PDFKit 我能够显示 pdf 并允许用户进行编辑。我无法更新文本框中的文本或更改复选框的状态。看起来我确实有 pdf 的读/写权限。
使用我当前的代码,我能够获取我的 pdf 的页数并打印出每个注释的注释名称,这样我就可以获得我想要为其设置值但 setvalue 函数不起作用并检查的字段的名称填充文本框的内容显示为零。
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)
}
}
}
}
}
}
我的实现是否存在问题,或者是否有另一种以编程方式更新 pdf 注释的方法?