WWD2017 pdfkit填写PDF表格

时间:2017-10-01 07:18:13

标签: ios swift ios11 pdfkit

在我的swift应用程序中有PDF表单字段,其中定义了所有字段名称,我想以编程方式填充这些字段,但是观看WWDC演示文稿,PDFKit首先在字段周围绘制一个框架,这意味着创建表单首先将字段设置为

textField.widgetStringVale =“WWDC 2017”

我的问题是

1)是否可以使用swift以编程方式在adobe acrobat中创建的字段填写PDF表单,而不是首先使用PDFKit创建

2)如果不是1)如何确定字段的绝对帧大小,因为有很多字段所以我不想尝试和错误

enter image description here

2 个答案:

答案 0 :(得分:2)

是的,您可以使用PDFKit以编程方式填写PDF表单:

import UIKit
import PDFKit

let pdfURL = ...
let document = PDFDocument(url: pdfURL!)
let page1 = document!.page(at: 0)

let field = page1?.annotations.filter({ $0.fieldName! == "ConferenceName" })
field?[0].widgetStringValue = "WWDC 2017"

let saveURL = ...
document!.write(to: saveURL)

答案 1 :(得分:0)

我只会添加一个也可以用于测试目的的保存功能

import UIKit
import PDFKit

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

 let pdfURL = ...
 let document = PDFDocument(url: pdfURL!)
 let page1 = document!.page(at: 0)

 let field = page1?.annotations.filter({ $0.fieldName! == "ConferenceName" })
 field?[0].widgetStringValue = "WWDC 2017"

 savePDF()
}

 func savePDF() {
    #if targetEnvironment(simulator)
    let path = URL(string: "file:///Users/bbird/Desktop/PDFKit/awesome.pdf")!
    #else
    let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last!.appendingPathComponent("awesome.pdf")
    #endif
    print(path)
    self.pdfView?.document?.write(to: path) //We don't really need to write this here - just demo'ing code
   }