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 .NET.

English

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

using System.Text.Json.Nodes;
using Nexdevit.NexPdf;

using var doc = NexPdfDocument.Open("application.pdf");
foreach (var f in JsonNode.Parse(doc.FormFieldsJson())!.AsArray())
{
    var required = f!["required"]!.GetValue<bool>() ? "required" : "";
    Console.WriteLine($"{f["name"]} {f["kind"]} {f["value"]?.ToJsonString()} {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");

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

Field appearances shape text with OpenType and order it with Unicode bidi, in embedded Unicode fonts (the bundled Liberation fonts, then fallback fonts registered with NexPdfSdk.RegisterFallbackFont(data)), so Arabic, Hebrew, Indic and CJK values display correctly. Right-to-left text without an explicit alignment starts at the right.

Fill many fields from JSON

using System.Text.Json;

doc.ImportFormJson(JsonSerializer.Serialize(new Dictionary<string, string>
{
    ["applicant.name"] = "Ana Silva",
    ["quantity"] = "4",
}));
var values = JsonNode.Parse(doc.ExportFormJson());

FDF and XFDF

byte[] fdf = doc.ExportFormFdf("application.pdf");
string xfdf = doc.ExportFormXfdf("application.pdf");

using var blank = NexPdfDocument.Open("application.pdf");
blank.ImportFormFdf(fdf);
blank.Save("from-fdf.pdf");

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.

// rgba: width * height * 4 bytes, for example from SkiaSharp's SKBitmap.Bytes (Rgba8888).
doc.SetSignatureAppearance("signature", rgba, (uint)width, (uint)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

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

On this page