Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Make /Annots indirect #26

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn main() -> std::io::Result<()> {
let page_id = Ref::new(3);
let font_id = Ref::new(4);
let content_id = Ref::new(5);
let annotation_id = Ref::new(6);
let font_name = Name(b"F1");

// Write the document catalog with a reference to the page tree.
Expand All @@ -29,11 +30,16 @@ fn main() -> std::io::Result<()> {
page.media_box(Rect::new(0.0, 0.0, 595.0, 842.0));
page.parent(page_tree_id);
page.contents(content_id);
page.annotations([annotation_id]);

// We also create the annotations list here that allows us to have things
// like links or comments on the page.
let mut annotations = page.annotations();
let mut annotation = annotations.push();
// We also need to specify which resources the page needs, which in our case
// is only a font that we name "F1" (the specific name doesn't matter).
page.resources().fonts().pair(font_name, font_id);
page.finish();

// We also create an annotation which allows us to have things like links or
// comments on the page.
let mut annotation = pdf.annotation(annotation_id);

// Write the type, area, alt-text, and color for our link annotation.
annotation.subtype(AnnotationType::Link);
Expand All @@ -58,12 +64,6 @@ fn main() -> std::io::Result<()> {
// you cannot accidentally forget it. The `finish()` method from the `Finish`
// trait is just a postfix-style version of dropping.
annotation.finish();
annotations.finish();

// We also need to specify which resources the page needs, which in our case
// is only a font that we name "F1" (the specific name doesn't matter).
page.resources().fonts().pair(font_name, font_id);
page.finish();

// Specify the font we want to use. Because Helvetica is one of the 14 base
// fonts shipped with every PDF reader, we don't have to embed any font
Expand Down
27 changes: 11 additions & 16 deletions src/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;

/// Writer for an _annotation dictionary_.
///
/// An array of this struct is created by [`Page::annotations`].
/// An array of this struct is created by [`Chunk::annotation`].
pub struct Annotation<'a> {
dict: Dict<'a>,
}
Expand Down Expand Up @@ -671,24 +671,19 @@ mod tests {
fn test_annotations() {
test!(
crate::tests::slice(|w| {
let mut page = w.page(Ref::new(1));
let mut annots = page.annotations();
annots.push().rect(Rect::new(0.0, 0.0, 1.0, 1.0));
annots.push().rect(Rect::new(1.0, 1.0, 0.0, 0.0));
annots.finish();
page.bleed_box(Rect::new(-100.0, -100.0, 100.0, 100.0));
w.annotation(Ref::new(1)).rect(Rect::new(0.0, 0.0, 1.0, 1.0));
w.annotation(Ref::new(2)).rect(Rect::new(1.0, 1.0, 0.0, 0.0));
}),
b"1 0 obj",
b"<<",
b" /Type /Page",
b" /Annots [<<",
b" /Type /Annot",
b" /Rect [0 0 1 1]",
b" >> <<",
b" /Type /Annot",
b" /Rect [1 1 0 0]",
b" >>]",
b" /BleedBox [-100 -100 100 100]",
b" /Type /Annot",
b" /Rect [0 0 1 1]",
b">>",
b"endobj\n",
b"2 0 obj",
b"<<",
b" /Type /Annot",
b" /Rect [1 1 0 0]",
b">>",
b"endobj\n\n",
);
Expand Down
5 changes: 5 additions & 0 deletions src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ impl Chunk {

/// Interactive features.
impl Chunk {
/// Start writing an annotation dictionary.
pub fn annotation(&mut self, id: Ref) -> Annotation<'_> {
self.indirect(id).start()
}

/// Start writing a form field dictionary.
pub fn form_field(&mut self, id: Ref) -> Field<'_> {
self.indirect(id).start()
Expand Down
7 changes: 4 additions & 3 deletions src/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,9 +1167,10 @@ impl<'a> Page<'a> {
self.insert(Name(b"Trans")).start()
}

/// Start writing the `/Annots` (annotations) array.'
pub fn annotations(&mut self) -> TypedArray<'_, Annotation> {
self.insert(Name(b"Annots")).array().typed()
/// Write the `/Annots` (annotations) array.
pub fn annotations(&mut self, ids: impl IntoIterator<Item = Ref>) -> &mut Self {
self.insert(Name(b"Annots")).array().items(ids);
self
}

/// Write the `/StructParents` attribute to indicate the [structure tree
Expand Down