Skip to content

Commit

Permalink
Change Page annotations to Lazy<MaybeRef>
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjie committed Dec 14, 2024
1 parent 9fc4f2b commit 1c170a2
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
108 changes: 108 additions & 0 deletions examples/src/bin/add_annot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
extern crate pdf;


use std::env::args;
use std::ops::Deref;

use pdf::content::{FormXObject, Op, serialize_ops};
use pdf::error::PdfError;
use pdf::file::{FileOptions, Log};
use pdf::font::{Font, FontData, TFont};
use pdf::object::*;
use pdf::primitive::{Dictionary, Name, PdfString, Primitive};

fn run() -> Result<(), PdfError> {
let path = args().nth(1).expect("no file given");
println!("read: {}", path);

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 mut new_annots = annots.deref().clone();
// for annot in &new_annots {
// dbg!(&annot.subtype);
// dbg!(&annot.rect);
// dbg!(&annot.color);
// dbg!(&annot.transparency);
// dbg!(&annot.ink_list);
// dbg!(&annot.line);
// dbg!(&annot.creation_date);
// dbg!(&annot.uuid);
// dbg!(&annot.border_style);
// dbg!(&annot.popup);
// dbg!(&annot.other);
// }

let mut bs = Dictionary::new();
bs.insert(Name::from("S"), PdfString::from("/S"));
bs.insert(Name::from("W"), PdfString::from("3"));
let new_annot = Annot {
subtype: Name::from("Line"),
// rect: Some(Rectangle {
// left: 89.774,
// bottom: 726.55,
// right: 300.961,
// top: 742.55,
// }),
rect: None,
contents: None,
page: Some(old_page.clone()),
border: None,
annotation_name: None,
date: None,
annot_flags: 4,
appearance_streams: None,
appearance_state: None,
color: Some(Primitive::Array(
vec![Primitive::Integer(1), Primitive::Integer(0), Primitive::Integer(0)]
)),
ink_list: None,
line: Some(Primitive::Array(
vec![
Primitive::Number(95.774),
Primitive::Number(734.237),
Primitive::Number(320.961),
Primitive::Number(734.863)
]
)),
// creation_date: None,
// uuid: None,
// border_style: Some(bs),
// border_style: None,
// popup: None,
other: Dictionary::new(),
// transparency: Some(1.0),
// transparency: None,
};

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();


// 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);
old_file.save_to("/Users/apple/Downloads/test_pdf/out.pdf")?;

Ok(())
}

fn main() {
if let Err(e) = run() {
println!("{e}");
}
}
7 changes: 7 additions & 0 deletions pdf/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,13 @@ impl<T> Default for Lazy<T> {
}
}

impl<T> From<RcRef<T>> for Lazy<T> {
fn from(value: RcRef<T>) -> Self {
Lazy { primitive: Primitive::Reference(value.inner), _marker: PhantomData }
}
}


//////////////////////////////////////
// Object for Primitives & other types
//////////////////////////////////////
Expand Down
8 changes: 7 additions & 1 deletion pdf/src/object/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ impl PageRc {
pub fn get_ref(&self) -> Ref<PagesNode> {
self.0.get_ref()
}
pub fn get_plain_ref(&self) -> PlainRef {
self.0.inner
}
}
impl Object for PageRc {
fn from_primitive(p: Primitive, resolve: &impl Resolve) -> Result<PageRc> {
Expand Down Expand Up @@ -295,7 +298,7 @@ pub struct Page {
pub vp: Option<Primitive>,

#[pdf(key="Annots")]
pub annotations: Lazy<Vec<MaybeRef<Annot>>>,
pub annotations: Lazy<MaybeRef<Vec<MaybeRef<Annot>>>>,

#[pdf(other)]
pub other: Dictionary,
Expand Down Expand Up @@ -984,6 +987,9 @@ pub struct Annot {
#[pdf(key="InkList")]
pub ink_list: Option<Primitive>,

#[pdf(key="L")]
pub line: Option<Primitive>,

#[pdf(other)]
pub other: Dictionary,
}
Expand Down

0 comments on commit 1c170a2

Please sign in to comment.