Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用UITextField中的信息填写PDF文档_Ios_Swift_Ios Pdfkit - Fatal编程技术网

Ios 使用UITextField中的信息填写PDF文档

Ios 使用UITextField中的信息填写PDF文档,ios,swift,ios-pdfkit,Ios,Swift,Ios Pdfkit,我有一个PDF文档存储在我的应用程序的主捆绑包中,还有一个带有文本字段的ViewController供用户输入。我还有一个按钮,可以通过电子邮件将填写好的PDF发送给用户。我不需要将PDF保存到设备 我想使用PDFKit来实现这一点,但我还没有弄清楚如何实现这一点 我尝试了以下代码: if let path = Bundle.main.path(forResource: "User", ofType: "pdf") { let url = URL(fileURLWithPath: pat

我有一个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)
    }
}
如果let path=Bundle.main.path(forResource:“User”,of type:“pdf”){
让url=url(fileURLWithPath:path)
如果let doc=PDFDocument(url:url){

对于0..中的索引,我认为您可以替换行:

annotation.setValue(firstnameField.text, forAnnotationKey: .widgetValue)

如果这不起作用(可能这不是文本或自由文本批注),也可以创建自由文本批注并将其放在那里:

let textObj = PDFAnnotation(bounds: annotation.bounds, forType: .freeText, withProperties: nil)
textObj.contents = firstnameField.text
textObj.color = UIColor.clear
page.addAnnotation(textObj)

希望这能有所帮助。

我想您可以更换线路:

annotation.setValue(firstnameField.text, forAnnotationKey: .widgetValue)

如果这不起作用(可能这不是文本或自由文本批注),也可以创建自由文本批注并将其放在那里:

let textObj = PDFAnnotation(bounds: annotation.bounds, forType: .freeText, withProperties: nil)
textObj.contents = firstnameField.text
textObj.color = UIColor.clear
page.addAnnotation(textObj)

希望这能有所帮助。

什么是错误,或者这段代码在哪里失败?什么是错误,或者这段代码在哪里失败?谢谢!添加新注释对我有效,因为某些原因,我在Acrobat中添加的字段不起作用。谢谢!添加新注释对我有效,因为某些原因,我在Acro中添加的字段对我有效蝙蝠不工作了。