Debian Codegraph @

debian/missing-sources/lopdf/examples/debug_object_stream.rs

raw
1 use lopdf::{Object, ObjectStream};
2
3 fn main() {
4 println!("Testing Object Stream creation...\n");
5
6 // Create a simple object stream with a few objects
7 let mut obj_stream = ObjectStream::builder()
8 .max_objects(10)
9 .compression_level(6)
10 .build();
11
12 // Add some simple objects
13 obj_stream.add_object((1, 0), Object::Integer(42)).unwrap();
14 obj_stream.add_object((2, 0), Object::String(b"Hello".to_vec(), lopdf::StringFormat::Literal)).unwrap();
15 obj_stream.add_object((3, 0), Object::Boolean(true)).unwrap();
16
17 println!("Added {} objects to stream", obj_stream.object_count());
18
19 // Build the stream content
20 match obj_stream.build_stream_content() {
21 Ok(content) => {
22 println!("\nStream content built successfully:");
23 println!("Content size: {} bytes", content.len());
24
25 // Show the content
26 if let Ok(content_str) = std::str::from_utf8(&content) {
27 println!("\nContent (first 200 chars):");
28 println!("{}", &content_str[..content_str.len().min(200)]);
29 } else {
30 println!("\nRaw content (first 100 bytes): {:?}", &content[..content.len().min(100)]);
31 }
32
33 // Try to create the stream object
34 match obj_stream.to_stream_object() {
35 Ok(stream) => {
36 println!("\n\nStream object created successfully!");
37 println!("Dictionary: {:?}", stream.dict);
38 println!("Compressed: {}", stream.dict.get(b"Filter").is_ok());
39 println!("Content length: {}", stream.content.len());
40
41 // Try to decompress and verify
42 match stream.decompressed_content() {
43 Ok(decompressed) => {
44 println!("\nDecompressed successfully!");
45 println!("Decompressed size: {} bytes", decompressed.len());
46 if let Ok(decomp_str) = std::str::from_utf8(&decompressed) {
47 println!("Decompressed content:\n{}", decomp_str);
48 }
49 }
50 Err(e) => {
51 println!("\nERROR: Failed to decompress: {}", e);
52 }
53 }
54 }
55 Err(e) => {
56 println!("\nERROR: Failed to create stream object: {}", e);
57 }
58 }
59 }
60 Err(e) => {
61 println!("ERROR: Failed to build stream content: {}", e);
62 }
63 }
64 }