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

1//! Defines the [PdfFormTextField] struct, exposing functionality related to a single
2//! form field of type [PdfFormFieldType::Text].
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::Text]. The form field object defines
17/// an interactive data entry widget that allows the user to enter data by typing.
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 PdfFormTextField<'a> {
24    form_handle: FPDF_FORMHANDLE,
25    annotation_handle: FPDF_ANNOTATION,
26    bindings: &'a dyn PdfiumLibraryBindings,
27}
28
29impl<'a> PdfFormTextField<'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        PdfFormTextField {
37            form_handle,
38            annotation_handle,
39            bindings,
40        }
41    }
42
43    /// Returns the [PdfiumLibraryBindings] used by this [PdfFormTextField] object.
44    #[inline]
45    pub fn bindings(&self) -> &'a dyn PdfiumLibraryBindings {
46        self.bindings
47    }
48
49    /// Returns the value assigned to this [PdfFormTextField] object, if any.
50    #[inline]
51    pub fn value(&self) -> Option<String> {
52        self.value_impl()
53    }
54
55    /// Sets the value of this [PdfFormTextField] object.
56    #[inline]
57    pub fn set_value(&mut self, value: &str) -> Result<(), PdfiumError> {
58        self.set_value_impl(value)
59    }
60}
61
62impl<'a> PdfFormFieldPrivate<'a> for PdfFormTextField<'a> {
63    #[inline]
64    fn form_handle(&self) -> &FPDF_FORMHANDLE {
65        &self.form_handle
66    }
67
68    #[inline]
69    fn annotation_handle(&self) -> &FPDF_ANNOTATION {
70        &self.annotation_handle
71    }
72
73    #[inline]
74    fn bindings(&self) -> &dyn PdfiumLibraryBindings {
75        self.bindings
76    }
77}