如何使用Swift和PDFKit在交互式pdf文件中使用自定义文本填写注释?

时间:2019-09-18 09:56:35

标签: ios swift pdfkit

我有一个交互式pdf文件,我只需要在ios应用程序中填写其文本字段即可。它可以是任何工具,无论是本机还是非本机。

它看起来像这样:

enter image description here

enter image description here

我需要用任何数据来完成该数组。可能吗?

使用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 ...,并且...为空;)为什么?我的bbcc标签在哪里?

1 个答案:

答案 0 :(得分:0)

只需将widgetStringValue用于PDFAnnotation

for annotation in document.page(at: 0)!.annotations {
    annotation.widgetStringValue = "value"
}