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

1//! Defines the [PdfPageAnnotationObjects] struct, exposing functionality related to the
2//! page objects contained within a single `PdfPageAnnotation`.
3
4use crate::bindgen::{FPDF_ANNOTATION, FPDF_DOCUMENT, FPDF_PAGE};
5use crate::bindings::PdfiumLibraryBindings;
6use crate::error::{PdfiumError, PdfiumInternalError};
7use crate::pdf::document::page::object::ownership::PdfPageObjectOwnership;
8use crate::pdf::document::page::object::private::internal::PdfPageObjectPrivate;
9use crate::pdf::document::page::object::PdfPageObject;
10use crate::pdf::document::page::objects::common::{
11    PdfPageObjectIndex, PdfPageObjectsCommon, PdfPageObjectsIterator,
12};
13use crate::pdf::document::page::objects::private::internal::PdfPageObjectsPrivate;
14use std::os::raw::c_int;
15
16/// The page objects contained within a single `PdfPageAnnotation`.
17///
18/// Content in an annotation is structured as a stream of [PdfPageObject] objects of different types:
19/// text objects, image objects, path objects, and so on.
20///
21/// Note that Pdfium does not support or recognize all PDF page object types. For instance,
22/// Pdfium does not currently support or recognize the External Object ("XObject") page object type
23/// supported by Adobe Acrobat and Foxit's commercial PDF SDK. In these cases, Pdfium will return
24/// `PdfPageObjectType::Unsupported`.
25///
26/// Page objects can be retrieved from any type of `PdfPageAnnotation`, but Pdfium currently
27/// only permits adding new page objects to, or removing existing page objects from, annotations
28/// of types `PdfPageAnnotationType::Ink` and `PdfPageAnnotationType::Stamp`. All other annotation
29/// types are read-only.
30pub struct PdfPageAnnotationObjects<'a> {
31    annotation_handle: FPDF_ANNOTATION,
32    ownership: PdfPageObjectOwnership,
33    bindings: &'a dyn PdfiumLibraryBindings,
34}
35
36impl<'a> PdfPageAnnotationObjects<'a> {
37    #[inline]
38    pub(crate) fn from_pdfium(
39        document_handle: FPDF_DOCUMENT,
40        page_handle: FPDF_PAGE,
41        annotation_handle: FPDF_ANNOTATION,
42        bindings: &'a dyn PdfiumLibraryBindings,
43    ) -> Self {
44        Self {
45            annotation_handle,
46            ownership: PdfPageObjectOwnership::owned_by_attached_annotation(
47                document_handle,
48                page_handle,
49                annotation_handle,
50            ),
51            bindings,
52        }
53    }
54
55    /// Returns the internal `FPDF_ANNOTATION` handle for the [PdfPageAnnotation] containing
56    /// this [PdfPageAnnotationObjects] collection.
57    #[inline]
58    pub(crate) fn annotation_handle(&self) -> FPDF_ANNOTATION {
59        self.annotation_handle
60    }
61}
62
63impl<'a> PdfPageObjectsPrivate<'a> for PdfPageAnnotationObjects<'a> {
64    #[inline]
65    fn ownership(&self) -> &PdfPageObjectOwnership {
66        &self.ownership
67    }
68
69    #[inline]
70    fn bindings(&self) -> &'a dyn PdfiumLibraryBindings {
71        self.bindings
72    }
73
74    #[inline]
75    fn len_impl(&self) -> PdfPageObjectIndex {
76        self.bindings()
77            .FPDFAnnot_GetObjectCount(self.annotation_handle()) as PdfPageObjectIndex
78    }
79
80    fn get_impl(&self, index: PdfPageObjectIndex) -> Result<PdfPageObject<'a>, PdfiumError> {
81        let object_handle = self
82            .bindings()
83            .FPDFAnnot_GetObject(self.annotation_handle(), index as c_int);
84
85        if object_handle.is_null() {
86            if index >= self.len() {
87                Err(PdfiumError::PageObjectIndexOutOfBounds)
88            } else {
89                Err(PdfiumError::PdfiumLibraryInternalError(
90                    PdfiumInternalError::Unknown,
91                ))
92            }
93        } else {
94            Ok(PdfPageObject::from_pdfium(
95                object_handle,
96                *self.ownership(),
97                self.bindings,
98            ))
99        }
100    }
101
102    #[inline]
103    fn iter_impl(&'a self) -> PdfPageObjectsIterator<'a> {
104        PdfPageObjectsIterator::new(self)
105    }
106
107    #[inline]
108    fn add_object_impl(
109        &mut self,
110        mut object: PdfPageObject<'a>,
111    ) -> Result<PdfPageObject<'a>, PdfiumError> {
112        object.add_object_to_annotation(self).map(|_| object)
113    }
114
115    #[inline]
116    fn remove_object_impl(
117        &mut self,
118        mut object: PdfPageObject<'a>,
119    ) -> Result<PdfPageObject<'a>, PdfiumError> {
120        object.remove_object_from_annotation().map(|_| object)
121    }
122}