oxidize-pdf 2.4.3

A pure Rust PDF generation and manipulation library with zero external dependencies
Documentation
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//! Form-related actions (Submit-Form, Reset-Form, Import-Data) per ISO 32000-1 ยง12.7.5

use crate::objects::{Dictionary, Object};

/// Submit-form action flags
#[derive(Debug, Clone, Copy)]
pub struct SubmitFormFlags {
    /// Include/exclude fields
    pub include: bool,
    /// Submit as HTML form instead of FDF
    pub html: bool,
    /// Include coordinates of mouse click
    pub coordinates: bool,
    /// Submit as XML instead of FDF
    pub xml: bool,
    /// Include annotations
    pub include_annotations: bool,
    /// Submit PDF file instead of FDF
    pub pdf: bool,
    /// Convert dates to standard format
    pub canonical_dates: bool,
    /// Include only user-entered data
    pub exclude_non_user: bool,
    /// Include field names without values
    pub include_no_value_fields: bool,
    /// Export as Windows code page
    pub export_format: bool,
}

impl Default for SubmitFormFlags {
    fn default() -> Self {
        Self {
            include: true,
            html: false,
            coordinates: false,
            xml: false,
            include_annotations: false,
            pdf: false,
            canonical_dates: false,
            exclude_non_user: false,
            include_no_value_fields: false,
            export_format: false,
        }
    }
}

impl SubmitFormFlags {
    /// Convert to integer flags value
    pub fn to_flags(&self) -> i32 {
        let mut flags = 0;
        if !self.include {
            flags |= 1 << 0;
        }
        if self.html {
            flags |= 1 << 2;
        }
        if self.coordinates {
            flags |= 1 << 4;
        }
        if self.xml {
            flags |= 1 << 5;
        }
        if self.include_annotations {
            flags |= 1 << 7;
        }
        if self.pdf {
            flags |= 1 << 8;
        }
        if self.canonical_dates {
            flags |= 1 << 9;
        }
        if self.exclude_non_user {
            flags |= 1 << 10;
        }
        if self.include_no_value_fields {
            flags |= 1 << 11;
        }
        if self.export_format {
            flags |= 1 << 12;
        }
        flags
    }
}

/// Submit-form action - submit form data to a URL
#[derive(Debug, Clone)]
pub struct SubmitFormAction {
    /// URL to submit to
    pub url: String,
    /// Fields to include/exclude (empty means all)
    pub fields: Vec<String>,
    /// Submission flags
    pub flags: SubmitFormFlags,
    /// Character set for submission
    pub charset: Option<String>,
}

impl SubmitFormAction {
    /// Create new submit-form action
    pub fn new(url: impl Into<String>) -> Self {
        Self {
            url: url.into(),
            fields: Vec::new(),
            flags: SubmitFormFlags::default(),
            charset: None,
        }
    }

    /// Submit as HTML form
    pub fn as_html(mut self) -> Self {
        self.flags.html = true;
        self
    }

    /// Submit as XML
    pub fn as_xml(mut self) -> Self {
        self.flags.xml = true;
        self
    }

    /// Submit entire PDF
    pub fn as_pdf(mut self) -> Self {
        self.flags.pdf = true;
        self
    }

    /// Include specific fields only
    pub fn with_fields(mut self, fields: Vec<String>) -> Self {
        self.fields = fields;
        self.flags.include = true;
        self
    }

    /// Exclude specific fields
    pub fn excluding_fields(mut self, fields: Vec<String>) -> Self {
        self.fields = fields;
        self.flags.include = false;
        self
    }

    /// Set character set
    pub fn with_charset(mut self, charset: impl Into<String>) -> Self {
        self.charset = Some(charset.into());
        self
    }

    /// Include mouse click coordinates
    pub fn with_coordinates(mut self) -> Self {
        self.flags.coordinates = true;
        self
    }

    /// Include annotations
    pub fn with_annotations(mut self) -> Self {
        self.flags.include_annotations = true;
        self
    }

    /// Convert to dictionary
    pub fn to_dict(&self) -> Dictionary {
        let mut dict = Dictionary::new();
        dict.set("Type", Object::Name("Action".to_string()));
        dict.set("S", Object::Name("SubmitForm".to_string()));
        dict.set("F", Object::String(self.url.clone()));

        // Add fields array if specified
        if !self.fields.is_empty() {
            let fields_array: Vec<Object> = self
                .fields
                .iter()
                .map(|f| Object::String(f.clone()))
                .collect();
            dict.set("Fields", Object::Array(fields_array));
        }

        // Add flags
        dict.set("Flags", Object::Integer(self.flags.to_flags() as i64));

        // Add charset if specified
        if let Some(charset) = &self.charset {
            dict.set("CharSet", Object::String(charset.clone()));
        }

        dict
    }
}

/// Reset-form action - reset form fields to default values
#[derive(Debug, Clone)]
pub struct ResetFormAction {
    /// Fields to reset (empty means all)
    pub fields: Vec<String>,
    /// Whether to include (true) or exclude (false) the specified fields
    pub include: bool,
}

impl ResetFormAction {
    /// Create new reset-form action (resets all fields)
    pub fn new() -> Self {
        Self {
            fields: Vec::new(),
            include: true,
        }
    }

    /// Reset specific fields only
    pub fn with_fields(mut self, fields: Vec<String>) -> Self {
        self.fields = fields;
        self.include = true;
        self
    }

    /// Reset all fields except specified ones
    pub fn excluding_fields(mut self, fields: Vec<String>) -> Self {
        self.fields = fields;
        self.include = false;
        self
    }

    /// Convert to dictionary
    pub fn to_dict(&self) -> Dictionary {
        let mut dict = Dictionary::new();
        dict.set("Type", Object::Name("Action".to_string()));
        dict.set("S", Object::Name("ResetForm".to_string()));

        // Add fields array if specified
        if !self.fields.is_empty() {
            let fields_array: Vec<Object> = self
                .fields
                .iter()
                .map(|f| Object::String(f.clone()))
                .collect();
            dict.set("Fields", Object::Array(fields_array));
        }

        // Add flags (bit 0: 0=include, 1=exclude)
        let flags = if self.include { 0 } else { 1 };
        dict.set("Flags", Object::Integer(flags));

        dict
    }
}

impl Default for ResetFormAction {
    fn default() -> Self {
        Self::new()
    }
}

/// Import-data action - import FDF or XFDF data
#[derive(Debug, Clone)]
pub struct ImportDataAction {
    /// File specification for the data file
    pub file: String,
}

impl ImportDataAction {
    /// Create new import-data action
    pub fn new(file: impl Into<String>) -> Self {
        Self { file: file.into() }
    }

    /// Convert to dictionary
    pub fn to_dict(&self) -> Dictionary {
        let mut dict = Dictionary::new();
        dict.set("Type", Object::Name("Action".to_string()));
        dict.set("S", Object::Name("ImportData".to_string()));
        dict.set("F", Object::String(self.file.clone()));
        dict
    }
}

/// Hide action - show or hide form fields and annotations
#[derive(Debug, Clone)]
pub struct HideAction {
    /// Target annotations/fields to hide
    pub targets: Vec<String>,
    /// Whether to hide (true) or show (false)
    pub hide: bool,
}

impl HideAction {
    /// Create new hide action
    pub fn new(targets: Vec<String>) -> Self {
        Self {
            targets,
            hide: true,
        }
    }

    /// Hide the targets
    pub fn hide(mut self) -> Self {
        self.hide = true;
        self
    }

    /// Show the targets
    pub fn show(mut self) -> Self {
        self.hide = false;
        self
    }

    /// Convert to dictionary
    pub fn to_dict(&self) -> Dictionary {
        let mut dict = Dictionary::new();
        dict.set("Type", Object::Name("Action".to_string()));
        dict.set("S", Object::Name("Hide".to_string()));

        // Add targets
        if self.targets.len() == 1 {
            dict.set("T", Object::String(self.targets[0].clone()));
        } else {
            let targets_array: Vec<Object> = self
                .targets
                .iter()
                .map(|t| Object::String(t.clone()))
                .collect();
            dict.set("T", Object::Array(targets_array));
        }

        dict.set("H", Object::Boolean(self.hide));

        dict
    }
}

/// Set-OCG-State action - set the state of optional content groups
#[derive(Debug, Clone)]
pub struct SetOCGStateAction {
    /// State changes to apply
    pub state_changes: Vec<OCGStateChange>,
    /// Whether to preserve radio button relationships
    pub preserve_rb: bool,
}

/// OCG state change specification
#[derive(Debug, Clone)]
pub enum OCGStateChange {
    /// Turn on OCGs
    On(Vec<String>),
    /// Turn off OCGs
    Off(Vec<String>),
    /// Toggle OCGs
    Toggle(Vec<String>),
}

impl SetOCGStateAction {
    /// Create new set-OCG-state action
    pub fn new() -> Self {
        Self {
            state_changes: Vec::new(),
            preserve_rb: true,
        }
    }

    /// Turn on specified OCGs
    pub fn turn_on(mut self, ocgs: Vec<String>) -> Self {
        self.state_changes.push(OCGStateChange::On(ocgs));
        self
    }

    /// Turn off specified OCGs
    pub fn turn_off(mut self, ocgs: Vec<String>) -> Self {
        self.state_changes.push(OCGStateChange::Off(ocgs));
        self
    }

    /// Toggle specified OCGs
    pub fn toggle(mut self, ocgs: Vec<String>) -> Self {
        self.state_changes.push(OCGStateChange::Toggle(ocgs));
        self
    }

    /// Set whether to preserve radio button relationships
    pub fn preserve_radio_buttons(mut self, preserve: bool) -> Self {
        self.preserve_rb = preserve;
        self
    }

    /// Convert to dictionary
    pub fn to_dict(&self) -> Dictionary {
        let mut dict = Dictionary::new();
        dict.set("Type", Object::Name("Action".to_string()));
        dict.set("S", Object::Name("SetOCGState".to_string()));

        // Build state array
        let mut state_array = Vec::new();
        for change in &self.state_changes {
            match change {
                OCGStateChange::On(ocgs) => {
                    state_array.push(Object::Name("ON".to_string()));
                    for ocg in ocgs {
                        state_array.push(Object::String(ocg.clone()));
                    }
                }
                OCGStateChange::Off(ocgs) => {
                    state_array.push(Object::Name("OFF".to_string()));
                    for ocg in ocgs {
                        state_array.push(Object::String(ocg.clone()));
                    }
                }
                OCGStateChange::Toggle(ocgs) => {
                    state_array.push(Object::Name("Toggle".to_string()));
                    for ocg in ocgs {
                        state_array.push(Object::String(ocg.clone()));
                    }
                }
            }
        }

        dict.set("State", Object::Array(state_array));
        dict.set("PreserveRB", Object::Boolean(self.preserve_rb));

        dict
    }
}

impl Default for SetOCGStateAction {
    fn default() -> Self {
        Self::new()
    }
}

/// JavaScript action - execute JavaScript code
#[derive(Debug, Clone)]
pub struct JavaScriptAction {
    /// JavaScript code to execute
    pub script: String,
}

impl JavaScriptAction {
    /// Create new JavaScript action
    pub fn new(script: impl Into<String>) -> Self {
        Self {
            script: script.into(),
        }
    }

    /// Convert to dictionary
    pub fn to_dict(&self) -> Dictionary {
        let mut dict = Dictionary::new();
        dict.set("Type", Object::Name("Action".to_string()));
        dict.set("S", Object::Name("JavaScript".to_string()));
        dict.set("JS", Object::String(self.script.clone()));
        dict
    }
}

/// Sound action - play a sound
#[derive(Debug, Clone)]
pub struct SoundAction {
    /// Sound object name
    pub sound: String,
    /// Volume (0.0 to 1.0)
    pub volume: f64,
    /// Whether to play synchronously
    pub synchronous: bool,
    /// Whether to repeat
    pub repeat: bool,
    /// Whether to mix with other sounds
    pub mix: bool,
}

impl SoundAction {
    /// Create new sound action
    pub fn new(sound: impl Into<String>) -> Self {
        Self {
            sound: sound.into(),
            volume: 1.0,
            synchronous: false,
            repeat: false,
            mix: false,
        }
    }

    /// Set volume (0.0 to 1.0)
    pub fn with_volume(mut self, volume: f64) -> Self {
        self.volume = volume.clamp(0.0, 1.0);
        self
    }

    /// Play synchronously
    pub fn synchronous(mut self) -> Self {
        self.synchronous = true;
        self
    }

    /// Repeat the sound
    pub fn repeat(mut self) -> Self {
        self.repeat = true;
        self
    }

    /// Mix with other sounds
    pub fn mix(mut self) -> Self {
        self.mix = true;
        self
    }

    /// Convert to dictionary
    pub fn to_dict(&self) -> Dictionary {
        let mut dict = Dictionary::new();
        dict.set("Type", Object::Name("Action".to_string()));
        dict.set("S", Object::Name("Sound".to_string()));
        dict.set("Sound", Object::String(self.sound.clone()));
        dict.set("Volume", Object::Real(self.volume));
        dict.set("Synchronous", Object::Boolean(self.synchronous));
        dict.set("Repeat", Object::Boolean(self.repeat));
        dict.set("Mix", Object::Boolean(self.mix));
        dict
    }
}