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 crate::bindgen::{FPDF_ANNOTATION, FPDF_FORMHANDLE};
5use crate::bindings::PdfiumLibraryBindings;
6use crate::pdf::document::page::field::options::PdfFormFieldOptions;
7use crate::pdf::document::page::field::private::internal::PdfFormFieldPrivate;
8
9/// A single `PdfFormField` of type `PdfFormFieldType::ComboBox`. The form field object defines
10/// an interactive drop-down list widget that allows the user to either select a value
11/// from a list of options or type a value into a text field.
12///
13/// Form fields in Pdfium are wrapped inside page annotations of type `PdfPageAnnotationType::Widget`
14/// or `PdfPageAnnotationType::XfaWidget`. User-specified values can be retrieved directly from
15/// each form field object by unwrapping the form field from the annotation, or in bulk from the
16/// `PdfForm::field_values()` function.
17pub struct PdfFormComboBoxField<'a> {
18    form_handle: FPDF_FORMHANDLE,
19    annotation_handle: FPDF_ANNOTATION,
20    options: PdfFormFieldOptions<'a>,
21    bindings: &'a dyn PdfiumLibraryBindings,
22}
23
24impl<'a> PdfFormComboBoxField<'a> {
25    #[inline]
26    pub(crate) fn from_pdfium(
27        form_handle: FPDF_FORMHANDLE,
28        annotation_handle: FPDF_ANNOTATION,
29        bindings: &'a dyn PdfiumLibraryBindings,
30    ) -> Self {
31        PdfFormComboBoxField {
32            form_handle,
33            annotation_handle,
34            options: PdfFormFieldOptions::from_pdfium(form_handle, annotation_handle, bindings),
35            bindings,
36        }
37    }
38
39    /// Returns the [PdfiumLibraryBindings] used by this [PdfFormComboBoxField] object.
40    #[inline]
41    pub fn bindings(&self) -> &'a dyn PdfiumLibraryBindings {
42        self.bindings
43    }
44
45    /// Returns the collection of selectable options in this [PdfFormComboBoxField].
46    pub fn options(&self) -> &PdfFormFieldOptions {
47        &self.options
48    }
49
50    /// Returns the displayed label for the currently selected option in this [PdfFormComboBoxField] object, if any.
51    #[inline]
52    pub fn value(&self) -> Option<String> {
53        self.options()
54            .iter()
55            .find(|option| option.is_set())
56            .and_then(|option| option.label().cloned())
57    }
58}
59
60impl<'a> PdfFormFieldPrivate<'a> for PdfFormComboBoxField<'a> {
61    #[inline]
62    fn form_handle(&self) -> &FPDF_FORMHANDLE {
63        &self.form_handle
64    }
65
66    #[inline]
67    fn annotation_handle(&self) -> &FPDF_ANNOTATION {
68        &self.annotation_handle
69    }
70
71    #[inline]
72    fn bindings(&self) -> &dyn PdfiumLibraryBindings {
73        self.bindings
74    }
75}