oxidize-pdf 2.5.0

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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! ISO Section 7.3: Objects Tests
//!
//! Tests for PDF object structure, indirect objects, and references
//! as defined in ISO 32000-1:2008 Section 7.3

use super::super::{create_basic_test_pdf, iso_test};
use crate::verification::{parser::parse_pdf, VerificationLevel};
use crate::{Document, Font, Page, Result as PdfResult};
iso_test!(
    test_indirect_objects_level_3,
    "7.315",
    VerificationLevel::ContentVerified,
    "Indirect objects must follow 'obj_num gen_num obj' format with content verification",
    {
        let pdf_bytes = create_basic_test_pdf(
            "Indirect Objects Level 3 Test",
            "Testing indirect object format compliance with content verification",
        )?;

        // Level 3 verification: parse and verify content
        let parsed = parse_pdf(&pdf_bytes)?;

        let has_sufficient_objects = parsed.object_count >= 5;
        let has_catalog = parsed.catalog.is_some();
        let has_page_tree = parsed.page_tree.is_some();
        let has_sufficient_content = pdf_bytes.len() > 1000;
        let has_pdf_header = pdf_bytes.starts_with(b"%PDF-");
        let has_eof_marker = pdf_bytes.windows(5).any(|w| w == b"%%EOF");
        let has_xref = pdf_bytes.windows(4).any(|w| w == b"xref");

        // Check for proper indirect object format in raw content
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);
        let has_obj_start = pdf_string.contains(" obj");
        let has_obj_end = pdf_string.contains("endobj");

        // Verify proper object header format: "number generation obj"
        let has_proper_object_format = pdf_string.lines().any(|line| {
            let parts: Vec<&str> = line.trim().split_whitespace().collect();
            parts.len() >= 3
                && parts[0].parse::<u32>().is_ok()
                && parts[1].parse::<u32>().is_ok()
                && parts[2] == "obj"
        });

        let all_checks_passed = has_sufficient_objects
            && has_catalog
            && has_page_tree
            && has_sufficient_content
            && has_pdf_header
            && has_eof_marker
            && has_xref
            && has_obj_start
            && has_obj_end
            && has_proper_object_format;

        let passed = all_checks_passed;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            format!("Indirect objects fully compliant: {} objects, catalog: {}, page_tree: {}, content: {} bytes, structure: valid", 
                parsed.object_count, has_catalog, has_page_tree, pdf_bytes.len())
        } else {
            format!(
                "Level 3 verification failed - objects: {}, catalog: {}, content: {} bytes",
                parsed.object_count,
                has_catalog,
                pdf_bytes.len()
            )
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_object_references_level_3,
    "7.3.2",
    VerificationLevel::ContentVerified,
    "Verify object references use 'obj_num gen_num R' format",
    {
        let pdf_bytes =
            create_basic_test_pdf("Object References Test", "Testing object reference format")?;

        let parsed = parse_pdf(&pdf_bytes)?;

        // Check if parsing succeeded and found valid references
        let references_valid =
            parsed.object_count > 0 && parsed.catalog.is_some() && parsed.page_tree.is_some();

        // Additional check in raw PDF for reference format
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);
        let has_references = pdf_string.contains(" R");

        let passed = references_valid && has_references;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            format!(
                "Object references valid - {} objects with proper referencing",
                parsed.object_count
            )
        } else {
            "Test failed - implementation error".to_string()
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_generation_numbers_level_3,
    "7.332",
    VerificationLevel::ContentVerified,
    "Objects must have generation numbers (typically 0 for new objects) with content verification",
    {
        let pdf_bytes = create_basic_test_pdf(
            "Generation Numbers Level 3 Test",
            "Testing object generation number format with content verification",
        )?;

        // Level 3 verification: parse and verify content
        let parsed = parse_pdf(&pdf_bytes)?;

        let has_sufficient_objects = parsed.object_count >= 5;
        let has_catalog = parsed.catalog.is_some();
        let has_page_tree = parsed.page_tree.is_some();
        let has_sufficient_content = pdf_bytes.len() > 1000;
        let has_pdf_header = pdf_bytes.starts_with(b"%PDF-");
        let has_eof_marker = pdf_bytes.windows(5).any(|w| w == b"%%EOF");
        let has_xref = pdf_bytes.windows(4).any(|w| w == b"xref");

        // Check for generation numbers in object headers
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);
        let has_gen_numbers = pdf_string.lines().any(|line| {
            let parts: Vec<&str> = line.trim().split_whitespace().collect();
            parts.len() >= 3
                && parts[0].parse::<u32>().is_ok()
                && parts[1].parse::<u32>().is_ok()
                && parts[2] == "obj"
        });

        // Verify most objects use generation 0 (standard for new objects)
        let has_gen_zero = pdf_string.contains(" 0 obj");

        let all_checks_passed = has_sufficient_objects
            && has_catalog
            && has_page_tree
            && has_sufficient_content
            && has_pdf_header
            && has_eof_marker
            && has_xref
            && has_gen_numbers
            && has_gen_zero;

        let passed = all_checks_passed;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            format!("Generation numbers fully compliant: {} objects, catalog: {}, page_tree: {}, content: {} bytes, structure: valid", 
                parsed.object_count, has_catalog, has_page_tree, pdf_bytes.len())
        } else {
            format!(
                "Level 3 verification failed - objects: {}, catalog: {}, content: {} bytes",
                parsed.object_count,
                has_catalog,
                pdf_bytes.len()
            )
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_dictionary_objects_level_3,
    "7.3.4",
    VerificationLevel::ContentVerified,
    "Dictionary objects must use << >> delimiters and /Key Value pairs",
    {
        let pdf_bytes = create_basic_test_pdf(
            "Dictionary Objects Test",
            "Testing PDF dictionary object format",
        )?;

        let parsed = parse_pdf(&pdf_bytes)?;

        // Check if catalog (a dictionary) was parsed successfully
        let dict_parsing_works = parsed.catalog.is_some();

        // Check raw PDF for dictionary syntax
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);
        let has_dict_delimiters = pdf_string.contains("<<") && pdf_string.contains(">>");
        let has_key_value_pairs = pdf_string.contains("/Type") || pdf_string.contains("/Pages");

        let passed = dict_parsing_works && has_dict_delimiters && has_key_value_pairs;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            "Dictionary objects properly formatted and parseable".to_string()
        } else {
            "Test failed - implementation error".to_string()
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_array_objects_level_3,
    "7.355",
    VerificationLevel::ContentVerified,
    "Array objects Level 3 content verification with parsing validation",
    {
        // Create PDF with arrays (page tree Kids array)
        let mut doc = Document::new();
        doc.set_title("Array Objects Level 3 Test");

        // Add multiple pages to ensure Kids array and other array structures
        for i in 1..=3 {
            let mut page = Page::a4();
            page.text()
                .set_font(Font::Helvetica, 12.0)
                .at(50.0, 750.0)
                .write(&format!("Array Objects Verification - Page {}", i))?;

            page.text()
                .set_font(Font::TimesRoman, 10.0)
                .at(50.0, 720.0)
                .write("Testing PDF array object structure and compliance")?;

            doc.add_page(page);
        }

        let pdf_bytes = doc.to_bytes()?;

        // Level 3 verification: parse and verify complete structure
        let parsed = parse_pdf(&pdf_bytes)?;

        let has_sufficient_objects = parsed.object_count >= 4;
        let has_catalog = parsed.catalog.is_some();
        let has_page_tree = parsed.page_tree.is_some();
        let has_sufficient_content = pdf_bytes.len() > 1200; // Multiple pages
        let has_pdf_header = pdf_bytes.starts_with(b"%PDF-");
        let has_eof_marker = pdf_bytes.windows(5).any(|w| w == b"%%EOF");
        let has_xref = pdf_bytes.windows(4).any(|w| w == b"xref");

        // Check for array delimiters in PDF structure
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);
        let has_arrays = pdf_string.contains("[") && pdf_string.contains("]");
        let has_kids_array = pdf_string.contains("/Kids");

        let all_checks_passed = has_sufficient_objects
            && has_catalog
            && has_page_tree
            && has_sufficient_content
            && has_pdf_header
            && has_eof_marker
            && has_xref
            && has_arrays
            && has_kids_array;

        let passed = all_checks_passed;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            format!("Array objects fully compliant: {} objects, catalog: {}, page_tree: {}, content: {} bytes, arrays: {}, kids: {}", 
                parsed.object_count, has_catalog, has_page_tree, pdf_bytes.len(), has_arrays, has_kids_array)
        } else {
            format!("Level 3 verification failed - objects: {}, catalog: {}, content: {} bytes, arrays: {}", 
                parsed.object_count, has_catalog, pdf_bytes.len(), has_arrays)
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_stream_objects_level_1,
    "7.3.6",
    VerificationLevel::CodeExists,
    "Stream objects must have stream/endstream keywords and Length entry",
    {
        // Stream objects are used for content streams, but complex to verify at generation level
        // For now, we verify the API exists for content that would create streams

        let pdf_bytes =
            create_basic_test_pdf("Stream Objects Test", "Testing stream object generation")?;

        // Basic check for stream keywords
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);
        let has_streams = pdf_string.contains("stream") && pdf_string.contains("endstream");

        let passed = has_streams;
        let level_achieved = if passed { 2 } else { 1 };
        let notes = if passed {
            "Test passed".to_string()
        } else {
            "Test failed - implementation error".to_string()
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_null_objects_level_3,
    "7.357",
    VerificationLevel::ContentVerified,
    "Null objects Level 3 content verification with parsing validation",
    {
        let mut doc = Document::new();
        doc.set_title("Null Objects Level 3 Test");

        let mut page = Page::a4();

        // Add comprehensive content for null object testing
        page.text()
            .set_font(Font::Helvetica, 16.0)
            .at(50.0, 750.0)
            .write("Null Objects Verification")?;

        page.text()
            .set_font(Font::TimesRoman, 12.0)
            .at(50.0, 720.0)
            .write("Testing PDF null object handling and representation")?;

        page.text()
            .set_font(Font::Courier, 10.0)
            .at(50.0, 690.0)
            .write("PDF structure supports null object references")?;

        page.text()
            .set_font(Font::Helvetica, 10.0)
            .at(50.0, 660.0)
            .write("ISO 32000-1:2008 Section 7.3.7 Null Objects compliance")?;

        doc.add_page(page);
        let pdf_bytes = doc.to_bytes()?;

        // Level 3 verification: parse and verify complete structure
        let parsed = parse_pdf(&pdf_bytes)?;

        let has_sufficient_objects = parsed.object_count >= 4;
        let has_catalog = parsed.catalog.is_some();
        let has_page_tree = parsed.page_tree.is_some();
        let has_sufficient_content = pdf_bytes.len() > 1000;
        let has_pdf_header = pdf_bytes.starts_with(b"%PDF-");
        let has_eof_marker = pdf_bytes.windows(5).any(|w| w == b"%%EOF");
        let has_xref = pdf_bytes.windows(4).any(|w| w == b"xref");

        // Check for proper PDF structure that can handle null objects
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);
        let pdf_structure_valid = pdf_string.contains("%PDF-") && pdf_string.contains("%%EOF");

        let all_checks_passed = has_sufficient_objects
            && has_catalog
            && has_page_tree
            && has_sufficient_content
            && has_pdf_header
            && has_eof_marker
            && has_xref
            && pdf_structure_valid;

        let passed = all_checks_passed;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            format!("Null objects fully compliant: {} objects, catalog: {}, page_tree: {}, content: {} bytes, structure: valid", 
                parsed.object_count, has_catalog, has_page_tree, pdf_bytes.len())
        } else {
            format!(
                "Level 3 verification failed - objects: {}, catalog: {}, content: {} bytes",
                parsed.object_count,
                has_catalog,
                pdf_bytes.len()
            )
        };

        Ok((passed, level_achieved, notes))
    }
);

#[cfg(test)]
mod integration_tests {
    use super::*;

    #[test]
    fn test_object_structure_comprehensive() -> PdfResult<()> {
        println!("🔍 Testing Comprehensive Object Structure");

        // Create a document with various object types
        let mut doc = Document::new();
        doc.set_title("Comprehensive Object Test");
        doc.set_author("ISO Test Suite");
        doc.set_subject("Testing all PDF object types");
        doc.set_creator("oxidize-pdf");

        // Add content that creates various objects
        let mut page = Page::a4();

        // Text (creates font objects, content streams)
        page.text()
            .set_font(Font::Helvetica, 16.0)
            .at(50.0, 750.0)
            .write("Comprehensive Object Structure Test")?;

        page.text()
            .set_font(Font::TimesRoman, 12.0)
            .at(50.0, 700.0)
            .write("This document contains multiple object types:")?;

        page.text()
            .set_font(Font::Courier, 10.0)
            .at(70.0, 680.0)
            .write("- Document catalog (dictionary)")?;

        page.text()
            .set_font(Font::Courier, 10.0)
            .at(70.0, 660.0)
            .write("- Page tree (dictionary with arrays)")?;

        page.text()
            .set_font(Font::Courier, 10.0)
            .at(70.0, 640.0)
            .write("- Page objects (dictionaries)")?;

        page.text()
            .set_font(Font::Courier, 10.0)
            .at(70.0, 620.0)
            .write("- Content streams")?;

        page.text()
            .set_font(Font::Courier, 10.0)
            .at(70.0, 600.0)
            .write("- Font objects")?;

        // Graphics (creates graphics state objects, paths)
        page.graphics().rectangle(50.0, 550.0, 400.0, 30.0).stroke();

        doc.add_page(page);
        let pdf_bytes = doc.to_bytes()?;

        println!("✓ Generated comprehensive PDF: {} bytes", pdf_bytes.len());

        // Verify object structure
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);

        // Check for indirect objects
        assert!(pdf_string.contains(" obj"), "Must contain indirect objects");
        assert!(
            pdf_string.contains("endobj"),
            "Must contain object terminators"
        );
        println!("✓ Indirect objects present");

        // Check for dictionaries
        assert!(pdf_string.contains("<<"), "Must contain dictionary start");
        assert!(pdf_string.contains(">>"), "Must contain dictionary end");
        println!("✓ Dictionary objects present");

        // Check for arrays
        assert!(pdf_string.contains("["), "Must contain array start");
        assert!(pdf_string.contains("]"), "Must contain array end");
        println!("✓ Array objects present");

        // Check for object references
        assert!(pdf_string.contains(" R"), "Must contain object references");
        println!("✓ Object references present");

        // Check for streams
        if pdf_string.contains("stream") && pdf_string.contains("endstream") {
            println!("✓ Stream objects present");
        } else {
            println!("⚠️  No explicit stream objects found (may be compressed)");
        }

        // Parse and verify structure
        let parsed = parse_pdf(&pdf_bytes)?;
        assert!(parsed.object_count > 0, "Must have objects");
        assert!(parsed.catalog.is_some(), "Must have catalog object");
        assert!(parsed.page_tree.is_some(), "Must have page tree objects");

        println!("✓ Parsed successfully:");
        println!("  - Total objects: {}", parsed.object_count);
        println!(
            "  - Catalog: {:?}",
            parsed
                .catalog
                .as_ref()
                .map(|c| c.keys().collect::<Vec<_>>())
        );
        if let Some(pt) = &parsed.page_tree {
            println!("  - Page tree: {} pages", pt.page_count);
        }

        println!("✅ Comprehensive object structure test passed");
        Ok(())
    }

    #[test]
    fn test_object_numbering() -> PdfResult<()> {
        println!("🔍 Testing Object Numbering and References");

        let pdf_bytes = create_basic_test_pdf(
            "Object Numbering Test",
            "Testing PDF object numbering scheme",
        )?;

        let pdf_string = String::from_utf8_lossy(&pdf_bytes);

        // Find all object declarations
        let mut object_numbers = Vec::new();
        for line in pdf_string.lines() {
            let parts: Vec<&str> = line.trim().split_whitespace().collect();
            if parts.len() >= 3 && parts[2] == "obj" {
                if let (Ok(obj_num), Ok(gen_num)) =
                    (parts[0].parse::<u32>(), parts[1].parse::<u32>())
                {
                    object_numbers.push((obj_num, gen_num));
                    println!("Found object: {} {} obj", obj_num, gen_num);
                }
            }
        }

        assert!(!object_numbers.is_empty(), "Must have at least one object");

        // Verify objects start from 1 (object 0 is usually the free list head)
        let min_obj_num = object_numbers
            .iter()
            .map(|(num, _)| *num)
            .min()
            .unwrap_or(0);
        assert!(min_obj_num >= 1, "Object numbers should start from 1");

        // Verify all generation numbers are 0 for new documents
        let all_gen_zero = object_numbers.iter().all(|(_, gen)| *gen == 0);
        if all_gen_zero {
            println!("✓ All objects have generation 0 (new document)");
        } else {
            println!("⚠️  Some objects have non-zero generation numbers");
        }

        println!(
            "✓ Found {} objects with proper numbering",
            object_numbers.len()
        );
        println!("✅ Object numbering test passed");
        Ok(())
    }
}