pdfium_render/pdf/document/page/field/
checkbox.rs

1//! Defines the [PdfFormCheckboxField] struct, exposing functionality related to a single
2//! form field of type [PdfFormFieldType::Checkbox].
3
4use crate::bindgen::{FPDF_ANNOTATION, FPDF_FORMHANDLE};
5use crate::bindings::PdfiumLibraryBindings;
6use crate::error::PdfiumError;
7use crate::pdf::document::page::field::private::internal::PdfFormFieldPrivate;
8
9#[cfg(doc)]
10use {
11    crate::pdf::document::form::PdfForm,
12    crate::pdf::document::page::annotation::PdfPageAnnotationType,
13    crate::pdf::document::page::field::{PdfFormField, PdfFormFieldType},
14};
15
16/// A single [PdfFormField] of type [PdfFormFieldType::Checkbox]. The form field object defines
17/// an interactive checkbox widget that can be toggled by the user.
18///
19/// Form fields in Pdfium are wrapped inside page annotations of type [PdfPageAnnotationType::Widget]
20/// or [PdfPageAnnotationType::XfaWidget]. User-specified values can be retrieved directly from
21/// each form field object by unwrapping the form field from the annotation, or in bulk from the
22/// [PdfForm::field_values()] function.
23pub struct PdfFormCheckboxField<'a> {
24    form_handle: FPDF_FORMHANDLE,
25    annotation_handle: FPDF_ANNOTATION,
26    bindings: &'a dyn PdfiumLibraryBindings,
27}
28
29impl<'a> PdfFormCheckboxField<'a> {
30    #[inline]
31    pub(crate) fn from_pdfium(
32        form_handle: FPDF_FORMHANDLE,
33        annotation_handle: FPDF_ANNOTATION,
34        bindings: &'a dyn PdfiumLibraryBindings,
35    ) -> Self {
36        PdfFormCheckboxField {
37            form_handle,
38            annotation_handle,
39            bindings,
40        }
41    }
42
43    /// Returns the [PdfiumLibraryBindings] used by this [PdfFormCheckboxField] object.
44    #[inline]
45    pub fn bindings(&self) -> &'a dyn PdfiumLibraryBindings {
46        self.bindings
47    }
48
49    /// Returns the index of this [PdfFormCheckboxField] in its control group.
50    ///
51    /// Control groups are used to group related interactive fields together. Checkboxes and
52    /// radio buttons can be grouped such that only a single button can be selected within
53    /// the control group. Each field within the group has a unique group index.
54    #[inline]
55    pub fn index_in_group(&self) -> u32 {
56        self.index_in_group_impl()
57    }
58
59    /// Returns the value set for the control group containing this [PdfFormCheckboxField].
60    ///
61    /// Control groups are used to group related interactive fields together. Checkboxes and
62    /// radio buttons can be grouped such that only a single button can be selected within
63    /// the control group. In this case, a single value can be shared by the group, indicating
64    /// the value of the currently selected field within the group.
65    #[inline]
66    pub fn group_value(&self) -> Option<String> {
67        self.value_impl()
68    }
69
70    /// Returns `true` if this [PdfFormCheckboxField] object has its checkbox checked.
71    #[inline]
72    pub fn is_checked(&self) -> Result<bool, PdfiumError> {
73        // The PDF Reference manual, version 1.7, states that an appearance stream of "Yes"
74        // can be used to indicate a selected checkbox. Pdfium's FPDFAnnot_IsChecked()
75        // function doesn't account for appearance streams, so we check the currently set
76        // appearance stream ourselves.
77
78        match self.appearance_stream_impl().as_ref() {
79            Some(appearance_stream) => {
80                // Appearance streams are in use. An appearance stream of "Yes" indicates
81                // a selected checkbox.
82
83                Ok(appearance_stream == "Yes" || appearance_stream == "/Yes")
84            }
85            None => {
86                // Appearance streams are not in use. We can fall back to using Pdfium's
87                // FPDFAnnot_IsChecked() implementation.
88
89                match self.is_checked_impl() {
90                    Ok(true) => Ok(true),
91                    Ok(false) => match self.group_value() {
92                        Some(value) => Ok(value == "Yes"),
93                        _ => Ok(false),
94                    },
95                    Err(err) => match self.group_value() {
96                        Some(value) => Ok(value == "Yes"),
97                        _ => Err(err),
98                    },
99                }
100            }
101        }
102    }
103
104    /// Checks or clears the checkbox of this [PdfFormCheckboxField] object.
105    #[inline]
106    pub fn set_checked(&mut self, is_checked: bool) -> Result<(), PdfiumError> {
107        // The manner in which we apply the value varies slightly depending on
108        // whether or not appearance streams are in use.
109
110        match self.appearance_stream_impl() {
111            Some(_) => {
112                // Appearance streams are in use. We must set both the value and
113                // the in-use appearance stream for the checkbox.
114
115                self.set_string_value("AS", if is_checked { "/Yes" } else { "/Off" })?;
116                self.set_value_impl(if is_checked { "/Yes" } else { "/Off" })
117            }
118            None => {
119                // Appearance streams are not in use.
120
121                self.set_value_impl(if is_checked { "Yes" } else { "Off" })
122            }
123        }
124    }
125}
126
127impl<'a> PdfFormFieldPrivate<'a> for PdfFormCheckboxField<'a> {
128    #[inline]
129    fn form_handle(&self) -> FPDF_FORMHANDLE {
130        self.form_handle
131    }
132
133    #[inline]
134    fn annotation_handle(&self) -> FPDF_ANNOTATION {
135        self.annotation_handle
136    }
137
138    #[inline]
139    fn bindings(&self) -> &dyn PdfiumLibraryBindings {
140        self.bindings
141    }
142}