From d206580edba34efdd05cfd11d1aa830122479f93 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 2 May 2021 19:27:54 +0200 Subject: Proof of concept to replace PDF form fields' font PDF form fields are `/DA` fields (PDF Reference page 534, https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/pdf_reference_archives/PDFReference.pdf). Get all `/DA` objects. Their value is a string that specifies the field's appearance properties. One part of the appearance string is the font name (its Postscript name). By replacing the font name with a different one, we can change the field's font. --- src/main.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..91e1a0e --- /dev/null +++ b/src/main.rs @@ -0,0 +1,36 @@ +use lopdf::{Document, Object}; + + +fn main() { + let mut doc = Document::load("./f1040.pdf").unwrap(); + + for (_, mut obj) in &mut doc.objects { + // println!("{:?}", obj); + + match &mut obj { + Object::Dictionary(ref mut d) => { + for (k, v) in d.iter_mut() { + let key = std::str::from_utf8(k).unwrap(); + + if key == "DA" { + // println!("{:?}", d); + + let properties = v.as_str_mut().unwrap(); + + let new_properties = std::str::from_utf8(properties) + .unwrap() + .replace("HelveticaLTStd-Bold", "CourierNewPSMT"); + + *properties = new_properties.into_bytes(); + + dbg!(std::str::from_utf8(properties).unwrap()); + // dbg!(properties); + } + } + }, + _ => (), + } + } + + doc.save("./new.pdf").unwrap(); +} -- cgit v1.2.3