diff --git a/examples/src/bin/add_annot.rs b/examples/src/bin/add_annot.rs index 4206d87..a5acf94 100644 --- a/examples/src/bin/add_annot.rs +++ b/examples/src/bin/add_annot.rs @@ -18,7 +18,8 @@ fn run() -> Result<(), PdfError> { let mut old_file = FileOptions::cached().open(&path)?; let mut old_page: PageRc = old_file.get_page(0).unwrap(); - let mut annots = old_page.annotations.load(&old_file.resolver()).expect("can't load annotations"); + let old_annots = old_page.annotations.load(&old_file.resolver()).expect("can't load annotations"); + let mut annots: Vec<_> = (*old_annots).clone(); // let mut new_annots = annots.deref().clone(); // for annot in &new_annots { // dbg!(&annot.subtype); @@ -79,23 +80,24 @@ fn run() -> Result<(), PdfError> { let annot_ref = old_file.create(new_annot)?; annots.push(MaybeRef::Indirect(annot_ref)); - // let lazy_annots = Lazy::from_primitive( - // annots.to_primitive(&mut FileOptions::cached().storage()).unwrap(), - // &file.resolver() - // ); - // old_page.update_annots(annots, &old_file.resolver(), &mut FileOptions::cached().storage()); - // let old_annots = old_page.annotations.to_primitive(&mut old_file).unwrap(); + match old_annots { + MaybeRef::Direct(_) => { + // need to update the whole page + let mut new_page: Page = (*old_page).clone(); - - // let layz_annots = Lazy::from(annots); - // match annots { - // MaybeRef::Indirect(annot) => { - // old_page.annotations = Lazy::from(annot); - // } - // } - - old_file.update(old_page.get_plain_ref(), old_page); + let lazy_annots = Lazy::safe( + MaybeRef::Indirect(old_file.create(annots).unwrap()), + &mut old_file + ).unwrap(); + new_page.annotations = lazy_annots; + PageRc::update(new_page, &old_page, &mut old_file).unwrap(); + } + MaybeRef::Indirect(r) => { + // can just update the annot reference + old_file.update_ref(&r, annots).unwrap(); + } + } old_file.save_to("/Users/apple/Downloads/test_pdf/out.pdf")?; Ok(()) @@ -105,4 +107,4 @@ fn main() { if let Err(e) = run() { println!("{e}"); } -} \ No newline at end of file +} diff --git a/pdf/src/object/mod.rs b/pdf/src/object/mod.rs index f0c29e3..01798f9 100644 --- a/pdf/src/object/mod.rs +++ b/pdf/src/object/mod.rs @@ -108,6 +108,9 @@ pub trait DeepClone: Sized + Sync + Send + 'static { pub trait Updater { fn create(&mut self, obj: T) -> Result>; fn update(&mut self, old: PlainRef, obj: T) -> Result>; + fn update_ref(&mut self, old: &RcRef, obj: T) -> Result> { + self.update(old.get_ref().inner, obj) + } fn promise(&mut self) -> PromisedRef; fn fulfill(&mut self, promise: PromisedRef, obj: T) -> Result>; } @@ -439,6 +442,12 @@ impl Lazy { pub fn load(&self, resolve: &impl Resolve) -> Result { T::from_primitive(self.primitive.clone(), resolve) } + pub fn safe(value: T, update: &mut impl Updater) -> Result + where T: ObjectWrite + { + let primitive = value.to_primitive(update)?; + Ok(Lazy { primitive, _marker: PhantomData }) + } } impl Object for Lazy { fn from_primitive(p: Primitive, _: &impl Resolve) -> Result { @@ -462,7 +471,6 @@ impl From> for Lazy { } } - ////////////////////////////////////// // Object for Primitives & other types ////////////////////////////////////// diff --git a/pdf/src/object/types.rs b/pdf/src/object/types.rs index 1e11c63..36191c7 100644 --- a/pdf/src/object/types.rs +++ b/pdf/src/object/types.rs @@ -68,6 +68,9 @@ impl PageRc { pub fn create(page: Page, update: &mut impl Updater) -> Result { Ok(PageRc(update.create(PagesNode::Leaf(page))?)) } + pub fn update(page: Page, old_page: &PageRc, update: &mut impl Updater) -> Result { + update.update(old_page.get_plain_ref(), PagesNode::Leaf(page)).map(PageRc) + } pub fn get_ref(&self) -> Ref { self.0.get_ref() }