Form Fields
Commercial License Required - This feature requires a commercial license. See printwell.dev/pricing.
Create interactive PDF forms with text fields, checkboxes, dropdowns, and signature fields.
Field Types
| Type | Description |
|---|---|
| Text | Single or multi-line text input |
| Checkbox | Boolean checkbox |
| Dropdown | Selection from options |
| Signature | Digital signature field |
Adding Form Fields
#![allow(unused)]
fn main() {
use printwell::forms::{add_form_fields, TextField, Checkbox, Dropdown, SignatureField, Rect};
let fields = FormFields {
text_fields: vec![
TextField {
name: "full_name".into(),
page: 1,
rect: Rect::new(100.0, 700.0, 200.0, 20.0),
default_value: None,
required: true,
..Default::default()
},
TextField {
name: "email".into(),
page: 1,
rect: Rect::new(100.0, 660.0, 200.0, 20.0),
required: true,
..Default::default()
},
],
checkboxes: vec![
Checkbox {
name: "agree_terms".into(),
page: 1,
rect: Rect::new(100.0, 620.0, 15.0, 15.0),
checked: false,
export_value: "Yes".into(),
},
],
dropdowns: vec![
Dropdown {
name: "country".into(),
page: 1,
rect: Rect::new(100.0, 580.0, 150.0, 20.0),
options: vec!["USA".into(), "Canada".into(), "UK".into()],
selected_index: None,
editable: false,
},
],
signature_fields: vec![
SignatureField {
name: "signature".into(),
page: 1,
rect: Rect::new(100.0, 500.0, 200.0, 50.0),
},
],
};
let result = add_form_fields(&pdf_data, &fields)?;
}
Text Field Options
#![allow(unused)]
fn main() {
TextField {
name: "notes".into(),
page: 1,
rect: Rect::new(100.0, 400.0, 300.0, 100.0),
multiline: true, // Multi-line text area
password: false, // Password field
max_length: Some(500), // Character limit
required: true,
read_only: false,
font_size: 12.0,
font_name: Some("Helvetica".into()),
default_value: Some("Default text".into()),
}
}
Form Validation
Validate form field values against rules:
#![allow(unused)]
fn main() {
use printwell::forms::{validate_form_fields, ValidationRule};
let rules = vec![
ValidationRule {
field_name: "full_name".into(),
required: true,
min_length: Some(2),
max_length: Some(100),
..Default::default()
},
ValidationRule {
field_name: "email".into(),
required: true,
pattern: Some(r"^[^@]+@[^@]+\.[^@]+$".into()),
pattern_message: Some("Invalid email format".into()),
..Default::default()
},
ValidationRule {
field_name: "age".into(),
min_value: Some(18.0),
max_value: Some(120.0),
..Default::default()
},
];
let summary = validate_form_fields(&form_elements, &rules);
if !summary.all_valid {
for result in &summary.results {
if !result.is_valid {
println!("{}: {:?}", result.field_name, result.errors);
}
}
}
}
Node.js Example
import { addFormFields, validateFormFields } from 'printwell';
const result = addFormFields(pdfData, [
{
fieldType: 'text',
name: 'full_name',
page: 1,
rect: { x: 100, y: 700, width: 200, height: 20 },
required: true
},
{
fieldType: 'checkbox',
name: 'agree',
page: 1,
rect: { x: 100, y: 650, width: 15, height: 15 }
}
]);
const validation = validateFormFields(elements, [
{ fieldName: 'full_name', required: true, minLength: 2 }
]);
Python Example
from printwell import add_form_fields, TextField, FormCheckbox, FormRect
result = add_form_fields(
pdf_data,
text_fields=[
TextField(
name="full_name",
page=1,
rect=FormRect(x=100, y=700, width=200, height=20),
required=True
)
],
checkboxes=[
FormCheckbox(
name="agree",
page=1,
rect=FormRect(x=100, y=650, width=15, height=15)
)
]
)