Forms
List, fill, create, recalculate and flatten PDF form fields, and exchange form data as JSON, FDF or XFDF, with NexPDF for Go.
License module
Every form call needs the forms module (Indie tier and above).
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
type Field struct {
Name string `json:"name"`
Kind string `json:"kind"`
Value any `json:"value"` // string, bool, []any for list fields, or nil
Page int `json:"page"`
Llx float64 `json:"llx"`
Lly float64 `json:"lly"`
Urx float64 `json:"urx"`
Ury float64 `json:"ury"`
ReadOnly bool `json:"read_only"`
Required bool `json:"required"`
}
doc, err := nexpdf.Open("application.pdf")
if err != nil {
log.Fatal(err)
}
defer doc.Close()
s, err := doc.FormFieldsJSON()
if err != nil {
log.Fatal(err)
}
var fields []Field
json.Unmarshal([]byte(s), &fields)
for _, f := range fields {
fmt.Printf("%s %s %v required=%v\n", f.Name, f.Kind, f.Value, f.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.
doc.FillFormField("applicant.name", "Ana Silva")
doc.FillFormField("quantity", "4") // calculated totals update
doc.FillFormField("accept_terms", "true") // check boxes take "true" or "false"
doc.FillFormField("country", "Portugal") // list fields take the option text
doc.Save("filled.pdf", false)A value rejected by a validate script returns a *nexpdf.Error with the script's message.
Field appearances are drawn in embedded subset Unicode fonts (the bundled Liberation fonts, then
fallback fonts registered with nexpdf.RegisterFallbackFont), with OpenType shaping and Unicode bidi order, so Arabic,
Hebrew, Indic and CJK values display correctly. Right-to-left text without explicit alignment
starts at the right, and copying or extracting the text returns it in logical order.
Fill many fields from JSON
values, _ := json.Marshal(map[string]string{"applicant.name": "Ana Silva", "quantity": "4"})
doc.ImportFormJSON(string(values))
s, _ := doc.ExportFormJSON()
var current map[string]any
json.Unmarshal([]byte(s), ¤t)FDF and XFDF
fdf, err := doc.ExportFormFDF("application.pdf") // []byte
xfdf, err := doc.ExportFormXFDF("application.pdf") // string
blank, err := nexpdf.Open("application.pdf")
if err != nil {
log.Fatal(err)
}
defer blank.Close()
blank.ImportFormFDF(fdf)
blank.Save("from-fdf.pdf", false)Add fields
AddFormField(pageIndex, kind, name, llx, lly, urx, ury) creates a field. kind is text,
checkbox, radio, combobox, listbox, button, signature or barcode.
doc.AddFormField(0, "text", "customer", 100, 700, 300, 720)
doc.AddFormField(0, "checkbox", "newsletter", 100, 670, 114, 684)
doc.AddFormField(0, "signature", "signature", 100, 100, 300, 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.
f, _ := os.Open("signature.png")
src, _ := png.Decode(f)
img := image.NewNRGBA(src.Bounds())
draw.Draw(img, img.Bounds(), src, src.Bounds().Min, draw.Src)
w, h := img.Bounds().Dx(), img.Bounds().Dy()
if _, err := doc.SetSignatureAppearance("signature", img.Pix, uint32(w), uint32(h)); err != nil {
return err
}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
doc.RecalculateForms() // runs every calculate script; returns the number of fields changed
doc.FlattenForms() // burns the values into the page; the form is no longer editable
doc.Save("final.pdf", false)