|
| 1 | +extern crate pdf; |
| 2 | + |
| 3 | + |
| 4 | +use std::env::args; |
| 5 | +use std::ops::Deref; |
| 6 | + |
| 7 | +use pdf::content::{FormXObject, Op, serialize_ops}; |
| 8 | +use pdf::error::PdfError; |
| 9 | +use pdf::file::{FileOptions, Log}; |
| 10 | +use pdf::font::{Font, FontData, TFont}; |
| 11 | +use pdf::object::*; |
| 12 | +use pdf::primitive::{Dictionary, Name, PdfString, Primitive}; |
| 13 | + |
| 14 | +fn run() -> Result<(), PdfError> { |
| 15 | + let path = args().nth(1).expect("no file given"); |
| 16 | + println!("read: {}", path); |
| 17 | + |
| 18 | + let mut old_file = FileOptions::cached().open(&path)?; |
| 19 | + let mut old_page: PageRc = old_file.get_page(0).unwrap(); |
| 20 | + |
| 21 | + let old_annots = old_page.annotations.load(&old_file.resolver()).expect("can't load annotations"); |
| 22 | + let mut annots: Vec<_> = (*old_annots).clone(); |
| 23 | + // let mut new_annots = annots.deref().clone(); |
| 24 | + // for annot in &new_annots { |
| 25 | + // dbg!(&annot.subtype); |
| 26 | + // dbg!(&annot.rect); |
| 27 | + // dbg!(&annot.color); |
| 28 | + // dbg!(&annot.transparency); |
| 29 | + // dbg!(&annot.ink_list); |
| 30 | + // dbg!(&annot.line); |
| 31 | + // dbg!(&annot.creation_date); |
| 32 | + // dbg!(&annot.uuid); |
| 33 | + // dbg!(&annot.border_style); |
| 34 | + // dbg!(&annot.popup); |
| 35 | + // dbg!(&annot.other); |
| 36 | + // } |
| 37 | + |
| 38 | + let mut bs = Dictionary::new(); |
| 39 | + bs.insert(Name::from("S"), PdfString::from("/S")); |
| 40 | + bs.insert(Name::from("W"), PdfString::from("3")); |
| 41 | + let new_annot = Annot { |
| 42 | + subtype: Name::from("Line"), |
| 43 | + // rect: Some(Rectangle { |
| 44 | + // left: 89.774, |
| 45 | + // bottom: 726.55, |
| 46 | + // right: 300.961, |
| 47 | + // top: 742.55, |
| 48 | + // }), |
| 49 | + rect: Some(Rectangle { left: 10., bottom: 10., right: 200., top: 200. }), |
| 50 | + contents: None, |
| 51 | + page: Some(old_page.clone()), |
| 52 | + border: None, |
| 53 | + annotation_name: None, |
| 54 | + date: None, |
| 55 | + annot_flags: 4, |
| 56 | + appearance_streams: None, |
| 57 | + appearance_state: None, |
| 58 | + color: Some(Primitive::Array( |
| 59 | + vec![Primitive::Integer(1), Primitive::Integer(0), Primitive::Integer(0)] |
| 60 | + )), |
| 61 | + ink_list: None, |
| 62 | + line: Some(vec![10., 100., 20., 200.]), |
| 63 | + // creation_date: None, |
| 64 | + // uuid: None, |
| 65 | + // border_style: Some(bs), |
| 66 | + // border_style: None, |
| 67 | + // popup: None, |
| 68 | + other: Dictionary::new(), |
| 69 | + // transparency: Some(1.0), |
| 70 | + // transparency: None, |
| 71 | + }; |
| 72 | + |
| 73 | + let annot_ref = old_file.create(new_annot)?; |
| 74 | + annots.push(MaybeRef::Indirect(annot_ref)); |
| 75 | + |
| 76 | + |
| 77 | + match old_annots { |
| 78 | + MaybeRef::Direct(_) => { |
| 79 | + // need to update the whole page |
| 80 | + let mut new_page: Page = (*old_page).clone(); |
| 81 | + |
| 82 | + let lazy_annots = Lazy::from(old_file.create(annots).unwrap()); |
| 83 | + new_page.annotations = lazy_annots; |
| 84 | + PageRc::update(new_page, &old_page, &mut old_file).unwrap(); |
| 85 | + } |
| 86 | + MaybeRef::Indirect(r) => { |
| 87 | + // can just update the annot reference |
| 88 | + old_file.update_ref(&r, annots).unwrap(); |
| 89 | + } |
| 90 | + } |
| 91 | + old_file.save_to("out.pdf")?; |
| 92 | + |
| 93 | + Ok(()) |
| 94 | +} |
| 95 | + |
| 96 | +fn main() { |
| 97 | + if let Err(e) = run() { |
| 98 | + println!("{e}"); |
| 99 | + } |
| 100 | +} |
0 commit comments