我在应用程序的主捆绑包中存储了一个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)
}
}
任何帮助,将不胜感激。
乔什
答案 0 :(得分:0)
我认为您可以替换行:
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)
希望这会有所帮助。