Skip to main content

acroform

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.

Features

  • Load PDF documents from files or bytes
  • List all form fields with their properties
  • Fill form fields with typed values
  • Save filled PDFs to files or bytes
  • Support for text, boolean, choice, and integer field types
  • Automatic UTF-16BE encoding for text fields
  • Hierarchical field name resolution

Usage

Add this to your Cargo.toml:

[dependencies]
acroform = "0.2.0"

Example

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(())
}

API

AcroFormDocument

The main struct for working with PDF forms.

Methods

  • from_pdf(path: impl AsRef<Path>) -> Result<Self> - Load a PDF from a file path
  • from_bytes(data: Vec<u8>) -> Result<Self> - Load a PDF from bytes
  • fields(&self) -> Result<Vec<FormField>> - Get all form fields
  • fill(&mut self, values: HashMap<String, FieldValue>) -> Result<Vec<u8>> - Fill fields and return PDF bytes
  • fill_and_save(&mut self, values: HashMap<String, FieldValue>, output: impl AsRef<Path>) -> Result<()> - Fill fields and save to file

FormField

Represents a form field with its properties.

Fields

  • name: String - The fully qualified field name (e.g., "parent.child.field")
  • field_type: FieldType - The type of the field
  • current_value: Option<FieldValue> - The current value
  • default_value: Option<FieldValue> - The default value
  • flags: u32 - Field flags (bit field)
  • tooltip: Option<String> - Tooltip text

FieldValue

Typed values for form fields.

Variants

  • Text(String) - Text string
  • Boolean(bool) - Boolean value (for checkboxes)
  • Choice(String) - Choice value (for radio buttons, list boxes, combo boxes)
  • Integer(i32) - Integer value

FieldType

PDF form field types.

Variants

  • Text - Text field (/Tx)
  • Button - Button field (/Btn) - includes checkboxes, radio buttons, and push buttons
  • Choice - Choice field (/Ch) - includes list boxes and combo boxes
  • Signature - Signature field (/Sig)
  • Unknown(String) - Unknown or custom field type

Implementation Details

String Encoding

Text fields are automatically encoded as UTF-16BE with BOM when filling forms, which ensures proper Unicode support across PDF viewers.

Field Hierarchy

Fields can be organized hierarchically in PDF forms. This library constructs fully qualified field names using dot notation (e.g., "parent.child.field").

Appearance Updates

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.

Python Bindings

High-performance Python bindings are available:

pip install acroform

Requires Python 3.12 or later.

import acroform

# Load a PDF form
doc = acroform.AcroFormDocument.from_pdf("form.pdf")

# List all fields
fields = doc.fields()
for field in fields:
    print(f"Field: {field.name} ({field.field_type})")

# Fill fields and save
doc.fill_and_save({"name": "John Doe"}, "filled_form.pdf")

See PYTHON_BINDINGS.md for detailed documentation.

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Release files for acroform 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for acroform 0.2.2
File Size Uploaded
acroform-0.2.2.tar.gz 165.1 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for acroform 0.2.2
File
acroform-0.2.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ x86-64 Details
acroform-0.2.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ ARM64 Details
acroform-0.2.2-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
acroform-0.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details
acroform-0.2.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ ARM64 Details
acroform-0.2.2-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
acroform-0.2.2-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details

Total release size: 4.0 MB

Release files / acroform-0.2.2.tar.gz

Download URL acroform-0.2.2.tar.gz
Size 165.1 kB
Tags Source
SHA-256 checksum
How to use checksums
54727d14c140b8ac839f197dbf616cd27f96e1c6da626f51c6cddaf5dd279190
BLAKE2b-256 checksum
How to use checksums
6debe3666e45347a0544a266f760a7c7a2a4f5681968483b96890699f008d2f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.12.6

Release files / acroform-0.2.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL acroform-0.2.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 567.0 kB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
b4fd625dc9787a140da6d857d6235c026576c237e90963c51f2938640f31b2c8
BLAKE2b-256 checksum
How to use checksums
53de360a91af3c3f6127f0b3dc1d5ac24182713ef70ffe5cfdd8264724aafd19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.12.6

Release files / acroform-0.2.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL acroform-0.2.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 536.9 kB
Tags CPython 3.13 Linux glibc 2.17+ ARM64
SHA-256 checksum
How to use checksums
7db8633fa2e25bc3a48ab92560db38b920125bc9ed6d289a019e310c94f195e3
BLAKE2b-256 checksum
How to use checksums
3b545648873de2b0ed1eed9e6e40469c5b69fcddc08d4c61d9348174cc4e5806
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.12.6

Release files / acroform-0.2.2-cp313-cp313-macosx_11_0_arm64.whl

Download URL acroform-0.2.2-cp313-cp313-macosx_11_0_arm64.whl
Size 546.2 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
bc4842ecefa49407b3ac0a97c47ba620deb7602928e597e9f87e1540b01ba0e5
BLAKE2b-256 checksum
How to use checksums
4b2a3852a878dd20044bfccb3f595d2bb1ef3038c060016f1fabc52e1fd2d136
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.12.6

Release files / acroform-0.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL acroform-0.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 567.6 kB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
9dad1ad06fa03525b4511ee0ef79fa5a22973fa48d0c8aaca9a5e0e8545d9659
BLAKE2b-256 checksum
How to use checksums
9b127011f3500c3809195446b79f905b36027f4e6e643463d2e1eaa2a779ea30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.12.6

Release files / acroform-0.2.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL acroform-0.2.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 536.6 kB
Tags CPython 3.12 Linux glibc 2.17+ ARM64
SHA-256 checksum
How to use checksums
177d9fa0a5832b8de0d5d2d14d67a554cdb6ea394bd812ae23261d3b19516d56
BLAKE2b-256 checksum
How to use checksums
df9fb380f10ab96b3c189f8b23169ee9b4ceb3ae63843fb4a9e24db236ce7592
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.12.6

Release files / acroform-0.2.2-cp312-cp312-macosx_11_0_arm64.whl

Download URL acroform-0.2.2-cp312-cp312-macosx_11_0_arm64.whl
Size 546.6 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
9f3fbba7131bd9965fd3e00f492fd28372b757d669d1c7db671d48c9ef4a65f0
BLAKE2b-256 checksum
How to use checksums
fb646a606d65114f6a5efbe0ed305658062b0b9ba1a948f7407c08f9a6a49c32
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.12.6

Release files / acroform-0.2.2-cp311-cp311-macosx_11_0_arm64.whl

Download URL acroform-0.2.2-cp311-cp311-macosx_11_0_arm64.whl
Size 499.8 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e3a5e9c007fa8d659766dfcd372adbd57897e914e598dc7d1b4f0263bfbecfaa
BLAKE2b-256 checksum
How to use checksums
597c69789287214053af86b29505f9cd908ee7fe55fc58e1b4f8b1684ec72d60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.12.6

Release history Release notifications | RSS feed

This release

0.2.2 This release

8 release files

0.2.1

8 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page