pdf_oxide 0.3.22

The fastest Rust PDF library with text extraction: 0.8ms mean, 100% pass rate on 3,830 PDFs. 5× faster than pdf_extract, 17× faster than oxidize_pdf. Extract, create, and edit PDFs.
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
//! Integration tests for FDF and XFDF export functionality.
//!
//! Tests the export of form field data to FDF (Forms Data Format) and
//! XFDF (XML Forms Data Format) per ISO 32000-1:2008 Section 12.7.7.

use pdf_oxide::api::Pdf;
use pdf_oxide::editor::{DocumentEditor, EditableDocument};
use pdf_oxide::fdf::{FdfField, FdfValue, FdfWriter, XfdfWriter};
use pdf_oxide::geometry::Rect;
use pdf_oxide::writer::form_fields::{
    CheckboxWidget, ChoiceOption, ComboBoxWidget, TextFieldWidget,
};
use tempfile::tempdir;

// ============================================================================
// FdfWriter Unit Tests
// ============================================================================

#[test]
fn test_fdf_writer_basic() {
    let mut writer = FdfWriter::new();
    writer.add_field(FdfField::new("name", FdfValue::Text("John Doe".into())));
    writer.add_field(FdfField::new("email", FdfValue::Text("john@example.com".into())));

    let bytes = writer.to_bytes().unwrap();
    let content = String::from_utf8_lossy(&bytes);

    // Verify FDF structure
    assert!(content.contains("%FDF-1.2"));
    assert!(content.contains("/Fields"));
    assert!(content.contains("/T (name)"));
    assert!(content.contains("/V (John Doe)"));
    assert!(content.contains("/T (email)"));
    assert!(content.contains("/V (john@example.com)"));
    assert!(content.contains("%%EOF"));
}

#[test]
fn test_fdf_writer_with_file_spec() {
    let writer = FdfWriter::new().with_file_spec("original.pdf");
    let bytes = writer.to_bytes().unwrap();
    let content = String::from_utf8_lossy(&bytes);

    assert!(content.contains("/F (original.pdf)"));
}

#[test]
fn test_fdf_writer_boolean_values() {
    let mut writer = FdfWriter::new();
    writer.add_field(FdfField::new("agree", FdfValue::Boolean(true)));
    writer.add_field(FdfField::new("decline", FdfValue::Boolean(false)));

    let bytes = writer.to_bytes().unwrap();
    let content = String::from_utf8_lossy(&bytes);

    assert!(content.contains("/V /Yes"));
    assert!(content.contains("/V /Off"));
}

#[test]
fn test_fdf_writer_name_values() {
    let mut writer = FdfWriter::new();
    writer.add_field(FdfField::new("choice", FdfValue::Name("Option1".into())));

    let bytes = writer.to_bytes().unwrap();
    let content = String::from_utf8_lossy(&bytes);

    assert!(content.contains("/V /Option1"));
}

#[test]
fn test_fdf_writer_array_values() {
    let mut writer = FdfWriter::new();
    writer.add_field(FdfField::new(
        "multi",
        FdfValue::Array(vec!["Choice A".into(), "Choice B".into()]),
    ));

    let bytes = writer.to_bytes().unwrap();
    let content = String::from_utf8_lossy(&bytes);

    assert!(content.contains("/V [ (Choice A) (Choice B) ]"));
}

#[test]
fn test_fdf_writer_special_chars() {
    let mut writer = FdfWriter::new();
    writer.add_field(FdfField::new("note", FdfValue::Text("Hello (World)".into())));

    let bytes = writer.to_bytes().unwrap();
    let content = String::from_utf8_lossy(&bytes);

    // Parentheses should be escaped
    assert!(content.contains("/V (Hello \\(World\\))"));
}

#[test]
fn test_fdf_write_to_file() {
    let temp_dir = tempdir().unwrap();
    let output_path = temp_dir.path().join("test.fdf");

    let mut writer = FdfWriter::new();
    writer.add_field(FdfField::new("test", FdfValue::Text("value".into())));
    writer.write_to_file(&output_path).unwrap();

    // Verify file was created
    assert!(output_path.exists());

    // Verify content
    let content = String::from_utf8_lossy(&std::fs::read(&output_path).unwrap()).to_string();
    assert!(content.contains("%FDF-1.2"));
    assert!(content.contains("/T (test)"));
}

// ============================================================================
// XfdfWriter Unit Tests
// ============================================================================

#[test]
fn test_xfdf_writer_basic() {
    let mut writer = XfdfWriter::new();
    writer.add_field("name", "John Doe");
    writer.add_field("email", "john@example.com");

    let xml = writer.to_xml();

    assert!(xml.contains("<?xml version=\"1.0\""));
    assert!(xml.contains("<xfdf xmlns=\"http://ns.adobe.com/xfdf/\""));
    assert!(xml.contains("<fields>"));
    assert!(xml.contains("<field name=\"name\">"));
    assert!(xml.contains("<value>John Doe</value>"));
    assert!(xml.contains("<field name=\"email\">"));
    assert!(xml.contains("<value>john@example.com</value>"));
    assert!(xml.contains("</xfdf>"));
}

#[test]
fn test_xfdf_writer_with_file_spec() {
    let writer = XfdfWriter::new().with_file_spec("original.pdf");
    let xml = writer.to_xml();

    assert!(xml.contains("<f href=\"original.pdf\"/>"));
}

#[test]
fn test_xfdf_writer_xml_escaping() {
    let mut writer = XfdfWriter::new();
    writer.add_field("company", "Smith & Jones <Consulting>");

    let xml = writer.to_xml();

    assert!(xml.contains("<value>Smith &amp; Jones &lt;Consulting&gt;</value>"));
}

#[test]
fn test_xfdf_writer_boolean_values() {
    let mut writer = XfdfWriter::new();
    writer.add_fdf_field(FdfField::new("agree", FdfValue::Boolean(true)));
    writer.add_fdf_field(FdfField::new("decline", FdfValue::Boolean(false)));

    let xml = writer.to_xml();

    assert!(xml.contains("<field name=\"agree\">"));
    assert!(xml.contains("<value>Yes</value>"));
    assert!(xml.contains("<field name=\"decline\">"));
    assert!(xml.contains("<value>Off</value>"));
}

#[test]
fn test_xfdf_writer_hierarchical() {
    let mut writer = XfdfWriter::new();
    let parent = FdfField::new("address", FdfValue::None)
        .with_kid(FdfField::new("street", FdfValue::Text("123 Main St".into())))
        .with_kid(FdfField::new("city", FdfValue::Text("Anytown".into())));
    writer.add_fdf_field(parent);

    let xml = writer.to_xml();

    assert!(xml.contains("<field name=\"address\">"));
    assert!(xml.contains("<field name=\"street\">"));
    assert!(xml.contains("<value>123 Main St</value>"));
    assert!(xml.contains("<field name=\"city\">"));
    assert!(xml.contains("<value>Anytown</value>"));
}

#[test]
fn test_xfdf_write_to_file() {
    let temp_dir = tempdir().unwrap();
    let output_path = temp_dir.path().join("test.xfdf");

    let mut writer = XfdfWriter::new();
    writer.add_field("test", "value");
    writer.write_to_file(&output_path).unwrap();

    // Verify file was created
    assert!(output_path.exists());

    // Verify content
    let content = std::fs::read_to_string(&output_path).unwrap();
    assert!(content.contains("<?xml version=\"1.0\""));
    assert!(content.contains("<field name=\"test\">"));
}

// ============================================================================
// DocumentEditor Integration Tests
// ============================================================================

#[test]
fn test_export_fdf_from_editor() {
    let temp_dir = tempdir().unwrap();
    let pdf_path = temp_dir.path().join("form.pdf");
    let fdf_path = temp_dir.path().join("export.fdf");

    // Create a PDF with form fields
    let mut editor = DocumentEditor::open("tests/fixtures/simple.pdf").unwrap();
    editor
        .add_form_field(
            0,
            TextFieldWidget::new("username", Rect::new(100.0, 700.0, 200.0, 20.0))
                .with_value("test_user"),
        )
        .unwrap();
    editor
        .add_form_field(
            0,
            CheckboxWidget::new("agree", Rect::new(100.0, 660.0, 20.0, 20.0)).checked(),
        )
        .unwrap();
    editor.save(&pdf_path).unwrap();

    // Open and export FDF
    let mut editor = DocumentEditor::open(&pdf_path).unwrap();
    editor.export_form_data_fdf(&fdf_path).unwrap();

    // Verify FDF content
    let content = String::from_utf8_lossy(&std::fs::read(&fdf_path).unwrap()).to_string();
    assert!(content.contains("%FDF-1.2"));
    assert!(content.contains("/Fields"));
}

#[test]
fn test_export_xfdf_from_editor() {
    let temp_dir = tempdir().unwrap();
    let pdf_path = temp_dir.path().join("form.pdf");
    let xfdf_path = temp_dir.path().join("export.xfdf");

    // Create a PDF with form fields
    let mut editor = DocumentEditor::open("tests/fixtures/simple.pdf").unwrap();
    editor
        .add_form_field(
            0,
            TextFieldWidget::new("email", Rect::new(100.0, 700.0, 200.0, 20.0))
                .with_value("test@example.com"),
        )
        .unwrap();
    editor.save(&pdf_path).unwrap();

    // Open and export XFDF
    let mut editor = DocumentEditor::open(&pdf_path).unwrap();
    editor.export_form_data_xfdf(&xfdf_path).unwrap();

    // Verify XFDF content
    let content = std::fs::read_to_string(&xfdf_path).unwrap();
    assert!(content.contains("<?xml version=\"1.0\""));
    assert!(content.contains("<xfdf"));
    assert!(content.contains("<fields>"));
}

#[test]
fn test_export_from_pdf_without_forms() {
    let temp_dir = tempdir().unwrap();
    let fdf_path = temp_dir.path().join("empty.fdf");
    let xfdf_path = temp_dir.path().join("empty.xfdf");

    // Open a PDF without forms
    let mut editor = DocumentEditor::open("tests/fixtures/simple.pdf").unwrap();

    // Export should succeed but produce empty fields
    editor.export_form_data_fdf(&fdf_path).unwrap();
    editor.export_form_data_xfdf(&xfdf_path).unwrap();

    // Verify files exist
    assert!(fdf_path.exists());
    assert!(xfdf_path.exists());

    // FDF should have empty Fields array
    let fdf_content = String::from_utf8_lossy(&std::fs::read(&fdf_path).unwrap()).to_string();
    assert!(fdf_content.contains("%FDF-1.2"));
    assert!(fdf_content.contains("/Fields ["));

    // XFDF should have empty fields element
    let xfdf_content = std::fs::read_to_string(&xfdf_path).unwrap();
    assert!(xfdf_content.contains("<fields>"));
    assert!(xfdf_content.contains("</fields>"));
}

// ============================================================================
// High-Level Pdf API Tests
// ============================================================================

#[test]
fn test_pdf_api_export_fdf() {
    let temp_dir = tempdir().unwrap();
    let pdf_path = temp_dir.path().join("form.pdf");
    let fdf_path = temp_dir.path().join("export.fdf");

    // Create a PDF with form fields
    let mut editor = DocumentEditor::open("tests/fixtures/simple.pdf").unwrap();
    editor
        .add_form_field(
            0,
            TextFieldWidget::new("name", Rect::new(100.0, 700.0, 200.0, 20.0))
                .with_value("Test Name"),
        )
        .unwrap();
    editor.save(&pdf_path).unwrap();

    // Open and export via Pdf API
    let mut pdf = Pdf::open(&pdf_path).unwrap();
    pdf.export_form_data_fdf(&fdf_path).unwrap();

    assert!(fdf_path.exists());
}

#[test]
fn test_pdf_api_export_xfdf() {
    let temp_dir = tempdir().unwrap();
    let pdf_path = temp_dir.path().join("form.pdf");
    let xfdf_path = temp_dir.path().join("export.xfdf");

    // Create a PDF with form fields
    let mut editor = DocumentEditor::open("tests/fixtures/simple.pdf").unwrap();
    editor
        .add_form_field(
            0,
            ComboBoxWidget::new("country", Rect::new(100.0, 700.0, 150.0, 20.0))
                .with_choice_options(vec![
                    ChoiceOption::new("USA"),
                    ChoiceOption::new("Canada"),
                    ChoiceOption::new("UK"),
                ])
                .with_value("USA"),
        )
        .unwrap();
    editor.save(&pdf_path).unwrap();

    // Open and export via Pdf API
    let mut pdf = Pdf::open(&pdf_path).unwrap();
    pdf.export_form_data_xfdf(&xfdf_path).unwrap();

    assert!(xfdf_path.exists());
}

// ============================================================================
// Round-trip Tests
// ============================================================================

#[test]
fn test_fdf_round_trip_consistency() {
    let temp_dir = tempdir().unwrap();
    let pdf_path = temp_dir.path().join("form.pdf");
    let fdf_path1 = temp_dir.path().join("export1.fdf");
    let fdf_path2 = temp_dir.path().join("export2.fdf");

    // Create a PDF with multiple field types
    let mut editor = DocumentEditor::open("tests/fixtures/simple.pdf").unwrap();
    editor
        .add_form_field(
            0,
            TextFieldWidget::new("text_field", Rect::new(100.0, 700.0, 200.0, 20.0))
                .with_value("Hello World"),
        )
        .unwrap();
    editor
        .add_form_field(
            0,
            CheckboxWidget::new("checkbox", Rect::new(100.0, 660.0, 20.0, 20.0)).checked(),
        )
        .unwrap();
    editor.save(&pdf_path).unwrap();

    // Export twice and compare
    let mut editor1 = DocumentEditor::open(&pdf_path).unwrap();
    editor1.export_form_data_fdf(&fdf_path1).unwrap();

    let mut editor2 = DocumentEditor::open(&pdf_path).unwrap();
    editor2.export_form_data_fdf(&fdf_path2).unwrap();

    // Contents should be identical
    let content1 = String::from_utf8_lossy(&std::fs::read(&fdf_path1).unwrap()).to_string();
    let content2 = String::from_utf8_lossy(&std::fs::read(&fdf_path2).unwrap()).to_string();
    assert_eq!(content1, content2);
}

#[test]
fn test_xfdf_round_trip_consistency() {
    let temp_dir = tempdir().unwrap();
    let pdf_path = temp_dir.path().join("form.pdf");
    let xfdf_path1 = temp_dir.path().join("export1.xfdf");
    let xfdf_path2 = temp_dir.path().join("export2.xfdf");

    // Create a PDF with form fields
    let mut editor = DocumentEditor::open("tests/fixtures/simple.pdf").unwrap();
    editor
        .add_form_field(
            0,
            TextFieldWidget::new("field1", Rect::new(100.0, 700.0, 200.0, 20.0))
                .with_value("Value1"),
        )
        .unwrap();
    editor
        .add_form_field(
            0,
            TextFieldWidget::new("field2", Rect::new(100.0, 660.0, 200.0, 20.0))
                .with_value("Value2"),
        )
        .unwrap();
    editor.save(&pdf_path).unwrap();

    // Export twice and compare
    let mut editor1 = DocumentEditor::open(&pdf_path).unwrap();
    editor1.export_form_data_xfdf(&xfdf_path1).unwrap();

    let mut editor2 = DocumentEditor::open(&pdf_path).unwrap();
    editor2.export_form_data_xfdf(&xfdf_path2).unwrap();

    // Contents should be identical
    let content1 = std::fs::read_to_string(&xfdf_path1).unwrap();
    let content2 = std::fs::read_to_string(&xfdf_path2).unwrap();
    assert_eq!(content1, content2);
}