WWD2017 pdfkit to fill PDF form

Viewed 2909

In my swift application there is PDF form field which with all the fields name defined, I want to fill these fields programmatically, however watching the WWDC presentation, PDFKit first draw a frame around the field, meaning creating form field first than setting the vale as

textField.widgetStringVale = “WWDC 2017”

My questions are

1) is it possible to fill the PDF form with fields already created in adobe acrobat in swift programmatically instead of creating first using PDFKit

2) if not for 1) how to determine the absolute frame size for fields as there are many field so I don’t want to trial and error

enter image description here

2 Answers

I would just add a save function that works for testing purposes too

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
   }
Related