WWD2017 pdfkit to fill out a PDF form

My quick application has a PDF form field in which all the field names are defined, I want to fill in these fields programmatically, however, following the WWDC presentation, PDFKit first draw a frame around the field, which means creating a form field before setting the valley as

textField.widgetStringVale = 'WWDC 2017'

My questions

1) is it possible to fill out a PDF form with fields already created in Adobe Acrobat in Swift programmatically, and not create it first using PDFKit

2) if not for 1) how to determine the absolute frame size for fields, since there are many fields, so I do not want trial and error

enter image description here

+5
source share
2 answers

, 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)
+2

,

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

Source: https://habr.com/ru/post/1686713/


All Articles