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_7881",
76        feature = "pdfium_7763",
77        feature = "pdfium_7543",
78        feature = "pdfium_7350"
79    ))]
80    /// Controls whether or not this [PdfFormTextField] is configured as a multi-line text field.
81    #[inline]
82    pub fn set_is_multiline(&self, is_multiline: bool) -> Result<(), PdfiumError> {
83        self.update_one_flag_impl(PdfFormFieldFlags::TextMultiline, is_multiline)
84    }
85
86    /// Returns `true` if this [PdfFormTextField] is configured as a password field.
87    #[inline]
88    pub fn is_password(&self) -> bool {
89        self.get_flags_impl()
90            .contains(PdfFormFieldFlags::TextPassword)
91    }
92
93    #[cfg(any(
94        feature = "pdfium_future",
95        feature = "pdfium_7881",
96        feature = "pdfium_7763",
97        feature = "pdfium_7543",
98        feature = "pdfium_7350"
99    ))]
100    /// Controls whether or not this [PdfFormTextField] is configured as a password text field.
101    #[inline]
102    pub fn set_is_password(&self, is_password: bool) -> Result<(), PdfiumError> {
103        self.update_one_flag_impl(PdfFormFieldFlags::TextPassword, is_password)
104    }
105
106    /// Returns `true` if this [PdfFormTextField] represents the path of a file
107    /// whose contents are to be submitted as the value of the field.
108    ///
109    /// This flag was added in PDF version 1.4
110    pub fn is_file_select(&self) -> bool {
111        self.get_flags_impl()
112            .contains(PdfFormFieldFlags::TextFileSelect)
113    }
114
115    #[cfg(any(
116        feature = "pdfium_future",
117        feature = "pdfium_7881",
118        feature = "pdfium_7763",
119        feature = "pdfium_7543",
120        feature = "pdfium_7350"
121    ))]
122    /// Controls whether or not this [PdfFormTextField] represents the path of a file
123    /// whose contents are to be submitted as the value of the field.
124    ///
125    /// This flag was added in PDF version 1.4.
126    pub fn set_is_file_select(&mut self, is_file_select: bool) -> Result<(), PdfiumError> {
127        self.update_one_flag_impl(PdfFormFieldFlags::TextFileSelect, is_file_select)
128    }
129
130    /// Returns `true` if text entered into this [PdfFormTextField] should be spell checked.
131    pub fn is_spell_checked(&self) -> bool {
132        !self
133            .get_flags_impl()
134            .contains(PdfFormFieldFlags::TextDoNotSpellCheck)
135    }
136
137    #[cfg(any(
138        feature = "pdfium_future",
139        feature = "pdfium_7881",
140        feature = "pdfium_7763",
141        feature = "pdfium_7543",
142        feature = "pdfium_7350"
143    ))]
144    /// Controls whether or not text entered into this [PdfFormTextField] should be spell checked.
145    pub fn set_is_spell_checked(&mut self, is_spell_checked: bool) -> Result<(), PdfiumError> {
146        self.update_one_flag_impl(PdfFormFieldFlags::TextDoNotSpellCheck, !is_spell_checked)
147    }
148
149    /// Returns `true` if the internal area of this [PdfFormTextField] can scroll either
150    /// horizontally or vertically to accommodate text entry longer than what can fit
151    /// within the field's annotation bounds. If this value is `false`, then once the
152    /// field is full, no further text entry will be accepted.
153    ///
154    /// This flag was added in PDF version 1.4.
155    pub fn is_scrollable(&self) -> bool {
156        !self
157            .get_flags_impl()
158            .contains(PdfFormFieldFlags::TextDoNotScroll)
159    }
160
161    #[cfg(any(
162        feature = "pdfium_future",
163        feature = "pdfium_7881",
164        feature = "pdfium_7763",
165        feature = "pdfium_7543",
166        feature = "pdfium_7350"
167    ))]
168    /// Controls whether or not the internal area of this [PdfFormTextField] can scroll
169    /// either horizontally or vertically to accommodate text entry longer than what can fit
170    /// within the field's annotation bounds. If set to `false`, no further text entry
171    /// will be accepted once the field's annotation bounds are full.
172    ///
173    /// This flag was added in PDF version 1.4.
174    pub fn set_is_scrollable(&mut self, is_scrollable: bool) -> Result<(), PdfiumError> {
175        self.update_one_flag_impl(PdfFormFieldFlags::TextDoNotScroll, !is_scrollable)
176    }
177
178    /// Returns `true` if this [PdfFormTextField] is "combed", that is, automatically divided
179    /// into equally-spaced positions ("combs"), with the text in the field laid out into
180    /// those combs.
181    ///
182    /// For more information on this setting, refer to Table 8.77 of The PDF Reference
183    /// (Sixth Edition, PDF Format 1.7), on page 691.
184    ///
185    /// This flag was added in PDF version 1.5.
186    pub fn is_combed(&self) -> bool {
187        // This flag only takes effect if the multi-line, password, and file select flags
188        // are all unset.
189
190        !self.is_multiline()
191            && !self.is_password()
192            && !self.is_file_select()
193            && self.get_flags_impl().contains(PdfFormFieldFlags::TextComb)
194    }
195
196    // TODO: AJRC - 20/06/25 - there is little point providing the matching `set_is_combed()`
197    // function, because it makes little sense without being also able to set the `MaxValue`
198    // dictionary parameter that controls the number of combs. However, `MaxValue` must be
199    // an integer, and Pdfium does not currently provide a `FPDFAnnot_SetNumberValue()`
200    // function that could correctly set it.
201
202    /// Returns `true` if the text in this [PdfFormTextField] is a rich text string.
203    ///
204    /// This flag was added in PDF version 1.5.
205    pub fn is_rich_text(&self) -> bool {
206        self.get_flags_impl()
207            .contains(PdfFormFieldFlags::TextRichText)
208    }
209
210    #[cfg(any(
211        feature = "pdfium_future",
212        feature = "pdfium_7881",
213        feature = "pdfium_7763",
214        feature = "pdfium_7543",
215        feature = "pdfium_7350"
216    ))]
217    /// Controls whether or not the text in this [PdfFormTextField] is a rich text string.
218    ///
219    /// This flag was added in PDF version 1.5.
220    pub fn set_is_rich_text(&mut self, is_rich_text: bool) -> Result<(), PdfiumError> {
221        self.update_one_flag_impl(PdfFormFieldFlags::TextRichText, is_rich_text)
222    }
223}
224
225impl<'a> PdfFormFieldPrivate<'a> for PdfFormTextField<'a> {
226    #[inline]
227    fn form_handle(&self) -> FPDF_FORMHANDLE {
228        self.form_handle
229    }
230
231    #[inline]
232    fn annotation_handle(&self) -> FPDF_ANNOTATION {
233        self.annotation_handle
234    }
235}
236
237impl<'a> PdfiumLibraryBindingsAccessor<'a> for PdfFormTextField<'a> {}
238
239#[cfg(feature = "thread_safe")]
240unsafe impl<'a> Send for PdfFormTextField<'a> {}
241
242#[cfg(feature = "thread_safe")]
243unsafe impl<'a> Sync for PdfFormTextField<'a> {}