我想用 Swift 写入 pdf 模板上的字段

发布于 2025-01-18 21:28:07 字数 2649 浏览 0 评论 0原文

我在python中制作了一个应用程序,该应用程序将用户输入带入PDF-Template的正确字段。

我现在想在Swift中这样做,但不知道如何做。

我将所有输入存储在变量中,例如。 self.Location.StringValue; 并想在PDF的位置场上打印它。

因此,当我按下完成按钮时,它会在正确的位置打印所有信息,并向我展示PDF。

在Python中,我使用一个lib呼叫fillpdf,它只是将我的dict打印到每个字段。 fillpdfs.write_fillable_pdf(pdf_path,f'{temp_filename}',data_dict)

我只是不知道如何在Swift中做到这一点,

这就是我到目前为止所拥有的:

//
//  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);
        
    }
    

}


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);
        
    }
    

}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文