Skip to Content
NexPDF

Forms

List, fill, create, recalculate and flatten PDF form fields, and exchange form data as JSON, FDF or XFDF, with NexPDF for iOS (Swift).

English

License module

Every form call needs the forms module (Indie tier and above).

In the reader, the user types straight into text fields on the page, ticks check boxes and radio buttons and picks from lists; each change runs the field's scripts and the page is drawn again. Tapping a signature field opens Add signature, with Draw (black, blue or indigo ink), Image and Type tabs, and puts the picture in the field. Read-only and digitally signed fields are not editable. Filling is off with modeConfig.readOnly; the forms mode also adds new fields. The calls below do the same from code.

This page covers AcroForm fields. For XFA forms (XML Forms Architecture), see XFA forms. To fill a PDF from JSON data with placeholders, see Templates.

List the fields

import NexPdf

let doc = try NexPdfDocument.open(url: url)
let fields = NexPdf.json(try doc.formFieldsJson()) as? [[String: Any]] ?? []
for f in fields {
    print(f["name"] ?? "", f["kind"] ?? "", f["value"] ?? "", (f["required"] as? Bool) == true ? "required" : "")
}

Each field has name, kind, value, page, the rectangle (llx, lly, urx, ury), read_only and required.

Fill fields

fillFormField(name:value:) sets a value as a viewer would: the field's keystroke and validate scripts check it, then every calculated field is recomputed.

try doc.fillFormField(name: "applicant.name", value: "Ana Silva")
try doc.fillFormField(name: "quantity", value: "4")          // calculated totals update
try doc.fillFormField(name: "accept_terms", value: "true")   // check boxes take "true" or "false"
try doc.fillFormField(name: "country", value: "Portugal")    // list fields take the option text
try doc.write(to: outputURL)

A value rejected by a validate script throws NexPdfError with the script's message.

The new appearance of a text field is drawn in an embedded Unicode font (the bundled Liberation fonts, then any fallback font registered with NexPdf.registerFallbackFont(data:)), shaped with OpenType and ordered with the Unicode bidi algorithm, so Arabic, Hebrew, Indic and CJK values show as typed. Right-to-left text without an explicit alignment starts at the right, and copying or extracting the text returns it in logical order.

Fill many fields from JSON

try doc.importFormJson(json: #"{"applicant.name":"Ana Silva","quantity":"4"}"#)
let values = NexPdf.json(try doc.exportFormJson())

FDF and XFDF

let fdf = try doc.exportFormFdf(pdfName: "application.pdf")     // Data
let xfdf = try doc.exportFormXfdf(pdfName: "application.pdf")   // String

let blank = try NexPdfDocument.open(url: blankURL)
try blank.importFormFdf(data: fdf)                              // or importFormXfdf(xfdf:)
try blank.write(to: outputURL)

Add fields

addFormField(pageIndex:kind:name:llx:lly:urx:ury:) creates a field. kind is text, checkbox, radio, combobox, listbox, button, signature or barcode.

try doc.addFormField(pageIndex: 0, kind: "text", name: "customer", llx: 100, lly: 700, urx: 300, ury: 720)
try doc.addFormField(pageIndex: 0, kind: "checkbox", name: "newsletter", llx: 100, lly: 670, urx: 114, ury: 684)
try doc.addFormField(pageIndex: 0, kind: "signature", name: "signature", llx: 100, lly: 100, urx: 300, ury: 150)

Put a signature picture in a signature field

A drawn, typed or scanned signature can be shown in a signature field without signing the document digitally, as when a person signs a form on screen. setSignatureAppearance(name:rgba:width:height:) takes the picture as RGBA pixels (width × height × 4 bytes; transparent pixels stay transparent), scales it to fit every widget of the field and centres it. The field must be a signature field that is not read-only. A later digital signature into the same field replaces the picture. The field list then reports has_appearance: true for the field.

// rgba: Data with the RGBA pixels of a CGImage drawn into an 8-bit RGBA context.
try doc.setSignatureAppearance(name: "signature", rgba: rgba, width: UInt32(width), height: UInt32(height))

The field list also describes how each field is drawn, so a custom UI can lay its own controls over the page: align (left, center or right), font_size (null when the size is automatic), text_color (#rrggbb); text fields add multiline, password, comb, scroll and max_len; signature fields add signed (signed digitally) and has_appearance.

Recalculate and flatten

try doc.recalculateForms()   // runs every calculate script; returns the number of fields changed
try doc.flattenForms()       // burns the values into the page; the form is no longer editable
try doc.write(to: outputURL)

On this page