Skip to main content

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

1//! Defines the [PdfFormComboBoxField] struct, exposing functionality related to a single
2//! form field of type [PdfFormFieldType::ComboBox].
3
4use std::marker::PhantomData;
5
6use crate::bindgen::{FPDF_ANNOTATION, FPDF_FORMHANDLE};
7use crate::pdf::document::page::field::options::PdfFormFieldOptions;
8use crate::pdf::document::page::field::private::internal::{
9    PdfFormFieldFlags, PdfFormFieldPrivate,
10};
11use crate::pdfium::PdfiumLibraryBindingsAccessor;
12
13#[cfg(any(
14    feature = "pdfium_future",
15    feature = "pdfium_7763",
16    feature = "pdfium_7543",
17    feature = "pdfium_7350"
18))]
19use crate::error::PdfiumError;
20
21#[cfg(doc)]
22use {
23    crate::pdf::document::form::PdfForm,
24    crate::pdf::document::page::annotation::PdfPageAnnotationType,
25    crate::pdf::document::page::field::{PdfFormField, PdfFormFieldType},
26};
27
28/// A single [PdfFormField] of type [PdfFormFieldType::ComboBox]. The form field object defines
29/// an interactive drop-down list widget that allows the user to either select a value
30/// from a list of options or type a value into a text field.
31///
32/// Form fields in Pdfium are wrapped inside page annotations of type [PdfPageAnnotationType::Widget]
33/// or [PdfPageAnnotationType::XfaWidget]. User-specified values can be retrieved directly from
34/// each form field object by unwrapping the form field from the annotation, or in bulk from the
35/// [PdfForm::field_values()] function.
36pub struct PdfFormComboBoxField<'a> {
37    form_handle: FPDF_FORMHANDLE,
38    annotation_handle: FPDF_ANNOTATION,
39    options: PdfFormFieldOptions<'a>,
40    lifetime: PhantomData<&'a FPDF_ANNOTATION>,
41}
42
43impl<'a> PdfFormComboBoxField<'a> {
44    #[inline]
45    pub(crate) fn from_pdfium(
46        form_handle: FPDF_FORMHANDLE,
47        annotation_handle: FPDF_ANNOTATION,
48    ) -> Self {
49        PdfFormComboBoxField {
50            form_handle,
51            annotation_handle,
52            options: PdfFormFieldOptions::from_pdfium(form_handle, annotation_handle),
53            lifetime: PhantomData,
54        }
55    }
56
57    /// Returns the collection of selectable options in this [PdfFormComboBoxField].
58    pub fn options(&self) -> &PdfFormFieldOptions<'_> {
59        &self.options
60    }
61
62    /// Returns the displayed label for the currently selected option in this [PdfFormComboBoxField] object, if any.
63    #[inline]
64    pub fn value(&self) -> Option<String> {
65        self.options()
66            .iter()
67            .find(|option| option.is_set())
68            .and_then(|option| option.label().cloned())
69    }
70
71    /// Returns `true` if this [PdfFormComboBoxField] also includes an editable text box.
72    /// If `false`, this combo box field only includes a drop-down list.
73    #[inline]
74    pub fn has_editable_text_box(&self) -> bool {
75        self.get_flags_impl()
76            .contains(PdfFormFieldFlags::ChoiceEdit)
77    }
78
79    #[cfg(any(
80        feature = "pdfium_future",
81        feature = "pdfium_7763",
82        feature = "pdfium_7543",
83        feature = "pdfium_7350"
84    ))]
85    /// Controls whether or not this [PdfFormComboBoxField] includes an editable text box
86    /// in addition to a drop-down list.
87    #[inline]
88    pub fn set_has_editable_text_box(
89        &mut self,
90        has_editable_text_box: bool,
91    ) -> Result<(), PdfiumError> {
92        self.update_one_flag_impl(PdfFormFieldFlags::ChoiceEdit, has_editable_text_box)
93    }
94
95    /// Returns `true` if the option items of this [PdfFormComboBoxField] should be sorted
96    /// alphabetically.
97    ///
98    /// This flag is intended for use by form authoring tools, not by PDF viewer applications.
99    #[inline]
100    pub fn is_sorted(&self) -> bool {
101        self.get_flags_impl()
102            .contains(PdfFormFieldFlags::ChoiceSort)
103    }
104
105    #[cfg(any(
106        feature = "pdfium_future",
107        feature = "pdfium_7763",
108        feature = "pdfium_7543",
109        feature = "pdfium_7350"
110    ))]
111    /// Controls whether or not the option items of this [PdfFormComboBoxField] should be
112    /// sorted alphabetically.
113    ///
114    /// This flag is intended for use by form authoring tools, not by PDF viewer applications.
115    #[inline]
116    pub fn set_is_sorted(&mut self, is_sorted: bool) -> Result<(), PdfiumError> {
117        self.update_one_flag_impl(PdfFormFieldFlags::ChoiceSort, is_sorted)
118    }
119
120    /// Returns `true` if more than one of the option items in this [PdfFormComboBoxField]
121    /// may be selected simultaneously. If `false`, only one item at a time may be selected.
122    ///
123    /// This flag was added in PDF version 1.4.
124    pub fn is_multiselect(&self) -> bool {
125        self.get_flags_impl()
126            .contains(PdfFormFieldFlags::ChoiceMultiSelect)
127    }
128
129    #[cfg(any(
130        feature = "pdfium_future",
131        feature = "pdfium_7763",
132        feature = "pdfium_7543",
133        feature = "pdfium_7350"
134    ))]
135    /// Controls whether more than one of the option items in this [PdfFormComboBoxField]
136    /// may be selected simultaneously.
137    ///
138    /// This flag was added in PDF version 1.4.
139    pub fn set_is_multiselect(&mut self, is_multiselect: bool) -> Result<(), PdfiumError> {
140        self.update_one_flag_impl(PdfFormFieldFlags::ChoiceMultiSelect, is_multiselect)
141    }
142
143    /// Returns `true` if text entered into the editable text box included in this
144    /// [PdfFormComboBoxField] should be spell checked.
145    ///
146    /// This flag is meaningful only if the [PdfFormComboBoxField::has_editable_text_box()]
147    /// flag is also `true`.
148    ///
149    /// This flag was added in PDF version 1.4.
150    pub fn is_spell_checked(&self) -> bool {
151        !self
152            .get_flags_impl()
153            .contains(PdfFormFieldFlags::TextDoNotSpellCheck)
154    }
155
156    #[cfg(any(
157        feature = "pdfium_future",
158        feature = "pdfium_7763",
159        feature = "pdfium_7543",
160        feature = "pdfium_7350"
161    ))]
162    /// Controls whether or not text entered into the editable text box included in this
163    /// [PdfFormComboBoxField] should be spell checked.
164    ///
165    /// This flag was added in PDF version 1.4.
166    pub fn set_is_spell_checked(&mut self, is_spell_checked: bool) -> Result<(), PdfiumError> {
167        self.update_one_flag_impl(PdfFormFieldFlags::TextDoNotSpellCheck, !is_spell_checked)
168    }
169
170    /// Returns `true` if any new value is committed to this [PdfFormComboBoxField]
171    /// as soon as a selection is made with the pointing device. This option enables
172    /// applications to perform an action once a selection is made, without requiring
173    /// the user to exit the field. If `false`, any new value is not committed until the
174    /// user exits the field.
175    ///
176    /// This flag was added in PDF version 1.5.
177    pub fn is_commit_on_selection_change(&self) -> bool {
178        self.get_flags_impl()
179            .contains(PdfFormFieldFlags::ChoiceCommitOnSelectionChange)
180    }
181
182    #[cfg(any(
183        feature = "pdfium_future",
184        feature = "pdfium_7763",
185        feature = "pdfium_7543",
186        feature = "pdfium_7350"
187    ))]
188    /// Controls whether or not any new value is committed to this [PdfFormComboBoxField]
189    /// as soon as a selection is made with the pointing device.
190    ///
191    /// This flag was added in PDF version 1.5.
192    pub fn set_is_commit_on_selection_change(
193        &mut self,
194        is_commit_on_selection_change: bool,
195    ) -> Result<(), PdfiumError> {
196        self.update_one_flag_impl(
197            PdfFormFieldFlags::ChoiceCommitOnSelectionChange,
198            is_commit_on_selection_change,
199        )
200    }
201}
202
203impl<'a> PdfFormFieldPrivate<'a> for PdfFormComboBoxField<'a> {
204    #[inline]
205    fn form_handle(&self) -> FPDF_FORMHANDLE {
206        self.form_handle
207    }
208
209    #[inline]
210    fn annotation_handle(&self) -> FPDF_ANNOTATION {
211        self.annotation_handle
212    }
213}
214
215impl<'a> PdfiumLibraryBindingsAccessor<'a> for PdfFormComboBoxField<'a> {}
216
217#[cfg(feature = "thread_safe")]
218unsafe impl<'a> Send for PdfFormComboBoxField<'a> {}
219
220#[cfg(feature = "thread_safe")]
221unsafe impl<'a> Sync for PdfFormComboBoxField<'a> {}