Skip to main content

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