Forms
List, fill, create, recalculate and flatten PDF form fields, and exchange form data as JSON, FDF or XFDF, with NexPDF for Android.
License module
Every form call needs the forms module (Indie tier and above).
Every call below is a static method of NexPdfJni that takes doc.handle().
In the reader and in NexPdfViewerScreen (Compose), 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.
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 com.nexdevit.nexpdf.NexPdfDocument
import com.nexdevit.nexpdf.NexPdfJni
import org.json.JSONArray
NexPdfDocument.openBytes(bytes).use { doc ->
val fields = JSONArray(NexPdfJni.formFieldsJson(doc.handle()))
for (i in 0 until fields.length()) {
val f = fields.getJSONObject(i)
Log.i("NexPDF", "${f.getString("name")} ${f.getString("kind")} ${f.opt("value")} ${if (f.optBoolean("required")) "required" else ""}")
}
}Each field has name, kind, value, page, the rectangle (llx, lly, urx, ury),
read_only and required.
Fill fields
fillFormField(handle, name, value) sets a value as a viewer would: the field's keystroke and
validate scripts check it, then every calculated field is recomputed. It returns false if the
value is rejected, with the script's message in NexPdfJni.lastError().
val h = doc.handle()
NexPdfJni.fillFormField(h, "applicant.name", "Ana Silva")
NexPdfJni.fillFormField(h, "quantity", "4") // calculated totals update
NexPdfJni.fillFormField(h, "accept_terms", "true") // check boxes take "true" or "false"
if (!NexPdfJni.fillFormField(h, "country", "Portugal")) { // list fields take the option text
Log.w("NexPDF", NexPdfJni.lastError())
}
val filled = doc.saveBytes()Field appearances are drawn in embedded Unicode fonts (the bundled Liberation fonts, then registered fallback fonts), shaped with OpenType and ordered with Unicode bidi, so Arabic, Hebrew, Indic and other scripts show correctly. Right-to-left text without explicit alignment starts at the right.
Fill many fields from JSON
NexPdfJni.importFormJson(h, """{"applicant.name":"Ana Silva","quantity":"4"}""")
val values = JSONObject(NexPdfJni.exportFormJson(h))FDF and XFDF
val fdf = NexPdfJni.exportFormFdf(h, "application.pdf") // ByteArray
val xfdf = NexPdfJni.exportFormXfdf(h, "application.pdf") // String
NexPdfDocument.openBytes(blankBytes).use { blank ->
NexPdfJni.importFormFdf(blank.handle(), fdf) // or importFormXfdf
val fromFdf = blank.saveBytes()
}Add fields
addFormField(handle, pageIndex, kind, name, llx, lly, urx, ury) creates a field. kind is
text, checkbox, radio, combobox, listbox, button, signature or barcode.
NexPdfJni.addFormField(h, 0, "text", "customer", 100.0, 700.0, 300.0, 720.0)
NexPdfJni.addFormField(h, 0, "checkbox", "newsletter", 100.0, 670.0, 114.0, 684.0)
NexPdfJni.addFormField(h, 0, "signature", "signature", 100.0, 100.0, 300.0, 150.0)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. doc.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.
val bmp = BitmapFactory.decodeFile(path).copy(Bitmap.Config.ARGB_8888, false)
val rgba = ByteBuffer.allocate(bmp.byteCount).also { bmp.copyPixelsToBuffer(it) }.array() // RGBA order
doc.setSignatureAppearance("signature", rgba, bmp.width, bmp.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
NexPdfJni.recalculateForms(h) // runs every calculate script; returns the number of fields changed
NexPdfJni.flattenForms(h) // burns the values into the page; the form is no longer editable
val final = doc.saveBytes()