Skip to main content

pdfium_render/pdf/document/page/annotation/
xfa_widget.rs

1//! Defines the [PdfPageXfaWidgetAnnotation] struct, exposing functionality related to a single
2//! user annotation of type [PdfPageAnnotationType::XfaWidget].
3
4use crate::bindgen::{FPDF_ANNOTATION, FPDF_DOCUMENT, FPDF_FORMHANDLE, FPDF_PAGE};
5use crate::bindings::PdfiumLibraryBindings;
6use crate::pdf::document::page::annotation::attachment_points::PdfPageAnnotationAttachmentPoints;
7use crate::pdf::document::page::annotation::objects::PdfPageAnnotationObjects;
8use crate::pdf::document::page::annotation::private::internal::PdfPageAnnotationPrivate;
9use crate::pdf::document::page::field::PdfFormField;
10use crate::pdf::document::page::object::ownership::PdfPageObjectOwnership;
11use crate::pdf::document::page::objects::private::internal::PdfPageObjectsPrivate;
12use crate::pdfium::PdfiumLibraryBindingsAccessor;
13use std::marker::PhantomData;
14
15#[cfg(doc)]
16use {
17    crate::pdf::document::page::annotation::PdfPageAnnotation,
18    crate::pdf::document::page::annotation::PdfPageAnnotationType,
19};
20
21/// A single [PdfPageAnnotation] of type [PdfPageAnnotationType::XfaWidget].
22///
23/// Widget annotation types can wrap form fields. To access the form field, use the
24/// [PdfPageXfaWidgetAnnotation::form_field()] function.
25pub struct PdfPageXfaWidgetAnnotation<'a> {
26    annotation_handle: FPDF_ANNOTATION,
27    objects: PdfPageAnnotationObjects<'a>,
28    attachment_points: PdfPageAnnotationAttachmentPoints<'a>,
29    form_field: Option<PdfFormField<'a>>,
30    lifetime: PhantomData<&'a FPDF_ANNOTATION>,
31}
32
33impl<'a> PdfPageXfaWidgetAnnotation<'a> {
34    pub(crate) fn from_pdfium(
35        document_handle: FPDF_DOCUMENT,
36        page_handle: FPDF_PAGE,
37        annotation_handle: FPDF_ANNOTATION,
38        form_handle: Option<FPDF_FORMHANDLE>,
39        bindings: &'a dyn PdfiumLibraryBindings,
40    ) -> Self {
41        PdfPageXfaWidgetAnnotation {
42            annotation_handle,
43            objects: PdfPageAnnotationObjects::from_pdfium(
44                document_handle,
45                page_handle,
46                annotation_handle,
47            ),
48            attachment_points: PdfPageAnnotationAttachmentPoints::from_pdfium(annotation_handle),
49            form_field: form_handle.and_then(|form_handle| {
50                PdfFormField::from_pdfium(form_handle, annotation_handle, bindings)
51            }),
52            lifetime: PhantomData,
53        }
54    }
55
56    /// Returns an immutable reference to the [PdfFormField] wrapped by this
57    /// [PdfPageXfaWidgetAnnotation], if any.
58    #[inline]
59    pub fn form_field(&self) -> Option<&PdfFormField<'_>> {
60        self.form_field.as_ref()
61    }
62
63    /// Returns a mutable reference to the [PdfFormField] wrapped by this
64    /// [PdfPageXfaWidgetAnnotation], if any.
65    #[inline]
66    pub fn form_field_mut(&mut self) -> Option<&mut PdfFormField<'a>> {
67        self.form_field.as_mut()
68    }
69}
70
71impl<'a> PdfPageAnnotationPrivate<'a> for PdfPageXfaWidgetAnnotation<'a> {
72    #[inline]
73    fn handle(&self) -> FPDF_ANNOTATION {
74        self.annotation_handle
75    }
76
77    #[inline]
78    fn ownership(&self) -> &PdfPageObjectOwnership {
79        self.objects_impl().ownership()
80    }
81
82    #[inline]
83    fn objects_impl(&self) -> &PdfPageAnnotationObjects<'_> {
84        &self.objects
85    }
86
87    #[inline]
88    fn attachment_points_impl(&self) -> &PdfPageAnnotationAttachmentPoints<'_> {
89        &self.attachment_points
90    }
91}
92
93impl<'a> PdfiumLibraryBindingsAccessor<'a> for PdfPageXfaWidgetAnnotation<'a> {}
94
95#[cfg(feature = "thread_safe")]
96unsafe impl<'a> Send for PdfPageXfaWidgetAnnotation<'a> {}
97
98#[cfg(feature = "thread_safe")]
99unsafe impl<'a> Sync for PdfPageXfaWidgetAnnotation<'a> {}