ios - 如何使用 Swift 和 PDFKit 在交互式 pdf 文件中使用自定义文本完成注释?

标签 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 标签在哪里?

最佳答案

只需将 widgetStringValue 用于您的 PDFAnnotation:

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

关于ios - 如何使用 Swift 和 PDFKit 在交互式 pdf 文件中使用自定义文本完成注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57990001/

相关文章:

ruby-on-rails - 使用 PDFKit 开始新的一页

ios - 应该在 UITableViewCell 中调用 deinit 吗?

ios - iOS中的目标和 bundle 之间的区别?

objective-c - 如何知道字符串中的前 5 个字符

ios - 在 Segue 完成之前按下按钮 iOS + Swift

swift - 向下滚动时显示导航栏

ruby-on-rails-3 - pdfkit 无法在 Rails 3.1 中正确呈现

ruby - 如何使用 github 存储库中的新 afm 构建 pdfkit

ios - 无法使卡片翻转动画起作用

ios - Storyboard中的 Xcode 6 UIVisualEffectView