diff options
author | Teddy Wing | 2021-05-02 19:27:54 +0200 |
---|---|---|
committer | Teddy Wing | 2021-05-02 19:27:54 +0200 |
commit | d206580edba34efdd05cfd11d1aa830122479f93 (patch) | |
tree | 66365d0d1946c875214de364c56f2eb21ced9dbf /src | |
download | pdf-form-replace-font-d206580edba34efdd05cfd11d1aa830122479f93.tar.bz2 |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 36 |
1 files changed, 36 insertions, 0 deletions
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(); +} |