1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//! Defines the [PdfPageObjects] struct, exposing functionality related to the
//! page objects contained within a single `PdfPage`.
use crate::bindgen::{FPDF_DOCUMENT, FPDF_PAGE};
use crate::bindings::PdfiumLibraryBindings;
use crate::document::PdfDocument;
use crate::error::{PdfiumError, PdfiumInternalError};
use crate::page::PdfPage;
use crate::page_object::PdfPageObject;
use crate::page_object_group::PdfPageGroupObject;
use crate::page_object_private::internal::PdfPageObjectPrivate;
use crate::page_objects_common::{
PdfPageObjectIndex, PdfPageObjectsCommon, PdfPageObjectsIterator,
};
use crate::page_objects_private::internal::PdfPageObjectsPrivate;
use std::ops::RangeInclusive;
use std::os::raw::c_int;
/// The page objects contained within a single [PdfPage].
///
/// Content on a page is structured as a stream of [PdfPageObject] objects of different types:
/// text objects, image objects, path objects, and so on.
///
/// Note that Pdfium does not support or recognize all PDF page object types. For instance,
/// Pdfium does not currently support or recognize the External Object ("XObject") page object type
/// supported by Adobe Acrobat and Foxit's commercial PDF SDK. In these cases, Pdfium will return
/// `PdfPageObjectType::Unsupported`.
pub struct PdfPageObjects<'a> {
document_handle: FPDF_DOCUMENT,
page_handle: FPDF_PAGE,
bindings: &'a dyn PdfiumLibraryBindings,
do_regenerate_page_content_after_each_change: bool,
}
impl<'a> PdfPageObjects<'a> {
#[inline]
pub(crate) fn from_pdfium(
document_handle: FPDF_DOCUMENT,
page_handle: FPDF_PAGE,
bindings: &'a dyn PdfiumLibraryBindings,
) -> Self {
Self {
document_handle,
page_handle,
bindings,
do_regenerate_page_content_after_each_change: false,
}
}
/// Returns the internal `FPDF_PAGE` handle for the [PdfPage] containing this [PdfPageObjects] collection.
#[inline]
pub(crate) fn get_page_handle(&self) -> &FPDF_PAGE {
&self.page_handle
}
/// Sets whether or not this [PdfPageObjects] collection should trigger content regeneration
/// on its containing [PdfPage] when the collection is mutated.
#[inline]
pub(crate) fn do_regenerate_page_content_after_each_change(
&mut self,
do_regenerate_page_content_after_each_change: bool,
) {
self.do_regenerate_page_content_after_each_change =
do_regenerate_page_content_after_each_change;
}
/// Removes a single page object with the given source page object index from the given
/// source [PdfPage], adding the object to the end of this [PdfPageObjects] collection.
///
/// If the containing [PdfPage] has a content regeneration strategy of
/// `PdfPageContentRegenerationStrategy::AutomaticOnEveryChange` then content regeneration
/// will be triggered on the page.
///
/// Likewise, if the given source [PdfPage] has a content regeneration strategy of
/// `PdfPageContentRegenerationStrategy::AutomaticOnEveryChange` then content regeneration
/// will be triggered on the source page.
pub fn take_object_from_page(
&mut self,
source: &mut PdfPage,
source_page_object_index: PdfPageObjectIndex,
) -> Result<(), PdfiumError> {
self.take_object_range_from_page(
source,
source_page_object_index..=source_page_object_index,
)
}
/// Removes one or more page objects with the given range of indices from the given
/// source [PdfPage], adding the objects sequentially to the end of this
/// [PdfPageObjects] collection.
///
/// If the containing [PdfPage] has a content regeneration strategy of
/// `PdfPageContentRegenerationStrategy::AutomaticOnEveryChange` then content regeneration
/// will be triggered on the page.
///
/// Likewise, if the given source [PdfPage] has a content regeneration strategy of
/// `PdfPageContentRegenerationStrategy::AutomaticOnEveryChange` then content regeneration
/// will be triggered on the source page.
pub fn take_object_range_from_page(
&mut self,
source: &mut PdfPage,
source_page_object_range: RangeInclusive<PdfPageObjectIndex>,
) -> Result<(), PdfiumError> {
self.take_object_range_from_handles(
*source.handle(),
source.document(),
source_page_object_range,
)
}
// Take a raw FPDF_PAGE handle to avoid cascading lifetime problems associated with borrowing
// &'a mut PdfPage<'a>.
pub(crate) fn take_object_range_from_handles(
&mut self,
page: FPDF_PAGE,
document: &PdfDocument,
source_page_object_range: RangeInclusive<PdfPageObjectIndex>,
) -> Result<(), PdfiumError> {
let source = PdfPage::from_pdfium(page, None, document);
// Make sure we iterate over the range backwards. The collection's length will reduce
// each time we remove an object from it, and we must avoid overflow or Pdfium may segfault.
for index in source_page_object_range.rev() {
let mut object = source.objects().get(index)?;
object.remove_object_from_page()?;
object.add_object_to_page(self)?;
}
Ok(())
}
/// Moves all page objects in the given [PdfPage] into this [PdfPageObjects] collection,
/// appending them to the end of this [PdfPageObjects] collection. The given [PdfPage]
/// will be drained of all page objects once this operation is completed.
///
/// If the containing [PdfPage] has a content regeneration strategy of
/// `PdfPageContentRegenerationStrategy::AutomaticOnEveryChange` then content regeneration
/// will be triggered on the page.
///
/// Likewise, if the given source [PdfPage] has a content regeneration strategy of
/// `PdfPageContentRegenerationStrategy::AutomaticOnEveryChange` then content regeneration
/// will be triggered on the source page.
pub fn take_all(&mut self, source: &mut PdfPage) -> Result<(), PdfiumError> {
self.take_object_range_from_page(source, source.objects().as_range_inclusive())
}
/// Creates a new [PdfPageGroupObject] object group that includes any page objects in this
/// [PdfPageObjects] collection matching the given predicate function.
pub fn create_group<F>(&'a self, predicate: F) -> Result<PdfPageGroupObject<'a>, PdfiumError>
where
F: Fn(&PdfPageObject) -> bool,
{
let mut result = self.create_empty_group();
for mut object in self.iter().filter(predicate) {
result.push(&mut object)?;
}
Ok(result)
}
/// Creates a new [PdfPageGroupObject] object group that can accept any [PdfPageObject]
/// in this [PdfPageObjects]. The newly created group will be empty;
/// you will need to manually add to it the objects you want to manipulate.
#[inline]
pub fn create_empty_group(&self) -> PdfPageGroupObject<'a> {
PdfPageGroupObject::from_pdfium(
self.page_handle,
self.bindings,
self.do_regenerate_page_content_after_each_change,
)
}
}
impl<'a> PdfPageObjectsPrivate<'a> for PdfPageObjects<'a> {
#[inline]
fn document_handle(&self) -> FPDF_DOCUMENT {
self.document_handle
}
#[inline]
fn bindings(&self) -> &'a dyn PdfiumLibraryBindings {
self.bindings
}
#[inline]
fn len_impl(&self) -> PdfPageObjectIndex {
self.bindings.FPDFPage_CountObjects(self.page_handle) as PdfPageObjectIndex
}
fn get_impl(&self, index: PdfPageObjectIndex) -> Result<PdfPageObject<'a>, PdfiumError> {
if index >= self.len() {
return Err(PdfiumError::PageObjectIndexOutOfBounds);
}
let object_handle = self
.bindings
.FPDFPage_GetObject(self.page_handle, index as c_int);
if object_handle.is_null() {
if let Some(error) = self.bindings.get_pdfium_last_error() {
Err(PdfiumError::PdfiumLibraryInternalError(error))
} else {
// This would be an unusual situation; a null handle indicating failure,
// yet pdfium's error code indicates success.
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
} else {
Ok(PdfPageObject::from_pdfium(
object_handle,
Some(self.page_handle),
None,
self.bindings,
))
}
}
#[inline]
fn iter_impl(&'a self) -> PdfPageObjectsIterator<'a> {
PdfPageObjectsIterator::new(self)
}
fn add_object_impl(
&mut self,
mut object: PdfPageObject<'a>,
) -> Result<PdfPageObject<'a>, PdfiumError> {
object.add_object_to_page(self).and_then(|_| {
if self.do_regenerate_page_content_after_each_change {
if !self
.bindings
.is_true(self.bindings.FPDFPage_GenerateContent(self.page_handle))
{
if let Some(error) = self.bindings.get_pdfium_last_error() {
Err(PdfiumError::PdfiumLibraryInternalError(error))
} else {
// This would be an unusual situation; an FPDF_BOOL result indicating failure,
// yet pdfium's error code indicates success.
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
} else {
Ok(object)
}
} else {
Ok(object)
}
})
}
fn remove_object_impl(
&mut self,
mut object: PdfPageObject<'a>,
) -> Result<PdfPageObject<'a>, PdfiumError> {
object.remove_object_from_page().and_then(|_| {
if self.do_regenerate_page_content_after_each_change {
self.bindings.FPDFPage_GenerateContent(self.page_handle);
if let Some(error) = self.bindings.get_pdfium_last_error() {
Err(PdfiumError::PdfiumLibraryInternalError(error))
} else {
Ok(object)
}
} else {
Ok(object)
}
})
}
}