'I want to write to a field on a pdf template in Swift

I made an app in Python that takes user inputs and puts them in the right field of an pdf-template.

I want to do this in swift now, but can't figure out how.

I store all the inputs in variables eg. self.location.stringValue; and would like to print it on the location-field on the pdf.

So when I hit the done button, it prints all the info on the right place and shows me a pdf.

in Python I use a lib call fillpdf, which just prints my dict to every field. fillpdfs.write_fillable_pdf(pdf_path, f'{temp_fileName}', data_dict)

I just can't figure out how to do that in swift

This is what I've got so far:

//
//  ViewController.swift
//  PDF
//
//  Created by Robin Ellingsen on 2022-04-04.
//

import PDFKit
import Cocoa



class ViewController: NSViewController {
    
    var toReturn: Bool? = false
    var buyOut: Bool? = false
    var findmy: Bool? = false
    var looksOk: Bool? = false
    var app : Bool? = false
    var charger : Bool? = false
    var closed : Bool? = false
    
    @IBOutlet weak var location: NSTextField!
    
    @IBOutlet weak var receiver: NSTextField!
    
    @IBOutlet weak var number: NSTextField!
    
    @IBOutlet weak var company: NSTextField!
    
    @IBOutlet weak var name: NSTextField!
    
    @IBOutlet weak var phone: NSTextField!
    
    @IBOutlet weak var email: NSTextField!
    
    @IBOutlet weak var product: NSTextField!
    
    @IBOutlet weak var serial: NSTextField!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        guard let pdfURL = Bundle.main.url(forResource: "mSmart_util", withExtension: "pdf") else { return }
        
        
        let document = PDFDocument(url: pdfURL)
        let page1 = document!.page(at: 0)

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

        let saveURL = URL(string: "/Users/robin/Desktop/testpdf")
        document!.write(to: saveURL!)
        // Do any additional setup after loading the view.
    }
    //Lock window size
    override func viewDidAppear() {
        self.view.window?.styleMask.remove(NSWindow.StyleMask.resizable)
    }

    override var representedObject: Any? {
        didSet {
        
        // Update the view, if already loaded.
        }
    }
    @IBAction func returned(_ sender: Any) {
        toReturn = true;
    }
    
    @IBAction func done(_ sender: Any) {
        printer()
        
    }
    func printer() {
        print(self.location.stringValue);
        print(self.receiver.stringValue);
        print(self.number.stringValue);
        print(self.company.stringValue);
        print(self.name.stringValue);
        print(self.phone.stringValue);
        print(self.email.stringValue);
        print(self.product.stringValue);
        print(self.serial.stringValue);
        
    }
    

}




Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source