繁体   English   中英

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

[英]How do I fulfil annotations with my custom text within interactive pdf file using Swift and PDFKit?

我有一个交互式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 ...,并且...为空;)为什么? 我的bbcc标签在哪里?

只需使用widgetStringValuePDFAnnotation

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM