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