Skip to main content

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 std::marker::PhantomData;
5
6use crate::bindgen::{FPDF_ANNOTATION, FPDF_FORMHANDLE};
7use crate::error::PdfiumError;
8use crate::pdf::document::page::field::private::internal::{
9    PdfFormFieldFlags, PdfFormFieldPrivate,
10};
11use crate::pdfium::PdfiumLibraryBindingsAccessor;
12
13#[cfg(doc)]
14use {
15    crate::pdf::document::form::PdfForm,
16    crate::pdf::document::page::annotation::PdfPageAnnotationType,
17    crate::pdf::document::page::field::{PdfFormField, PdfFormFieldType},
18};
19
20/// A single [PdfFormField] of type [PdfFormFieldType::Text]. The form field object defines
21/// an interactive data entry widget that allows the user to enter data by typing.
22///
23/// Form fields in Pdfium are wrapped inside page annotations of type [PdfPageAnnotationType::Widget]
24/// or [PdfPageAnnotationType::XfaWidget]. User-specified values can be retrieved directly from
25/// each form field object by unwrapping the form field from the annotation, or in bulk from the
26/// [PdfForm::field_values()] function.
27pub struct PdfFormTextField<'a> {
28    form_handle: FPDF_FORMHANDLE,
29    annotation_handle: FPDF_ANNOTATION,
30    lifetime: PhantomData<&'a FPDF_ANNOTATION>,
31}
32
33impl<'a> PdfFormTextField<'a> {
34    #[inline]
35    pub(crate) fn from_pdfium(
36        form_handle: FPDF_FORMHANDLE,
37        annotation_handle: FPDF_ANNOTATION,
38    ) -> Self {
39        PdfFormTextField {
40            form_handle,
41            annotation_handle,
42            lifetime: PhantomData,
43        }
44    }
45
46    /// Returns the value assigned to this [PdfFormTextField] object, if any.
47    #[inline]
48    pub fn value(&self) -> Option<String> {
49        if self.is_rich_text() {
50            self.get_string_value("RV")
51        } else {
52            self.value_impl()
53        }
54    }
55
56    /// Sets the value of this [PdfFormTextField] object.
57    #[inline]
58    pub fn set_value(&mut self, value: &str) -> Result<(), PdfiumError> {
59        if self.is_rich_text() {
60            self.set_string_value("RV", value)
61        } else {
62            self.set_value_impl(value)
63        }
64    }
65
66    /// Returns `true` if this [PdfFormTextField] is configured as a multi-line text field.
67    #[inline]
68    pub fn is_multiline(&self) -> bool {
69        self.get_flags_impl()
70            .contains(PdfFormFieldFlags::TextMultiline)
71    }
72
73    #[cfg(any(
74        feature = "pdfium_future",
75        feature = "pdfium_8125",
76        feature = "pdfium_7881",
77        feature = "pdfium_7763",
78        feature = "pdfium_7543",
79        feature = "pdfium_7350"
80    ))]
81    /// Controls whether or not this [PdfFormTextField] is configured as a multi-line text field.
82    #[inline]
83    pub fn set_is_multiline(&self, is_multiline: bool) -> Result<(), PdfiumError> {
84        self.update_one_flag_impl(PdfFormFieldFlags::TextMultiline, is_multiline)
85    }
86
87    /// Returns `true` if this [PdfFormTextField] is configured as a password field.
88    #[inline]
89    pub fn is_password(&self) -> bool {
90        self.get_flags_impl()
91            .contains(PdfFormFieldFlags::TextPassword)
92    }
93
94    #[cfg(any(
95        feature = "pdfium_future",
96        feature = "pdfium_8125",
97        feature = "pdfium_7881",
98        feature = "pdfium_7763",
99        feature = "pdfium_7543",
100        feature = "pdfium_7350"
101    ))]
102    /// Controls whether or not this [PdfFormTextField] is configured as a password text field.
103    #[inline]
104    pub fn set_is_password(&self, is_password: bool) -> Result<(), PdfiumError> {
105        self.update_one_flag_impl(PdfFormFieldFlags::TextPassword, is_password)
106    }
107
108    /// Returns `true` if this [PdfFormTextField] represents the path of a file
109    /// whose contents are to be submitted as the value of the field.
110    ///
111    /// This flag was added in PDF version 1.4
112    pub fn is_file_select(&self) -> bool {
113        self.get_flags_impl()
114            .contains(PdfFormFieldFlags::TextFileSelect)
115    }
116
117    #[cfg(any(
118        feature = "pdfium_future",
119        feature = "pdfium_8125",
120        feature = "pdfium_7881",
121        feature = "pdfium_7763",
122        feature = "pdfium_7543",
123        feature = "pdfium_7350"
124    ))]
125    /// Controls whether or not this [PdfFormTextField] represents the path of a file
126    /// whose contents are to be submitted as the value of the field.
127    ///
128    /// This flag was added in PDF version 1.4.
129    pub fn set_is_file_select(&mut self, is_file_select: bool) -> Result<(), PdfiumError> {
130        self.update_one_flag_impl(PdfFormFieldFlags::TextFileSelect, is_file_select)
131    }
132
133    /// Returns `true` if text entered into this [PdfFormTextField] should be spell checked.
134    pub fn is_spell_checked(&self) -> bool {
135        !self
136            .get_flags_impl()
137            .contains(PdfFormFieldFlags::TextDoNotSpellCheck)
138    }
139
140    #[cfg(any(
141        feature = "pdfium_future",
142        feature = "pdfium_8125",
143        feature = "pdfium_7881",
144        feature = "pdfium_7763",
145        feature = "pdfium_7543",
146        feature = "pdfium_7350"
147    ))]
148    /// Controls whether or not text entered into this [PdfFormTextField] should be spell checked.
149    pub fn set_is_spell_checked(&mut self, is_spell_checked: bool) -> Result<(), PdfiumError> {
150        self.update_one_flag_impl(PdfFormFieldFlags::TextDoNotSpellCheck, !is_spell_checked)
151    }
152
153    /// Returns `true` if the internal area of this [PdfFormTextField] can scroll either
154    /// horizontally or vertically to accommodate text entry longer than what can fit
155    /// within the field's annotation bounds. If this value is `false`, then once the
156    /// field is full, no further text entry will be accepted.
157    ///
158    /// This flag was added in PDF version 1.4.
159    pub fn is_scrollable(&self) -> bool {
160        !self
161            .get_flags_impl()
162            .contains(PdfFormFieldFlags::TextDoNotScroll)
163    }
164
165    #[cfg(any(
166        feature = "pdfium_future",
167        feature = "pdfium_8125",
168        feature = "pdfium_7881",
169        feature = "pdfium_7763",
170        feature = "pdfium_7543",
171        feature = "pdfium_7350"
172    ))]
173    /// Controls whether or not the internal area of this [PdfFormTextField] can scroll
174    /// either horizontally or vertically to accommodate text entry longer than what can fit
175    /// within the field's annotation bounds. If set to `false`, no further text entry
176    /// will be accepted once the field's annotation bounds are full.
177    ///
178    /// This flag was added in PDF version 1.4.
179    pub fn set_is_scrollable(&mut self, is_scrollable: bool) -> Result<(), PdfiumError> {
180        self.update_one_flag_impl(PdfFormFieldFlags::TextDoNotScroll, !is_scrollable)
181    }
182
183    /// Returns `true` if this [PdfFormTextField] is "combed", that is, automatically divided
184    /// into equally-spaced positions ("combs"), with the text in the field laid out into
185    /// those combs.
186    ///
187    /// For more information on this setting, refer to Table 8.77 of The PDF Reference
188    /// (Sixth Edition, PDF Format 1.7), on page 691.
189    ///
190    /// This flag was added in PDF version 1.5.
191    pub fn is_combed(&self) -> bool {
192        // This flag only takes effect if the multi-line, password, and file select flags
193        // are all unset.
194
195        !self.is_multiline()
196            && !self.is_password()
197            && !self.is_file_select()
198            && self.get_flags_impl().contains(PdfFormFieldFlags::TextComb)
199    }
200
201    // TODO: AJRC - 20/06/25 - there is little point providing the matching `set_is_combed()`
202    // function, because it makes little sense without being also able to set the `MaxValue`
203    // dictionary parameter that controls the number of combs. However, `MaxValue` must be
204    // an integer, and Pdfium does not currently provide a `FPDFAnnot_SetNumberValue()`
205    // function that could correctly set it.
206
207    /// Returns `true` if the text in this [PdfFormTextField] is a rich text string.
208    ///
209    /// This flag was added in PDF version 1.5.
210    pub fn is_rich_text(&self) -> bool {
211        self.get_flags_impl()
212            .contains(PdfFormFieldFlags::TextRichText)
213    }
214
215    #[cfg(any(
216        feature = "pdfium_future",
217        feature = "pdfium_8125",
218        feature = "pdfium_7881",
219        feature = "pdfium_7763",
220        feature = "pdfium_7543",
221        feature = "pdfium_7350"
222    ))]
223    /// Controls whether or not the text in this [PdfFormTextField] is a rich text string.
224    ///
225    /// This flag was added in PDF version 1.5.
226    pub fn set_is_rich_text(&mut self, is_rich_text: bool) -> Result<(), PdfiumError> {
227        self.update_one_flag_impl(PdfFormFieldFlags::TextRichText, is_rich_text)
228    }
229}
230
231impl<'a> PdfFormFieldPrivate<'a> for PdfFormTextField<'a> {
232    #[inline]
233    fn form_handle(&self) -> FPDF_FORMHANDLE {
234        self.form_handle
235    }
236
237    #[inline]
238    fn annotation_handle(&self) -> FPDF_ANNOTATION {
239        self.annotation_handle
240    }
241}
242
243impl<'a> PdfiumLibraryBindingsAccessor<'a> for PdfFormTextField<'a> {}
244
245#[cfg(feature = "thread_safe")]
246unsafe impl<'a> Send for PdfFormTextField<'a> {}
247
248#[cfg(feature = "thread_safe")]
249unsafe impl<'a> Sync for PdfFormTextField<'a> {}