| Crates.io | acroform |
| lib.rs | acroform |
| version | 0.1.3 |
| created_at | 2025-10-15 14:43:29.446757+00 |
| updated_at | 2025-10-18 21:16:58.803071+00 |
| description | High-level PDF form manipulation library using lopdf |
| homepage | |
| repository | https://github.com/nibsbin/acroform-rs-lopdf |
| max_upload_size | |
| id | 1884526 |
| size | 343,741 |
A high-level PDF form manipulation library using lopdf.
This crate provides a simple API for reading and filling PDF forms (AcroForms). It uses the official lopdf crate for PDF operations.
Add this to your Cargo.toml:
[dependencies]
acroform = "0.1.0"
use acroform::{AcroFormDocument, FieldValue};
use std::collections::HashMap;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load a PDF with form fields
let mut doc = AcroFormDocument::from_pdf("form.pdf")?;
// List all fields
let fields = doc.fields()?;
for field in &fields {
println!("Field: {} ({})", field.name, field.field_type);
}
// Fill fields
let mut values = HashMap::new();
values.insert("name".to_string(), FieldValue::Text("John Doe".to_string()));
values.insert("age".to_string(), FieldValue::Integer(30));
values.insert("subscribe".to_string(), FieldValue::Boolean(true));
// Save filled PDF
doc.fill_and_save(values, "filled_form.pdf")?;
Ok(())
}
AcroFormDocumentThe main struct for working with PDF forms.
from_pdf(path: impl AsRef<Path>) -> Result<Self> - Load a PDF from a file pathfrom_bytes(data: Vec<u8>) -> Result<Self> - Load a PDF from bytesfields(&self) -> Result<Vec<FormField>> - Get all form fieldsfill(&mut self, values: HashMap<String, FieldValue>) -> Result<Vec<u8>> - Fill fields and return PDF bytesfill_and_save(&mut self, values: HashMap<String, FieldValue>, output: impl AsRef<Path>) -> Result<()> - Fill fields and save to fileFormFieldRepresents a form field with its properties.
name: String - The fully qualified field name (e.g., "parent.child.field")field_type: FieldType - The type of the fieldcurrent_value: Option<FieldValue> - The current valuedefault_value: Option<FieldValue> - The default valueflags: u32 - Field flags (bit field)tooltip: Option<String> - Tooltip textFieldValueTyped values for form fields.
Text(String) - Text stringBoolean(bool) - Boolean value (for checkboxes)Choice(String) - Choice value (for radio buttons, list boxes, combo boxes)Integer(i32) - Integer valueFieldTypePDF form field types.
Text - Text field (/Tx)Button - Button field (/Btn) - includes checkboxes, radio buttons, and push buttonsChoice - Choice field (/Ch) - includes list boxes and combo boxesSignature - Signature field (/Sig)Unknown(String) - Unknown or custom field typeText fields are automatically encoded as UTF-16BE with BOM when filling forms, which ensures proper Unicode support across PDF viewers.
Fields can be organized hierarchically in PDF forms. This library constructs fully qualified field names using dot notation (e.g., "parent.child.field").
When filling forms, the library sets the NeedAppearances flag, which tells PDF viewers to regenerate field appearances. This ensures that filled values are properly displayed.
MIT
Contributions are welcome! Please feel free to submit a Pull Request.