pdfox 0.1.0

A pure-Rust PDF library — create, parse, and render PDF documents with zero C 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
/// AcroForm: interactive PDF form fields.
///
/// Supports text fields, checkboxes, radio buttons, dropdowns (combo boxes),
/// and push buttons. Fields can be pre-filled with values.
///
/// PDF spec: § 12.7 Interactive forms

use crate::color::Color;
use crate::font::BuiltinFont;
use crate::object::{ObjRef, PdfDict, PdfObject};
use crate::writer::PdfWriter;

/// Horizontal alignment within a field
#[derive(Debug, Clone, Copy)]
pub enum FieldAlign {
    Left   = 0,
    Center = 1,
    Right  = 2,
}

/// The visual appearance zone for a field widget
#[derive(Debug, Clone)]
pub struct FieldRect {
    pub x: f64,
    pub y: f64,
    pub width: f64,
    pub height: f64,
}

impl FieldRect {
    pub fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
        Self { x, y, width, height }
    }

    fn to_pdf_array(&self) -> PdfObject {
        PdfObject::Array(vec![
            PdfObject::Real(self.x),
            PdfObject::Real(self.y),
            PdfObject::Real(self.x + self.width),
            PdfObject::Real(self.y + self.height),
        ])
    }
}

/// All supported field types
#[derive(Debug, Clone)]
pub enum FieldKind {
    /// Single or multi-line text input
    Text {
        default_value: Option<String>,
        multiline: bool,
        password: bool,
        max_len: Option<u32>,
    },
    /// Boolean checkbox
    Checkbox {
        checked: bool,
        export_value: String,
    },
    /// One button in a radio group — all buttons with the same `group` name are linked
    Radio {
        group: String,
        export_value: String,
        selected: bool,
    },
    /// Dropdown / combo box
    Dropdown {
        options: Vec<String>,
        selected: Option<String>,
        editable: bool,
    },
    /// List box (shows multiple options)
    ListBox {
        options: Vec<String>,
        selected: Vec<String>,
        multi_select: bool,
    },
    /// Push button (typically triggers JavaScript — stored as action)
    Button {
        label: String,
    },
}

/// A single form field with its visual widget
#[derive(Debug, Clone)]
pub struct FormField {
    /// Unique field name (used in data export)
    pub name: String,
    /// Tooltip / description shown on hover
    pub tooltip: Option<String>,
    pub kind: FieldKind,
    pub rect: FieldRect,
    /// Page index (0-based) this field appears on
    pub page: usize,
    pub font: BuiltinFont,
    pub font_size: f64,
    pub text_color: Color,
    pub bg_color: Option<Color>,
    pub border_color: Option<Color>,
    pub border_width: f64,
    pub align: FieldAlign,
    pub read_only: bool,
    pub required: bool,
}

impl FormField {
    pub fn text(name: impl Into<String>, rect: FieldRect, page: usize) -> Self {
        Self::base(name, FieldKind::Text {
            default_value: None,
            multiline: false,
            password: false,
            max_len: None,
        }, rect, page)
    }

    pub fn multiline_text(name: impl Into<String>, rect: FieldRect, page: usize) -> Self {
        Self::base(name, FieldKind::Text {
            default_value: None,
            multiline: true,
            password: false,
            max_len: None,
        }, rect, page)
    }

    pub fn password(name: impl Into<String>, rect: FieldRect, page: usize) -> Self {
        Self::base(name, FieldKind::Text {
            default_value: None,
            multiline: false,
            password: true,
            max_len: None,
        }, rect, page)
    }

    pub fn checkbox(name: impl Into<String>, rect: FieldRect, page: usize, checked: bool) -> Self {
        Self::base(name, FieldKind::Checkbox {
            checked,
            export_value: "Yes".into(),
        }, rect, page)
    }

    pub fn radio(name: impl Into<String>, group: impl Into<String>, export_value: impl Into<String>, rect: FieldRect, page: usize, selected: bool) -> Self {
        Self::base(name, FieldKind::Radio {
            group: group.into(),
            export_value: export_value.into(),
            selected,
        }, rect, page)
    }

    pub fn dropdown(name: impl Into<String>, options: Vec<String>, rect: FieldRect, page: usize) -> Self {
        Self::base(name, FieldKind::Dropdown {
            options,
            selected: None,
            editable: false,
        }, rect, page)
    }

    pub fn button(name: impl Into<String>, label: impl Into<String>, rect: FieldRect, page: usize) -> Self {
        Self::base(name, FieldKind::Button { label: label.into() }, rect, page)
    }

    fn base(name: impl Into<String>, kind: FieldKind, rect: FieldRect, page: usize) -> Self {
        Self {
            name: name.into(),
            tooltip: None,
            kind,
            rect,
            page,
            font: BuiltinFont::Helvetica,
            font_size: 10.0,
            text_color: Color::BLACK,
            bg_color: Some(Color::WHITE),
            border_color: Some(Color::rgb_u8(150, 150, 150)),
            border_width: 1.0,
            align: FieldAlign::Left,
            read_only: false,
            required: false,
        }
    }

    // ── Builder methods ───────────────────────────────────────────────────────

    pub fn value(mut self, v: impl Into<String>) -> Self {
        if let FieldKind::Text { ref mut default_value, .. } = self.kind {
            *default_value = Some(v.into());
        }
        self
    }

    pub fn tooltip(mut self, t: impl Into<String>) -> Self {
        self.tooltip = Some(t.into());
        self
    }

    pub fn font_size(mut self, s: f64) -> Self {
        self.font_size = s;
        self
    }

    pub fn bg(mut self, c: Color) -> Self {
        self.bg_color = Some(c);
        self
    }

    pub fn border(mut self, c: Color, w: f64) -> Self {
        self.border_color = Some(c);
        self.border_width = w;
        self
    }

    pub fn read_only(mut self) -> Self {
        self.read_only = true;
        self
    }

    pub fn required(mut self) -> Self {
        self.required = true;
        self
    }

    pub fn align(mut self, a: FieldAlign) -> Self {
        self.align = a;
        self
    }

    pub fn max_len(mut self, n: u32) -> Self {
        if let FieldKind::Text { ref mut max_len, .. } = self.kind {
            *max_len = Some(n);
        }
        self
    }
}

/// The AcroForm for a document — collects all fields
pub struct AcroForm {
    pub fields: Vec<FormField>,
    /// Suppress default appearance generation (advanced use)
    pub need_appearances: bool,
}

impl AcroForm {
    pub fn new() -> Self {
        Self { fields: Vec::new(), need_appearances: true }
    }

    pub fn add(mut self, field: FormField) -> Self {
        self.fields.push(field);
        self
    }

    pub fn is_empty(&self) -> bool {
        self.fields.is_empty()
    }

    /// Write all form fields to the writer.
    /// Returns (acroform_dict, vec of (page_index, widget_ref)) for the caller
    /// to add each widget ref to the correct page's /Annots array.
    pub fn write(
        &self,
        writer: &mut PdfWriter,
        page_refs: &[ObjRef],
    ) -> (PdfDict, Vec<(usize, ObjRef)>) {
        let mut field_refs: Vec<ObjRef> = Vec::new();
        let mut widget_placement: Vec<(usize, ObjRef)> = Vec::new();

        for field in &self.fields {
            let field_ref = self.write_field(writer, field, page_refs);
            field_refs.push(field_ref);
            widget_placement.push((field.page, field_ref));
        }

        // Build /AcroForm dictionary
        let mut acroform = PdfDict::new();
        acroform.set(
            "Fields",
            PdfObject::Array(field_refs.iter().map(|r| PdfObject::Reference(*r)).collect()),
        );
        acroform.set("NeedAppearances", PdfObject::Boolean(self.need_appearances));

        // Default resources for appearance streams
        let mut dr = PdfDict::new();
        let mut font_dict = PdfDict::new();
        let mut helv = PdfDict::new();
        helv.set("Type", PdfObject::name("Font"));
        helv.set("Subtype", PdfObject::name("Type1"));
        helv.set("BaseFont", PdfObject::name("Helvetica"));
        font_dict.set("Helv", PdfObject::Dictionary(helv));
        dr.set("Font", PdfObject::Dictionary(font_dict));
        acroform.set("DR", PdfObject::Dictionary(dr));
        acroform.set("DA", PdfObject::string("/Helv 10 Tf 0 g"));

        (acroform, widget_placement)
    }

    fn write_field(&self, writer: &mut PdfWriter, field: &FormField, page_refs: &[ObjRef]) -> ObjRef {
        let field_ref = writer.reserve();
        let mut dict = PdfDict::new();

        // Common widget annotation fields
        dict.set("Type", PdfObject::name("Annot"));
        dict.set("Subtype", PdfObject::name("Widget"));
        dict.set("Rect", field.rect.to_pdf_array());

        if let Some(&page_ref) = page_refs.get(field.page) {
            dict.set("P", PdfObject::Reference(page_ref));
        }

        if let Some(ref tip) = field.tooltip {
            dict.set("TU", PdfObject::string(tip.as_str()));
        }

        // Field flags
        let mut ff: i64 = 0;
        if field.read_only { ff |= 1; }
        if field.required  { ff |= 2; }

        // Default appearance string
        let da = format!("/Helv {} Tf {} g",
            field.font_size as i32,
            match field.text_color {
                Color::Gray(g) => format!("{:.3}", g),
                Color::Rgb(r, g, b) => format!("{:.3} {:.3} {:.3} rg", r, g, b),
                _ => "0 g".into(),
            }
        );

        match &field.kind {
            FieldKind::Text { default_value, multiline, password, max_len } => {
                dict.set("FT", PdfObject::name("Tx"));
                dict.set("T",  PdfObject::string(field.name.as_str()));
                dict.set("DA", PdfObject::string(da.as_str()));
                dict.set("Q",  PdfObject::Integer(field.align as i64));

                let mut field_flags: i64 = ff;
                if *multiline { field_flags |= 1 << 12; }
                if *password  { field_flags |= 1 << 13; }
                dict.set("Ff", PdfObject::Integer(field_flags));

                if let Some(ref v) = default_value {
                    dict.set("V",  PdfObject::string(v.as_str()));
                    dict.set("DV", PdfObject::string(v.as_str()));
                }
                if let Some(ml) = max_len {
                    dict.set("MaxLen", PdfObject::Integer(*ml as i64));
                }
            }

            FieldKind::Checkbox { checked, export_value } => {
                dict.set("FT", PdfObject::name("Btn"));
                dict.set("T",  PdfObject::string(field.name.as_str()));
                dict.set("Ff", PdfObject::Integer(ff));

                let val = if *checked { export_value.as_str() } else { "Off" };
                dict.set("V",  PdfObject::name(val));
                dict.set("DV", PdfObject::name("Off"));

                // Appearance states
                let mut ap_n = PdfDict::new();
                ap_n.set(export_value.as_str(), PdfObject::Null);
                ap_n.set("Off", PdfObject::Null);
                let mut ap = PdfDict::new();
                ap.set("N", PdfObject::Dictionary(ap_n));
                dict.set("AP", PdfObject::Dictionary(ap));
                dict.set("AS", PdfObject::name(val));
            }

            FieldKind::Radio { group, export_value, selected } => {
                dict.set("FT", PdfObject::name("Btn"));
                dict.set("T",  PdfObject::string(group.as_str()));
                let radio_flags = ff | (1 << 15) | (1 << 16); // Radio + NoToggleToOff
                dict.set("Ff", PdfObject::Integer(radio_flags));

                let val = if *selected { export_value.as_str() } else { "Off" };
                dict.set("V",  PdfObject::name(val));
                dict.set("AS", PdfObject::name(val));
            }

            FieldKind::Dropdown { options, selected, editable } => {
                dict.set("FT", PdfObject::name("Ch"));
                dict.set("T",  PdfObject::string(field.name.as_str()));
                dict.set("DA", PdfObject::string(da.as_str()));

                let mut ch_flags = ff | (1 << 17); // Combo
                if *editable { ch_flags |= 1 << 18; }
                dict.set("Ff", PdfObject::Integer(ch_flags));

                let opts: Vec<PdfObject> = options.iter()
                    .map(|o| PdfObject::string(o.as_str()))
                    .collect();
                dict.set("Opt", PdfObject::Array(opts));

                if let Some(ref sel) = selected {
                    dict.set("V", PdfObject::string(sel.as_str()));
                }
            }

            FieldKind::ListBox { options, selected, multi_select } => {
                dict.set("FT", PdfObject::name("Ch"));
                dict.set("T",  PdfObject::string(field.name.as_str()));
                dict.set("DA", PdfObject::string(da.as_str()));

                let mut lb_flags = ff;
                if *multi_select { lb_flags |= 1 << 21; }
                dict.set("Ff", PdfObject::Integer(lb_flags));

                let opts: Vec<PdfObject> = options.iter()
                    .map(|o| PdfObject::string(o.as_str()))
                    .collect();
                dict.set("Opt", PdfObject::Array(opts));

                if !selected.is_empty() {
                    if selected.len() == 1 {
                        dict.set("V", PdfObject::string(selected[0].as_str()));
                    } else {
                        let sel: Vec<PdfObject> = selected.iter()
                            .map(|s| PdfObject::string(s.as_str()))
                            .collect();
                        dict.set("V", PdfObject::Array(sel));
                    }
                }
            }

            FieldKind::Button { label } => {
                dict.set("FT", PdfObject::name("Btn"));
                dict.set("T",  PdfObject::string(field.name.as_str()));
                dict.set("Ff", PdfObject::Integer(ff | (1 << 16))); // Pushbutton
                dict.set("DA", PdfObject::string(da.as_str()));

                // MK (appearance characteristics) for button label
                let mut mk = PdfDict::new();
                mk.set("CA", PdfObject::string(label.as_str()));
                dict.set("MK", PdfObject::Dictionary(mk));
            }
        }

        // Border style
        if let Some(bc) = field.border_color {
            let mut bs = PdfDict::new();
            bs.set("Type", PdfObject::name("Border"));
            bs.set("W",    PdfObject::Real(field.border_width));
            bs.set("S",    PdfObject::name("S")); // Solid
            dict.set("BS", PdfObject::Dictionary(bs));

            let mut mk = match dict.0.get("MK").cloned() {
                Some(PdfObject::Dictionary(d)) => d,
                _ => PdfDict::new(),
            };
            mk.set("BC", color_to_pdf_array(bc));
            if let Some(bg) = field.bg_color {
                mk.set("BG", color_to_pdf_array(bg));
            }
            dict.set("MK", PdfObject::Dictionary(mk));
        }

        writer.write_object(field_ref, &PdfObject::Dictionary(dict));
        field_ref
    }
}

fn color_to_pdf_array(c: Color) -> PdfObject {
    match c {
        Color::Rgb(r, g, b) => PdfObject::Array(vec![
            PdfObject::Real(r), PdfObject::Real(g), PdfObject::Real(b),
        ]),
        Color::Gray(g) => PdfObject::Array(vec![PdfObject::Real(g)]),
        Color::Cmyk(c, m, y, k) => PdfObject::Array(vec![
            PdfObject::Real(c), PdfObject::Real(m), PdfObject::Real(y), PdfObject::Real(k),
        ]),
    }
}