Forms
List, fill, create, recalculate and flatten PDF form fields, and exchange form data as JSON, FDF or XFDF, with NexPDF for C++.
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
#include <nexpdf.hpp>
#include <nlohmann/json.hpp> // an example; use your JSON library
namespace nexpdf = nexdevit::nexpdf;
auto doc = nexpdf::Document::open("application.pdf");
for (const auto& f : nlohmann::json::parse(doc.form_fields_json())) {
std::cout << f["name"] << " " << f["kind"] << " " << f["value"]
<< (f["required"].get<bool>() ? " required" : "") << "\n";
}Each field has name, kind, value, page, the rectangle (llx, lly, urx, ury),
read_only and required.
Fill fields
fill_form_field(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.fill_form_field("applicant.name", "Ana Silva");
doc.fill_form_field("quantity", "4"); // calculated totals update
doc.fill_form_field("accept_terms", "true"); // check boxes take "true" or "false"
doc.fill_form_field("country", "Portugal"); // list fields take the option text
doc.save("filled.pdf");A value rejected by a validate script throws nexdevit::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::register_fallback_font), 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
nlohmann::json values = {{"applicant.name", "Ana Silva"}, {"quantity", "4"}};
doc.import_form_json(values.dump());
auto current = nlohmann::json::parse(doc.export_form_json());FDF and XFDF
std::vector<uint8_t> fdf = doc.export_form_fdf("application.pdf");
std::string xfdf = doc.export_form_xfdf("application.pdf");
auto blank = nexpdf::Document::open("application.pdf");
blank.import_form_fdf(fdf);
blank.save("from-fdf.pdf");Add fields
add_form_field(page_index, kind, name, llx, lly, urx, ury) creates a field. kind is text,
checkbox, radio, combobox, listbox, button, signature or barcode.
doc.add_form_field(0, "text", "customer", 100, 700, 300, 720);
doc.add_form_field(0, "checkbox", "newsletter", 100, 670, 114, 684);
doc.add_form_field(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. set_signature_appearance(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.
std::vector<uint8_t> rgba = load_rgba("signature.png", &width, &height); // your image loader
doc.set_signature_appearance("signature", rgba, width, 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.recalculate_forms(); // runs every calculate script; returns the number of fields changed
doc.flatten_forms(); // burns the values into the page; the form is no longer editable
doc.save("final.pdf");