Populating PDF with Cocoa - Use NSTextField contents to populate a PDF text field?

110 Views Asked by At

I have a simple macOS app programmed in cocoa with several NSTextFields that I would like to use as a means to gather some information such as name and telephone number from a customer. I would like to send that data to a blank PDF form, that I have created separately at a drive location, on execution of a button action.

The PDF form has known text field names, but how do I pass the data from the macOS app to the corresponding PDF text fields?

Thanks

1

There are 1 best solutions below

0
On

I haven't seen an easy way to do this with just the built in cocoa classes. PDFKit has PDFDocument, PDFPage and so on, but it's not clear you can modify the document via PDFKit. There's a commercial SDK PSPDFKit that can do what you want, but I think the reason they exist is because it's not easy to do with Cocoa itself.

One way without buying a third party SDK is to describe your form templates in XML (or something) and then create the view using native AppKit controls and then save it as a PDF. See the Quartz and PDFKit Programming Guide in particular.

Note that you don't have to display the full template - the user can edit in a simplified window and then you can build the full view from your template description (XML say) offscreen view and then use [NSView dataWithPDF(inside:)][3] to get the NSDat