use std::collections::{BTreeMap, BTreeSet};
use lopdf::{Dictionary, Document, Object, ObjectId, Stream, StringFormat};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FormFieldKind {
Text,
CheckBox,
RadioButton,
ComboBox,
ListBox,
PushButton,
Signature,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FormWidget {
pub page: usize,
pub rect: [f32; 4],
pub on_state: String,
pub hidden: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FormField {
pub name: String,
pub kind: FormFieldKind,
pub value: String,
pub default_value: String,
pub options: Vec<String>,
pub widgets: Vec<FormWidget>,
pub read_only: bool,
pub required: bool,
pub multiline: bool,
pub password: bool,
pub max_len: Option<u32>,
pub font_size: f32,
pub alignment: u8,
}
impl FormField {
#[must_use]
pub fn is_on(&self, widget: &FormWidget) -> bool {
is_on_value(&self.value, widget)
}
}
fn is_on_value(value: &str, widget: &FormWidget) -> bool {
match value {
"" | "Off" | "false" | "0" => false,
"true" | "on" | "On" | "Yes" if widget.on_state.is_empty() => true,
v => v == widget.on_state || (v == "true" && !widget.on_state.is_empty()),
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldValue {
pub name: String,
pub value: String,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum StampSegment {
MoveTo(f32, f32),
LineTo(f32, f32),
CubicTo(f32, f32, f32, f32, f32, f32),
Close,
}
#[derive(Debug, Clone, PartialEq)]
pub struct StampPath {
pub segments: Vec<StampSegment>,
pub stroke: Option<(f32, [f32; 3])>,
pub fill: Option<[f32; 3]>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FormStamp {
pub page: usize,
pub rect: [f32; 4],
pub view_box: [f32; 4],
pub paths: Vec<StampPath>,
}
const ANNOT_HIDDEN: i64 = 1 << 1;
const ANNOT_NO_VIEW: i64 = 1 << 5;
const FF_READ_ONLY: i64 = 1;
const FF_REQUIRED: i64 = 1 << 1;
const FF_MULTILINE: i64 = 1 << 12;
const FF_PASSWORD: i64 = 1 << 13;
const FF_RADIO: i64 = 1 << 15;
const FF_PUSHBUTTON: i64 = 1 << 16;
const FF_COMBO: i64 = 1 << 17;
#[derive(Clone, Default)]
struct Inherited {
ft: Option<Vec<u8>>,
ff: i64,
v: Option<Object>,
dv: Option<Object>,
da: Option<Vec<u8>>,
q: i64,
opt: Option<Object>,
max_len: Option<i64>,
}
struct FoundField {
id: ObjectId,
name: String,
widget_ids: Vec<ObjectId>,
inherited: Inherited,
}
pub fn parse_form_fields(bytes: &[u8]) -> Result<Vec<FormField>, String> {
let doc = Document::load_mem(bytes).map_err(|e| format!("not a PDF: {e}"))?;
Ok(form_fields(&doc))
}
fn form_fields(doc: &Document) -> Vec<FormField> {
let pages = page_index_of(doc);
find_fields(doc)
.into_iter()
.filter_map(|found| field_of(doc, &found, &pages))
.collect()
}
fn page_index_of(doc: &Document) -> BTreeMap<ObjectId, usize> {
doc.get_pages()
.iter()
.map(|(num, id)| (*id, (*num as usize).saturating_sub(1)))
.collect()
}
fn dict_of<'a>(doc: &'a Document, object: &'a Object) -> Option<&'a Dictionary> {
doc.dereference(object).ok()?.1.as_dict().ok()
}
fn find_fields(doc: &Document) -> Vec<FoundField> {
let Some(fields) = doc
.catalog()
.ok()
.and_then(|catalog| catalog.get(b"AcroForm").ok())
.and_then(|form| dict_of(doc, form))
.and_then(|form| form.get(b"Fields").ok())
.and_then(|fields| doc.dereference(fields).ok())
.and_then(|(_, fields)| fields.as_array().ok())
else {
return Vec::new();
};
let da = doc
.catalog()
.ok()
.and_then(|catalog| catalog.get(b"AcroForm").ok())
.and_then(|form| dict_of(doc, form))
.and_then(|form| form.get(b"DA").ok())
.and_then(|da| da.as_str().ok())
.map(<[u8]>::to_vec);
let mut out = Vec::new();
let mut seen = BTreeSet::new();
for field in fields {
if let Ok(id) = field.as_reference() {
let inherited = Inherited {
da: da.clone(),
..Inherited::default()
};
walk_field(doc, id, "", &inherited, &mut out, &mut seen, 0);
}
}
out
}
fn walk_field(
doc: &Document,
id: ObjectId,
parent_name: &str,
parent: &Inherited,
out: &mut Vec<FoundField>,
seen: &mut BTreeSet<ObjectId>,
depth: usize,
) {
if depth > 32 || !seen.insert(id) {
return;
}
let Ok(dict) = doc.get_dictionary(id) else {
return;
};
let mut inherited = parent.clone();
if let Ok(ft) = dict.get(b"FT").and_then(Object::as_name) {
inherited.ft = Some(ft.to_vec());
}
if let Ok(ff) = dict.get(b"Ff").and_then(Object::as_i64) {
inherited.ff = ff;
}
if let Ok(v) = dict.get(b"V") {
inherited.v = Some(v.clone());
}
if let Ok(dv) = dict.get(b"DV") {
inherited.dv = Some(dv.clone());
}
if let Ok(da) = dict.get(b"DA").and_then(Object::as_str) {
inherited.da = Some(da.to_vec());
}
if let Ok(q) = dict.get(b"Q").and_then(Object::as_i64) {
inherited.q = q;
}
if let Ok(opt) = dict.get(b"Opt") {
inherited.opt = Some(opt.clone());
}
if let Ok(max_len) = dict.get(b"MaxLen").and_then(Object::as_i64) {
inherited.max_len = Some(max_len);
}
let partial = dict
.get(b"T")
.and_then(Object::as_str)
.map(decode_text_string)
.unwrap_or_default();
let name = match (parent_name.is_empty(), partial.is_empty()) {
(_, true) => parent_name.to_string(),
(true, false) => partial,
(false, false) => format!("{parent_name}.{partial}"),
};
let kids: Vec<ObjectId> = dict
.get(b"Kids")
.ok()
.and_then(|kids| doc.dereference(kids).ok())
.and_then(|(_, kids)| kids.as_array().ok())
.map(|kids| kids.iter().filter_map(|k| k.as_reference().ok()).collect())
.unwrap_or_default();
let (child_fields, widgets): (Vec<ObjectId>, Vec<ObjectId>) = kids.into_iter().partition(|kid| {
doc.get_dictionary(*kid)
.is_ok_and(|kid| kid.has(b"T"))
});
for child in child_fields {
walk_field(doc, child, &name, &inherited, out, seen, depth + 1);
}
let is_widget = dict
.get(b"Subtype")
.and_then(Object::as_name)
.is_ok_and(|s| s == b"Widget");
let widget_ids = if widgets.is_empty() && is_widget {
vec![id]
} else {
widgets
};
if !widget_ids.is_empty() || (inherited.ft.is_some() && dict.has(b"T")) {
out.push(FoundField {
id,
name,
widget_ids,
inherited,
});
}
}
fn field_of(
doc: &Document,
found: &FoundField,
pages: &BTreeMap<ObjectId, usize>,
) -> Option<FormField> {
let inherited = &found.inherited;
let ff = inherited.ff;
let kind = match inherited.ft.as_deref()? {
b"Tx" => FormFieldKind::Text,
b"Btn" if ff & FF_PUSHBUTTON != 0 => FormFieldKind::PushButton,
b"Btn" if ff & FF_RADIO != 0 => FormFieldKind::RadioButton,
b"Btn" => FormFieldKind::CheckBox,
b"Ch" if ff & FF_COMBO != 0 => FormFieldKind::ComboBox,
b"Ch" => FormFieldKind::ListBox,
b"Sig" => FormFieldKind::Signature,
_ => return None,
};
let widgets: Vec<FormWidget> = found
.widget_ids
.iter()
.filter_map(|id| widget_of(doc, *id, pages))
.collect();
let options = match kind {
FormFieldKind::RadioButton => widgets
.iter()
.map(|w| w.on_state.clone())
.filter(|s| !s.is_empty())
.collect(),
_ => inherited
.opt
.as_ref()
.and_then(|opt| doc.dereference(opt).ok())
.and_then(|(_, opt)| opt.as_array().ok())
.map(|opt| opt.iter().map(|o| option_text(doc, o)).collect())
.unwrap_or_default(),
};
Some(FormField {
name: found.name.clone(),
kind,
value: inherited.v.as_ref().map(|v| value_text(doc, v)).unwrap_or_default(),
default_value: inherited
.dv
.as_ref()
.map(|v| value_text(doc, v))
.unwrap_or_default(),
options,
widgets,
read_only: ff & FF_READ_ONLY != 0,
required: ff & FF_REQUIRED != 0,
multiline: kind == FormFieldKind::Text && ff & FF_MULTILINE != 0,
password: kind == FormFieldKind::Text && ff & FF_PASSWORD != 0,
max_len: inherited.max_len.and_then(|m| u32::try_from(m).ok()),
font_size: inherited
.da
.as_deref()
.and_then(da_font_size)
.unwrap_or(0.0),
alignment: u8::try_from(inherited.q.clamp(0, 2)).unwrap_or(0),
})
}
fn widget_of(doc: &Document, id: ObjectId, pages: &BTreeMap<ObjectId, usize>) -> Option<FormWidget> {
let dict = doc.get_dictionary(id).ok()?;
let rect = rect_of(doc, dict.get(b"Rect").ok()?)?;
let page = dict
.get(b"P")
.and_then(Object::as_reference)
.ok()
.and_then(|p| pages.get(&p).copied())
.or_else(|| page_holding(doc, id, pages))?;
let on_state = dict
.get(b"AP")
.ok()
.and_then(|ap| dict_of(doc, ap))
.and_then(|ap| ap.get(b"N").ok())
.and_then(|n| dict_of(doc, n))
.and_then(|states| {
states
.iter()
.map(|(k, _)| String::from_utf8_lossy(k).into_owned())
.find(|k| k != "Off")
})
.unwrap_or_default();
let flags = dict.get(b"F").and_then(Object::as_i64).unwrap_or(0);
Some(FormWidget {
page,
rect,
on_state,
hidden: flags & (ANNOT_HIDDEN | ANNOT_NO_VIEW) != 0,
})
}
fn page_holding(doc: &Document, widget: ObjectId, pages: &BTreeMap<ObjectId, usize>) -> Option<usize> {
pages.iter().find_map(|(page_id, index)| {
let annots = doc.get_dictionary(*page_id).ok()?.get(b"Annots").ok()?;
let annots = doc.dereference(annots).ok()?.1.as_array().ok()?;
annots
.iter()
.any(|a| a.as_reference().ok() == Some(widget))
.then_some(*index)
})
}
fn number(object: &Object) -> Option<f32> {
match object {
Object::Integer(i) => Some(*i as f32),
Object::Real(r) => Some(*r),
_ => None,
}
}
fn rect_of(doc: &Document, object: &Object) -> Option<[f32; 4]> {
let array = doc.dereference(object).ok()?.1.as_array().ok()?;
let v: Vec<f32> = array.iter().filter_map(number).collect();
if v.len() != 4 {
return None;
}
Some([v[0].min(v[2]), v[1].min(v[3]), v[0].max(v[2]), v[1].max(v[3])])
}
#[must_use]
pub fn decode_text_string(bytes: &[u8]) -> String {
if let Some(utf16) = bytes.strip_prefix(&[0xFE, 0xFF]) {
let units: Vec<u16> = utf16
.chunks_exact(2)
.map(|c| u16::from_be_bytes([c[0], c[1]]))
.collect();
return String::from_utf16_lossy(&units);
}
if let Some(utf8) = bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]) {
return String::from_utf8_lossy(utf8).into_owned();
}
bytes.iter().map(|b| char::from(*b)).collect()
}
fn encode_text_string(text: &str) -> Vec<u8> {
if text.is_ascii() {
return text.as_bytes().to_vec();
}
let mut out = vec![0xFE, 0xFF];
for unit in text.encode_utf16() {
out.extend_from_slice(&unit.to_be_bytes());
}
out
}
fn value_text(doc: &Document, value: &Object) -> String {
let Ok((_, value)) = doc.dereference(value) else {
return String::new();
};
match value {
Object::String(bytes, _) => decode_text_string(bytes),
Object::Name(name) => String::from_utf8_lossy(name).into_owned(),
Object::Array(items) => items
.iter()
.map(|item| value_text(doc, item))
.collect::<Vec<_>>()
.join(", "),
Object::Stream(stream) => decode_text_string(&stream.content),
_ => String::new(),
}
}
fn option_text(doc: &Document, option: &Object) -> String {
match doc.dereference(option).map(|(_, o)| o) {
Ok(Object::Array(pair)) => pair.get(1).or(pair.first()).map_or_else(String::new, |o| value_text(doc, o)),
Ok(other) => value_text(doc, other),
Err(_) => String::new(),
}
}
fn da_font_size(da: &[u8]) -> Option<f32> {
let da = String::from_utf8_lossy(da);
let tokens: Vec<&str> = da.split_whitespace().collect();
let tf = tokens.iter().position(|t| *t == "Tf")?;
tokens.get(tf.checked_sub(1)?)?.parse().ok()
}
fn da_colour(da: &[u8]) -> String {
let da = String::from_utf8_lossy(da);
let tokens: Vec<&str> = da.split_whitespace().collect();
for (i, t) in tokens.iter().enumerate() {
let n = match *t {
"g" => 1,
"rg" => 3,
"k" => 4,
_ => continue,
};
if i >= n {
return tokens[i - n..=i].join(" ");
}
}
"0 g".to_string()
}
const HELVETICA_WIDTHS: [u16; 95] = [
278, 278, 355, 556, 556, 889, 667, 191, 333, 333, 389, 584, 278, 333, 278, 278, 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 278, 278, 584, 584, 584, 556, 1015, 667, 667, 722, 722, 667, 611, 778, 722, 278, 500, 667, 556, 833, 722, 778, 667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 278, 278, 278, 469, 556, 333, 556, 556, 500, 556, 556, 278, 556, 556, 222, 222, 500, 222, 833, 556, 556, 556, 556, 333, 500, 278, 556, 500, 722, 500, 500, 500, 334, 260, 334, 584, ];
fn helvetica_width(text: &str, size: f32) -> f32 {
let units: u32 = text
.chars()
.map(|c| {
let code = c as u32;
if (0x20..=0x7E).contains(&code) {
u32::from(HELVETICA_WIDTHS[(code - 0x20) as usize])
} else {
556
}
})
.sum();
units as f32 * size / 1000.0
}
fn win_ansi(c: char) -> u8 {
match c as u32 {
0x20..=0x7E | 0xA0..=0xFF => c as u32 as u8,
0x20AC => 0x80,
0x201A => 0x82,
0x0192 => 0x83,
0x201E => 0x84,
0x2026 => 0x85,
0x2020 => 0x86,
0x2021 => 0x87,
0x02C6 => 0x88,
0x2030 => 0x89,
0x0160 => 0x8A,
0x2039 => 0x8B,
0x0152 => 0x8C,
0x017D => 0x8E,
0x2018 => 0x91,
0x2019 => 0x92,
0x201C => 0x93,
0x201D => 0x94,
0x2022 => 0x95,
0x2013 => 0x96,
0x2014 => 0x97,
0x02DC => 0x98,
0x2122 => 0x99,
0x0161 => 0x9A,
0x203A => 0x9B,
0x0153 => 0x9C,
0x017E => 0x9E,
0x0178 => 0x9F,
_ => b'?',
}
}
fn pdf_literal(text: &str) -> Vec<u8> {
let mut out = vec![b'('];
for c in text.chars() {
let b = win_ansi(c);
if matches!(b, b'(' | b')' | b'\\') {
out.push(b'\\');
}
out.push(b);
}
out.push(b')');
out
}
const APPEARANCE_FONT: &[u8] = b"AzHelv";
fn helvetica() -> Dictionary {
let mut font = Dictionary::new();
font.set("Type", Object::Name(b"Font".to_vec()));
font.set("Subtype", Object::Name(b"Type1".to_vec()));
font.set("BaseFont", Object::Name(b"Helvetica".to_vec()));
font.set("Encoding", Object::Name(b"WinAnsiEncoding".to_vec()));
font
}
fn appearance_content(field: &FormField, widget: &FormWidget, da: &[u8], w: f32, h: f32) -> Vec<u8> {
let mut out = Vec::new();
match field.kind {
FormFieldKind::CheckBox | FormFieldKind::RadioButton => {
if !field.is_on(widget) {
return out;
}
let s = w.min(h);
let (cx, cy) = (w / 2.0, h / 2.0);
if field.kind == FormFieldKind::RadioButton {
let r = s * 0.3;
let k = r * 0.552_284_8;
out.extend_from_slice(
format!(
"q {colour} {x0} {cy} m {x0} {a} {b} {y1} {cx} {y1} c {c} {y1} {x1} {a} {x1} {cy} c \
{x1} {d} {c} {y0} {cx} {y0} c {b} {y0} {x0} {d} {x0} {cy} c f Q\n",
colour = da_colour(da),
x0 = cx - r,
x1 = cx + r,
y0 = cy - r,
y1 = cy + r,
a = cy + k,
d = cy - k,
b = cx - k,
c = cx + k,
)
.as_bytes(),
);
} else {
let colour = da_colour(da).replace(" g", " G").replace(" rg", " RG").replace(" k", " K");
out.extend_from_slice(
format!(
"q {colour} {lw} w 1 J 1 j {x0} {y0} m {x1} {y1} l {x2} {y2} l S Q\n",
lw = s * 0.12,
x0 = cx - s * 0.3,
y0 = cy,
x1 = cx - s * 0.08,
y1 = cy - s * 0.25,
x2 = cx + s * 0.32,
y2 = cy + s * 0.28,
)
.as_bytes(),
);
}
}
FormFieldKind::Text | FormFieldKind::ComboBox | FormFieldKind::ListBox => {
let text = if field.password {
"*".repeat(field.value.chars().count())
} else {
field.value.clone()
};
if text.is_empty() {
return out;
}
let lines: Vec<&str> = if field.multiline {
text.split('\n').collect()
} else {
vec![text.lines().next().unwrap_or("")]
};
let size = if field.font_size > 0.0 {
field.font_size
} else if field.multiline {
12.0_f32.min((h - 4.0).max(4.0))
} else {
((h - 4.0) * 0.75).clamp(4.0, 12.0)
};
let line_height = size * 1.15;
out.extend_from_slice(b"/Tx BMC q 1 1 ");
out.extend_from_slice(format!("{} {} re W n BT ", (w - 2.0).max(0.0), (h - 2.0).max(0.0)).as_bytes());
out.push(b'/');
out.extend_from_slice(APPEARANCE_FONT);
out.extend_from_slice(format!(" {size} Tf {} ", da_colour(da)).as_bytes());
for (i, line) in lines.iter().enumerate() {
let width = helvetica_width(line, size);
let x = match field.alignment {
1 => (w - width) / 2.0,
2 => w - 2.0 - width,
_ => 2.0,
};
let y = if field.multiline {
h - 2.0 - size - i as f32 * line_height
} else {
(h - size * 0.72) / 2.0
};
out.extend_from_slice(format!("1 0 0 1 {x} {y} Tm ").as_bytes());
out.extend_from_slice(&pdf_literal(line));
out.extend_from_slice(b" Tj ");
}
out.extend_from_slice(b"ET Q EMC\n");
}
FormFieldKind::PushButton | FormFieldKind::Signature => {}
}
out
}
#[must_use]
pub fn fields_svg(fields: &[FormField], page: usize, page_height: f32) -> String {
let mut svg = String::new();
for field in fields {
for widget in field.widgets.iter().filter(|w| w.page == page && !w.hidden) {
let [llx, lly, urx, ury] = widget.rect;
let (w, h) = (urx - llx, ury - lly);
let top = page_height - ury;
match field.kind {
FormFieldKind::CheckBox | FormFieldKind::RadioButton if field.is_on(widget) => {
let s = w.min(h);
let (cx, cy) = (llx + w / 2.0, top + h / 2.0);
if field.kind == FormFieldKind::RadioButton {
svg.push_str(&format!(
r#"<circle cx="{cx}" cy="{cy}" r="{}" fill="rgb(0, 0, 0)" />"#,
s * 0.3
));
} else {
svg.push_str(&format!(
r#"<path d="M{},{} L{},{} L{},{}" fill="none" stroke="rgb(0, 0, 0)" stroke-width="{}" stroke-linecap="round" stroke-linejoin="round" />"#,
cx - s * 0.3,
cy,
cx - s * 0.08,
cy + s * 0.25,
cx + s * 0.32,
cy - s * 0.28,
s * 0.12
));
}
}
FormFieldKind::Text | FormFieldKind::ComboBox | FormFieldKind::ListBox
if !field.value.is_empty() =>
{
let text = if field.password {
"*".repeat(field.value.chars().count())
} else {
field.value.clone()
};
let size = if field.font_size > 0.0 {
field.font_size
} else {
((h - 4.0) * 0.75).clamp(4.0, 12.0)
};
let lines: Vec<&str> = if field.multiline {
text.split('\n').collect()
} else {
vec![text.lines().next().unwrap_or("")]
};
for (i, line) in lines.iter().enumerate() {
let width = helvetica_width(line, size);
let x = llx
+ match field.alignment {
1 => (w - width) / 2.0,
2 => w - 2.0 - width,
_ => 2.0,
};
let baseline = if field.multiline {
top + 2.0 + size + i as f32 * size * 1.15
} else {
top + h - (h - size * 0.72) / 2.0
};
svg.push_str(&format!(
r#"<text x="{x}" y="{baseline}" font-family="Helvetica, Arial, sans-serif" font-size="{size}" fill="rgb(0, 0, 0)">{}</text>"#,
xml_escape(line)
));
}
}
_ => {}
}
}
}
svg
}
fn xml_escape(text: &str) -> String {
let mut out = String::with_capacity(text.len());
for c in text.chars() {
match c {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' => out.push_str(">"),
'"' => out.push_str("""),
c => out.push(c),
}
}
out
}
pub fn fill_form(
bytes: &[u8],
values: &[FieldValue],
stamps: &[FormStamp],
flatten: bool,
) -> Result<Vec<u8>, String> {
let mut doc = Document::load_mem(bytes).map_err(|e| format!("not a PDF: {e}"))?;
let pages = page_index_of(&doc);
let page_ids: BTreeMap<usize, ObjectId> = pages.iter().map(|(id, i)| (*i, *id)).collect();
let found = find_fields(&doc);
let mut fields: Vec<(FoundField, FormField)> = found
.into_iter()
.filter_map(|f| {
let field = field_of(&doc, &f, &pages)?;
Some((f, field))
})
.collect();
let wanted: BTreeMap<&str, &str> = values
.iter()
.map(|v| (v.name.as_str(), v.value.as_str()))
.collect();
for (found, field) in &mut fields {
let Some(value) = wanted.get(field.name.as_str()) else {
continue;
};
set_value(&mut doc, found, field, value);
}
let mut page_content: BTreeMap<usize, Vec<u8>> = BTreeMap::new();
let mut page_xobjects: BTreeMap<usize, Vec<(Vec<u8>, ObjectId)>> = BTreeMap::new();
if flatten {
let mut n = 0usize;
for (found, field) in &fields {
for (widget_id, widget) in found.widget_ids.iter().zip(&field.widgets) {
if widget.hidden {
continue;
}
let ap = match normal_appearance(&doc, *widget_id) {
Some(ap) => ap,
None => {
let da = found
.inherited
.da
.clone()
.unwrap_or_else(|| b"/Helv 0 Tf 0 g".to_vec());
add_appearance(&mut doc, field, widget, &da)
}
};
let Some(placement) = appearance_placement(&doc, ap, widget.rect) else {
continue;
};
n += 1;
let name = format!("AzFlat{n}").into_bytes();
let content = page_content.entry(widget.page).or_default();
content.extend_from_slice(format!("q {placement} cm /").as_bytes());
content.extend_from_slice(&name);
content.extend_from_slice(b" Do Q\n");
page_xobjects.entry(widget.page).or_default().push((name, ap));
}
}
}
for stamp in stamps {
page_content
.entry(stamp.page)
.or_default()
.extend_from_slice(&stamp_content(stamp));
}
for (page, content) in &page_content {
let Some(page_id) = page_ids.get(page).copied() else {
continue;
};
let xobjects = page_xobjects.remove(page).unwrap_or_default();
append_page_content(&mut doc, page_id, content.clone(), &xobjects);
}
if flatten {
let widget_ids: BTreeSet<ObjectId> = fields
.iter()
.flat_map(|(found, _)| found.widget_ids.iter().copied())
.collect();
for page_id in page_ids.values() {
remove_widgets(&mut doc, *page_id, &widget_ids);
}
if let Ok(catalog) = doc.catalog_mut() {
catalog.remove(b"AcroForm");
}
} else if !values.is_empty() {
set_need_appearances(&mut doc);
}
let mut out = Vec::new();
doc.save_to(&mut out).map_err(|e| format!("the PDF could not be written: {e}"))?;
Ok(out)
}
fn set_value(doc: &mut Document, found: &FoundField, field: &mut FormField, value: &str) {
field.value = value.to_string();
let v = match field.kind {
FormFieldKind::CheckBox | FormFieldKind::RadioButton => {
let on = field
.widgets
.iter()
.find(|w| is_on_value(value, w))
.map(|w| if w.on_state.is_empty() { "Yes".to_string() } else { w.on_state.clone() });
field.value = on.clone().unwrap_or_else(|| "Off".to_string());
Object::Name(field.value.clone().into_bytes())
}
FormFieldKind::PushButton | FormFieldKind::Signature => return,
_ => Object::String(encode_text_string(value), StringFormat::Literal),
};
if let Ok(dict) = doc.get_object_mut(found.id).and_then(Object::as_dict_mut) {
dict.set("V", v);
}
let da = found.inherited.da.clone().unwrap_or_else(|| b"/Helv 0 Tf 0 g".to_vec());
for (widget_id, widget) in found.widget_ids.iter().zip(field.widgets.clone()) {
match field.kind {
FormFieldKind::CheckBox | FormFieldKind::RadioButton => {
let on = field.is_on(&widget);
let state = if on {
if widget.on_state.is_empty() {
"Yes".to_string()
} else {
widget.on_state.clone()
}
} else {
"Off".to_string()
};
let has_own_appearance = !widget.on_state.is_empty();
if let Ok(dict) = doc.get_object_mut(*widget_id).and_then(Object::as_dict_mut) {
dict.set("AS", Object::Name(state.into_bytes()));
}
if !has_own_appearance {
let ap = add_appearance(doc, field, &widget, &da);
set_normal_appearance(doc, *widget_id, ap, on);
}
}
_ => {
let ap = add_appearance(doc, field, &widget, &da);
set_normal_appearance(doc, *widget_id, ap, false);
}
}
}
}
fn add_appearance(doc: &mut Document, field: &FormField, widget: &FormWidget, da: &[u8]) -> ObjectId {
let [llx, lly, urx, ury] = widget.rect;
let (w, h) = ((urx - llx).max(0.0), (ury - lly).max(0.0));
let mut fonts = Dictionary::new();
fonts.set(APPEARANCE_FONT.to_vec(), Object::Dictionary(helvetica()));
let mut resources = Dictionary::new();
resources.set("Font", Object::Dictionary(fonts));
let mut dict = Dictionary::new();
dict.set("Type", Object::Name(b"XObject".to_vec()));
dict.set("Subtype", Object::Name(b"Form".to_vec()));
dict.set(
"BBox",
Object::Array(vec![0.into(), 0.into(), Object::Real(w), Object::Real(h)]),
);
dict.set("Resources", Object::Dictionary(resources));
doc.add_object(Stream::new(dict, appearance_content(field, widget, da, w, h)))
}
fn set_normal_appearance(doc: &mut Document, widget: ObjectId, ap: ObjectId, on_state: bool) {
let Ok(dict) = doc.get_object_mut(widget).and_then(Object::as_dict_mut) else {
return;
};
let mut appearances = Dictionary::new();
let field_is_button = dict.has(b"AS");
if field_is_button {
let mut states = Dictionary::new();
states.set(if on_state { "Yes" } else { "Off" }, Object::Reference(ap));
appearances.set("N", Object::Dictionary(states));
} else {
appearances.set("N", Object::Reference(ap));
}
dict.set("AP", Object::Dictionary(appearances));
}
fn normal_appearance(doc: &Document, widget: ObjectId) -> Option<ObjectId> {
let dict = doc.get_dictionary(widget).ok()?;
let n = dict_of(doc, dict.get(b"AP").ok()?)?.get(b"N").ok()?;
if let Ok(id) = n.as_reference() {
if doc.get_object(id).ok()?.as_stream().is_ok() {
return Some(id);
}
}
let state = dict.get(b"AS").and_then(Object::as_name).ok()?;
dict_of(doc, n)?.get(state).ok()?.as_reference().ok()
}
fn appearance_placement(doc: &Document, ap: ObjectId, rect: [f32; 4]) -> Option<String> {
let stream = doc.get_object(ap).ok()?.as_stream().ok()?;
let bbox = rect_of(doc, stream.dict.get(b"BBox").ok()?)?;
let m: [f32; 6] = stream
.dict
.get(b"Matrix")
.ok()
.and_then(|m| m.as_array().ok())
.map(|m| m.iter().filter_map(number).collect::<Vec<f32>>())
.and_then(|m| m.try_into().ok())
.unwrap_or([1.0, 0.0, 0.0, 1.0, 0.0, 0.0]);
let corners = [
(bbox[0], bbox[1]),
(bbox[2], bbox[1]),
(bbox[0], bbox[3]),
(bbox[2], bbox[3]),
]
.map(|(x, y)| (m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]));
let (mut x0, mut y0, mut x1, mut y1) = (f32::MAX, f32::MAX, f32::MIN, f32::MIN);
for (x, y) in corners {
x0 = x0.min(x);
y0 = y0.min(y);
x1 = x1.max(x);
y1 = y1.max(y);
}
let (bw, bh) = (x1 - x0, y1 - y0);
if bw <= 0.0 || bh <= 0.0 {
return None;
}
let sx = (rect[2] - rect[0]) / bw;
let sy = (rect[3] - rect[1]) / bh;
Some(format!(
"{sx} 0 0 {sy} {} {}",
rect[0] - x0 * sx,
rect[1] - y0 * sy
))
}
fn stamp_content(stamp: &FormStamp) -> Vec<u8> {
let [vx, vy, vw, vh] = stamp.view_box;
let [llx, lly, urx, ury] = stamp.rect;
if vw <= 0.0 || vh <= 0.0 {
return Vec::new();
}
let sx = (urx - llx) / vw;
let sy = (ury - lly) / vh;
let mut out = format!(
"q {sx} 0 0 {} {} {} cm 1 J 1 j\n",
-sy,
llx - vx * sx,
ury + vy * sy
)
.into_bytes();
for path in &stamp.paths {
if path.segments.is_empty() || (path.stroke.is_none() && path.fill.is_none()) {
continue;
}
if let Some((width, [r, g, b])) = path.stroke {
out.extend_from_slice(format!("{width} w {r} {g} {b} RG ").as_bytes());
}
if let Some([r, g, b]) = path.fill {
out.extend_from_slice(format!("{r} {g} {b} rg ").as_bytes());
}
for segment in &path.segments {
let op = match *segment {
StampSegment::MoveTo(x, y) => format!("{x} {y} m "),
StampSegment::LineTo(x, y) => format!("{x} {y} l "),
StampSegment::CubicTo(x1, y1, x2, y2, x, y) => {
format!("{x1} {y1} {x2} {y2} {x} {y} c ")
}
StampSegment::Close => "h ".to_string(),
};
out.extend_from_slice(op.as_bytes());
}
out.extend_from_slice(match (path.fill.is_some(), path.stroke.is_some()) {
(true, true) => b"B\n",
(true, false) => b"f\n",
_ => b"S\n",
});
}
out.extend_from_slice(b"Q\n");
out
}
fn own_resources(doc: &Document, page: ObjectId) -> Dictionary {
let mut node = Some(page);
let mut depth = 0;
while let Some(id) = node {
let Ok(dict) = doc.get_dictionary(id) else {
break;
};
if let Ok(resources) = dict.get(b"Resources") {
if let Some(resources) = dict_of(doc, resources) {
return resources.clone();
}
}
node = dict.get(b"Parent").and_then(Object::as_reference).ok();
depth += 1;
if depth > 64 {
break;
}
}
Dictionary::new()
}
fn append_page_content(
doc: &mut Document,
page: ObjectId,
content: Vec<u8>,
xobjects: &[(Vec<u8>, ObjectId)],
) {
let mut resources = own_resources(doc, page);
if !xobjects.is_empty() {
let mut existing = resources
.get(b"XObject")
.ok()
.and_then(|x| dict_of(doc, x))
.cloned()
.unwrap_or_default();
for (name, id) in xobjects {
existing.set(name.clone(), Object::Reference(*id));
}
resources.set("XObject", Object::Dictionary(existing));
}
let old_contents: Vec<Object> = doc
.get_dictionary(page)
.ok()
.and_then(|d| d.get(b"Contents").ok())
.map(|c| match c {
Object::Array(items) => items.clone(),
other => vec![other.clone()],
})
.unwrap_or_default();
let open = doc.add_object(Stream::new(Dictionary::new(), b"q\n".to_vec()));
let mut close = b"\nQ\n".to_vec();
close.extend_from_slice(&content);
let close = doc.add_object(Stream::new(Dictionary::new(), close));
let mut contents = vec![Object::Reference(open)];
contents.extend(old_contents);
contents.push(Object::Reference(close));
if let Ok(dict) = doc.get_object_mut(page).and_then(Object::as_dict_mut) {
dict.set("Contents", Object::Array(contents));
dict.set("Resources", Object::Dictionary(resources));
}
}
fn remove_widgets(doc: &mut Document, page: ObjectId, widgets: &BTreeSet<ObjectId>) {
let annots = doc
.get_dictionary(page)
.ok()
.and_then(|d| d.get(b"Annots").ok())
.and_then(|a| doc.dereference(a).ok())
.and_then(|(_, a)| a.as_array().ok())
.cloned();
let Some(annots) = annots else {
return;
};
let kept: Vec<Object> = annots
.into_iter()
.filter(|a| a.as_reference().map_or(true, |id| !widgets.contains(&id)))
.collect();
if let Ok(dict) = doc.get_object_mut(page).and_then(Object::as_dict_mut) {
if kept.is_empty() {
dict.remove(b"Annots");
} else {
dict.set("Annots", Object::Array(kept));
}
}
}
fn set_need_appearances(doc: &mut Document) {
let form = doc
.catalog()
.ok()
.and_then(|c| c.get(b"AcroForm").ok())
.cloned();
match form {
Some(Object::Reference(id)) => {
if let Ok(dict) = doc.get_object_mut(id).and_then(Object::as_dict_mut) {
dict.set("NeedAppearances", Object::Boolean(true));
}
}
Some(Object::Dictionary(_)) => {
if let Ok(catalog) = doc.catalog_mut() {
if let Ok(form) = catalog.get_mut(b"AcroForm").and_then(Object::as_dict_mut) {
form.set("NeedAppearances", Object::Boolean(true));
}
}
}
_ => {}
}
}
#[cfg(test)]
mod tests {
use lopdf::{dictionary, Document, Object, Stream, StringFormat};
use super::*;
fn form_pdf() -> Vec<u8> {
let mut doc = Document::with_version("1.7");
let pages_id = doc.new_object_id();
let page_id = doc.new_object_id();
let on = doc.add_object(Stream::new(dictionary! {"BBox" => vec![0.into(), 0.into(), 12.into(), 12.into()]}, b"0 g 2 2 8 8 re f".to_vec()));
let off = doc.add_object(Stream::new(dictionary! {"BBox" => vec![0.into(), 0.into(), 12.into(), 12.into()]}, Vec::new()));
let person = doc.new_object_id();
let name = doc.add_object(dictionary! {
"Type" => "Annot", "Subtype" => "Widget", "P" => page_id, "Parent" => person,
"T" => Object::String(b"name".to_vec(), StringFormat::Literal),
"FT" => "Tx", "DA" => Object::String(b"/Helv 10 Tf 0 g".to_vec(), StringFormat::Literal),
"V" => Object::String(b"Grace".to_vec(), StringFormat::Literal),
"Rect" => vec![72.into(), 700.into(), 272.into(), 720.into()],
});
doc.objects.insert(person, Object::Dictionary(dictionary! {
"T" => Object::String(b"person".to_vec(), StringFormat::Literal),
"Kids" => vec![name.into()],
}));
let agree = doc.add_object(dictionary! {
"Type" => "Annot", "Subtype" => "Widget", "P" => page_id,
"T" => Object::String(b"agree".to_vec(), StringFormat::Literal),
"FT" => "Btn", "V" => "Off", "AS" => "Off",
"Rect" => vec![72.into(), 650.into(), 84.into(), 662.into()],
"AP" => dictionary! {"N" => dictionary! {"Yes" => on, "Off" => off}},
});
let colour = doc.add_object(dictionary! {
"Type" => "Annot", "Subtype" => "Widget", "P" => page_id,
"T" => Object::String(b"colour".to_vec(), StringFormat::Literal),
"FT" => "Ch", "Ff" => 1 << 17,
"Opt" => vec![Object::String(b"red".to_vec(), StringFormat::Literal), Object::String(b"green".to_vec(), StringFormat::Literal)],
"V" => Object::String(b"red".to_vec(), StringFormat::Literal),
"Rect" => vec![72.into(), 600.into(), 172.into(), 620.into()],
});
let content = doc.add_object(Stream::new(dictionary! {}, b"0 0 1 rg 10 10 50 50 re f".to_vec()));
doc.objects.insert(page_id, Object::Dictionary(dictionary! {
"Type" => "Page", "Parent" => pages_id, "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
"Contents" => content, "Annots" => vec![name.into(), agree.into(), colour.into()],
}));
doc.objects.insert(pages_id, Object::Dictionary(dictionary! {
"Type" => "Pages", "Kids" => vec![page_id.into()], "Count" => 1,
}));
let form = doc.add_object(dictionary! {
"Fields" => vec![person.into(), agree.into(), colour.into()],
"DA" => Object::String(b"/Helv 0 Tf 0 g".to_vec(), StringFormat::Literal),
});
let catalog = doc.add_object(dictionary! {"Type" => "Catalog", "Pages" => pages_id, "AcroForm" => form});
doc.trailer.set("Root", catalog);
let mut out = Vec::new();
doc.save_to(&mut out).unwrap();
out
}
fn field<'a>(fields: &'a [FormField], name: &str) -> &'a FormField {
fields.iter().find(|f| f.name == name).unwrap_or_else(|| panic!("no field {name}: {fields:?}"))
}
#[test]
fn the_fields_of_a_form_are_read_with_their_names_values_and_places() {
let fields = parse_form_fields(&form_pdf()).expect("a PDF");
assert_eq!(fields.len(), 3, "{fields:?}");
let name = field(&fields, "person.name");
assert_eq!(name.kind, FormFieldKind::Text);
assert_eq!(name.value, "Grace");
assert_eq!(name.font_size, 10.0);
assert_eq!(name.widgets.len(), 1);
assert_eq!(name.widgets[0].page, 0);
assert_eq!(name.widgets[0].rect, [72.0, 700.0, 272.0, 720.0]);
let agree = field(&fields, "agree");
assert_eq!(agree.kind, FormFieldKind::CheckBox);
assert_eq!(agree.value, "Off");
assert_eq!(agree.widgets[0].on_state, "Yes");
assert!(!agree.is_on(&agree.widgets[0]));
let colour = field(&fields, "colour");
assert_eq!(colour.kind, FormFieldKind::ComboBox);
assert_eq!(colour.options, ["red", "green"]);
assert_eq!(colour.value, "red");
assert!(parse_form_fields(b"not a pdf").is_err());
}
#[test]
fn filled_values_are_in_the_fields_of_the_saved_pdf() {
let filled = fill_form(
&form_pdf(),
&[
FieldValue { name: "person.name".into(), value: "Ada Lovelace \u{e9}".into() },
FieldValue { name: "agree".into(), value: "true".into() },
FieldValue { name: "colour".into(), value: "green".into() },
FieldValue { name: "nobody".into(), value: "x".into() },
],
&[],
false,
)
.expect("filled");
let fields = parse_form_fields(&filled).expect("a PDF");
assert_eq!(field(&fields, "person.name").value, "Ada Lovelace \u{e9}");
assert_eq!(field(&fields, "agree").value, "Yes");
assert_eq!(field(&fields, "colour").value, "green");
let doc = Document::load_mem(&filled).unwrap();
let shown = doc.objects.values().filter_map(|o| o.as_stream().ok()).any(|s| {
s.content.windows(15).any(|w| w == b"(Ada Lovelace \xE9")
});
assert!(shown, "an appearance stream draws the new text");
}
#[test]
fn a_flattened_form_is_drawn_into_its_page_and_has_no_fields_left() {
let filled = fill_form(
&form_pdf(),
&[FieldValue { name: "agree".into(), value: "Yes".into() }],
&[],
true,
)
.expect("flattened");
assert!(parse_form_fields(&filled).expect("a PDF").is_empty(), "no form left");
let doc = Document::load_mem(&filled).unwrap();
let page = *doc.get_pages().values().next().unwrap();
let page = doc.get_dictionary(page).unwrap();
assert!(!page.has(b"Annots"), "the widgets are gone");
let contents = page.get(b"Contents").unwrap().as_array().unwrap();
assert_eq!(contents.len(), 3, "q, the page's own content, Q + the fields");
let last = doc.get_object(contents[2].as_reference().unwrap()).unwrap().as_stream().unwrap();
let text = String::from_utf8_lossy(&last.content).into_owned();
assert_eq!(text.matches(" Do Q").count(), 3, "{text}");
assert!(text.contains("1 0 0 1 72 650 cm /AzFlat"), "the check box's own /AP on its rect: {text}");
let xobjects = page.get(b"Resources").unwrap().as_dict().unwrap().get(b"XObject").unwrap().as_dict().unwrap();
assert_eq!(xobjects.len(), 3);
}
#[test]
fn a_stamp_is_drawn_onto_its_rect_from_its_view_box() {
let stamp = FormStamp {
page: 0,
rect: [100.0, 100.0, 300.0, 150.0],
view_box: [0.0, 0.0, 400.0, 100.0],
paths: vec![StampPath {
segments: vec![StampSegment::MoveTo(0.0, 50.0), StampSegment::LineTo(400.0, 50.0)],
stroke: Some((4.0, [0.0, 0.0, 0.5])),
fill: None,
}],
};
let filled = fill_form(&form_pdf(), &[], &[stamp], false).expect("stamped");
let doc = Document::load_mem(&filled).unwrap();
let page = *doc.get_pages().values().next().unwrap();
let contents = doc.get_dictionary(page).unwrap().get(b"Contents").unwrap().as_array().unwrap().clone();
let last = doc.get_object(contents.last().unwrap().as_reference().unwrap()).unwrap().as_stream().unwrap();
let text = String::from_utf8_lossy(&last.content).into_owned();
assert!(text.contains("q 0.5 0 0 -0.5 100 150 cm"), "{text}");
assert!(text.contains("4 w 0 0 0.5 RG 0 50 m 400 50 l S"), "{text}");
assert_eq!(parse_form_fields(&filled).unwrap().len(), 3);
}
#[test]
fn the_values_draw_as_svg_on_their_page() {
let mut fields = parse_form_fields(&form_pdf()).unwrap();
fields.iter_mut().find(|f| f.name == "agree").unwrap().value = "Yes".into();
let svg = fields_svg(&fields, 0, 792.0);
assert!(svg.contains(">Grace</text>"), "{svg}");
assert!(svg.contains(">red</text>"), "{svg}");
assert!(svg.contains("<path"), "the check mark: {svg}");
assert!(fields_svg(&fields, 1, 792.0).is_empty(), "nothing on another page");
}
}