@@ -18,7 +18,8 @@ fn run() -> Result<(), PdfError> {
1818 let mut old_file = FileOptions :: cached ( ) . open ( & path) ?;
1919 let mut old_page: PageRc = old_file. get_page ( 0 ) . unwrap ( ) ;
2020
21- let mut annots = old_page. annotations . load ( & old_file. resolver ( ) ) . expect ( "can't load annotations" ) ;
21+ let old_annots = old_page. annotations . load ( & old_file. resolver ( ) ) . expect ( "can't load annotations" ) ;
22+ let mut annots: Vec < _ > = ( * old_annots) . clone ( ) ;
2223 // let mut new_annots = annots.deref().clone();
2324 // for annot in &new_annots {
2425 // dbg!(&annot.subtype);
@@ -79,23 +80,24 @@ fn run() -> Result<(), PdfError> {
7980 let annot_ref = old_file. create ( new_annot) ?;
8081 annots. push ( MaybeRef :: Indirect ( annot_ref) ) ;
8182
82- // let lazy_annots = Lazy::from_primitive(
83- // annots.to_primitive(&mut FileOptions::cached().storage()).unwrap(),
84- // &file.resolver()
85- // );
8683
87- // old_page.update_annots(annots, &old_file.resolver(), &mut FileOptions::cached().storage());
88- // let old_annots = old_page.annotations.to_primitive(&mut old_file).unwrap();
84+ match old_annots {
85+ MaybeRef :: Direct ( _) => {
86+ // need to update the whole page
87+ let mut new_page: Page = ( * old_page) . clone ( ) ;
8988
90-
91- // let layz_annots = Lazy::from(annots);
92- // match annots {
93- // MaybeRef::Indirect(annot) => {
94- // old_page.annotations = Lazy::from(annot);
95- // }
96- // }
97-
98- old_file. update ( old_page. get_plain_ref ( ) , old_page) ;
89+ let lazy_annots = Lazy :: safe (
90+ MaybeRef :: Indirect ( old_file. create ( annots) . unwrap ( ) ) ,
91+ & mut old_file
92+ ) . unwrap ( ) ;
93+ new_page. annotations = lazy_annots;
94+ PageRc :: update ( new_page, & old_page, & mut old_file) . unwrap ( ) ;
95+ }
96+ MaybeRef :: Indirect ( r) => {
97+ // can just update the annot reference
98+ old_file. update_ref ( & r, annots) . unwrap ( ) ;
99+ }
100+ }
99101 old_file. save_to ( "/Users/apple/Downloads/test_pdf/out.pdf" ) ?;
100102
101103 Ok ( ( ) )
@@ -105,4 +107,4 @@ fn main() {
105107 if let Err ( e) = run ( ) {
106108 println ! ( "{e}" ) ;
107109 }
108- }
110+ }
0 commit comments