我有一个交互式 pdf 文件,我所需要的只是在我的 ios 应用程序中填写其文本字段。它可以是任何工具,无论是否是原生的。
简单来说,它看起来像这样:
我需要用任何数据来填充该数组。有可能吗?
使用PDFKit
它看起来像这样:
let url = Bundle.main.path(forResource: "a", ofType: "pdf")!
let path = URL(fileURLWithPath: url)
let document = PDFDocument(url: path) //PDFDocument
let page1 = document!.page(at: 0)! //PDFPage
let page2 = document!.page(at: 1)! //PDFPage
let annotations = page1.annotations //[PDFAnnotation] count is 380, voilaa;)
在这里我可以访问每个注释/文本字段。但问题是:
如何使用自定义字符串填充注释?
我尝试以下操作:
for annotation in annotations {
annotation.setValue("bb", forAnnotationKey: PDFAnnotationKey.textLabel)
annotation.setValue("cc", forAnnotationKey: PDFAnnotationKey.contents)
}
let name = "output.pdf"
let output = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(name)
document.write(to: output)
let controller = UIActivityViewController(activityItems: [output], applicationActivities: nil)
present(controller, animated: true)
然后我将其保存到 iBooks...并且...它是空的;)为什么?我的 bb
或 cc
标签在哪里?
最佳答案
只需将 widgetStringValue
用于您的 PDFAnnotation
:
for annotation in document.page(at: 0)!.annotations {
annotation.widgetStringValue = "value"
}
关于ios - 如何使用 Swift 和 PDFKit 在交互式 pdf 文件中使用自定义文本完成注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57990001/