ios - 使用 UITextField 中的信息填写 PDF 文档
问题描述
我有一个 PDF 文档存储在我的应用程序的主包中,还有一个带有文本字段供用户输入的 ViewController。我还有一个按钮,可以将填写好的 PDF 通过电子邮件发送给用户。我不需要将 PDF 保存到设备
我想使用 PDFKit 来做到这一点,但我无法弄清楚如何实现这一点。
我尝试了以下代码:
if let path = Bundle.main.path(forResource: "User", ofType: "pdf") {
let url = URL(fileURLWithPath: path)
if let doc = PDFDocument(url: url) {
for index in 0..<doc!.pageCount{
if let page = doc?.page(at: index){
let annotations = page.annotations
for annotation in annotations{
print("Annotation Name :: \(annotation.fieldName ?? "")")
if annotation.fieldName == "firstname"{
annotation.setValue(firstnameField.text, forAnnotationKey: .widgetValue)
page.removeAnnotation(annotation)
page.addAnnotation(annotation)
}
}
}
}
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients([emailField.text])
mailComposer.setSubject("User Information")
mailComposer.setMessageBody("User Information for \(firstnameField.text).", isHTML: true)
mailComposer.addAttachmentData(doc.dataRepresentation()!, mimeType: "application/pdf", fileName: "User")
self.present(mailComposer, animated: true, completion: nil)
}
}
对此的任何帮助将不胜感激。
乔什
解决方案
我认为您可以替换行:
annotation.setValue(firstnameField.text, forAnnotationKey: .widgetValue)
和
annotation.contents = firstnameField.text
如果这不起作用(也许这不是文本或自由文本注释),您还可以创建自由文本注释并将其放在那里:
let textObj = PDFAnnotation(bounds: annotation.bounds, forType: .freeText, withProperties: nil)
textObj.contents = firstnameField.text
textObj.color = UIColor.clear
page.addAnnotation(textObj)
希望这可以帮助。
推荐阅读
- python - 结果合并两个字典列表并更新键
- javascript - jquery如何从数组中删除元素
- html - 是否可以在 Bootstrap 4 中创建一个高度等于其宽度的列?
- java - DateTimeFormatter 个位数月份
- javascript - 如何避免在Javascript中舍入具有超过16位精度的数字
- laravel-5 - UnexpectedValueException:响应内容必须是实现__toString()的字符串或对象,“boolean”给定
- aerospike - Aerospike 可以只返回指定的垃圾箱吗?
- github - how do i configure default view of files in github? (to treat files as json)
- jquery - jQuery keypress 获取第一个单词
- python - 使用条件 sympy 检查相等性