From ac818d22019ed230b3314108c4b543f0872e6ed3 Mon Sep 17 00:00:00 2001 From: tingerrr Date: Sun, 5 Nov 2023 13:36:42 +0100 Subject: [PATCH] Allow merging of form fields and widget annotations --- src/annotations.rs | 2 +- src/forms.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/annotations.rs b/src/annotations.rs index 4827f8b..f7caaa7 100644 --- a/src/annotations.rs +++ b/src/annotations.rs @@ -4,7 +4,7 @@ use super::*; /// /// An array of this struct is created by [`Chunk::annotation`]. pub struct Annotation<'a> { - dict: Dict<'a>, + pub(crate) dict: Dict<'a>, } writer!(Annotation: |obj| { diff --git a/src/forms.rs b/src/forms.rs index 6fcf77a..ec46237 100644 --- a/src/forms.rs +++ b/src/forms.rs @@ -1,3 +1,5 @@ +use crate::types::AnnotationType; + use super::*; /// Writer for an _interactive forms dictionary_. PDF 1.2+. @@ -165,6 +167,19 @@ impl<'a> Field<'a> { pub fn additional_actions(&mut self) -> AdditionalActions<'_> { self.dict.insert(Name(b"AA")).start() } + + /// Finish writing this field as a widget annotation. This is encouraged + /// for fields which are non-root and terminal (i.e. they have a parent and + /// no children). + /// + /// While the widget annotation could be a single child to a + /// terminal field, most readers will not correctly read the form + /// field, if it's not merged with its annotation. + pub fn to_annotation(self) -> Annotation<'a> { + let mut annot = Annotation { dict: self.dict }; + annot.subtype(AnnotationType::Widget); + annot + } } deref!('a, Field<'a> => Dict<'a>, dict);