我有一个交互式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
标签在哪里?
答案 0 :(得分:0)
只需将widgetStringValue
用于PDFAnnotation
:
for annotation in document.page(at: 0)!.annotations {
annotation.widgetStringValue = "value"
}