Debian Codegraph @

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

raw
1 use lopdf::{Document, Object, ObjectStream, dictionary};
2
3 #[cfg(feature = "async")]
4 use tokio::runtime::Builder;
5
6 #[cfg(not(feature = "async"))]
7 fn load_document(path: &str) -> Document {
8 Document::load(path).unwrap()
9 }
10
11 #[cfg(feature = "async")]
12 fn load_document(path: &str) -> Document {
13 Builder::new_current_thread()
14 .enable_all()
15 .build()
16 .unwrap()
17 .block_on(async move {
18 Document::load(path).await.unwrap()
19 })
20 }
21
22 fn main() {
23 println!("Testing direct object stream creation and saving...\n");
24
25 // Create a simple document
26 let mut doc = Document::with_version("1.5");
27
28 // Create an object stream manually
29 let mut obj_stream = ObjectStream::builder()
30 .max_objects(10)
31 .compression_level(6)
32 .build();
33
34 // Add some objects
35 obj_stream.add_object((1, 0), Object::Integer(42)).unwrap();
36 obj_stream.add_object((2, 0), Object::String(b"Test".to_vec(), lopdf::StringFormat::Literal)).unwrap();
37
38 println!("Object stream has {} objects", obj_stream.object_count());
39
40 // Convert to stream object
41 let stream_obj = obj_stream.to_stream_object().unwrap();
42 println!("Stream dict: {:?}", stream_obj.dict);
43 println!("Stream has Filter: {}", stream_obj.dict.get(b"Filter").is_ok());
44
45 // Add it to the document
46 let stream_id = doc.add_object(stream_obj);
47 println!("Added object stream as {} 0 R", stream_id.0);
48
49 // Add a simple catalog
50 let pages_id = doc.add_object(dictionary! {
51 "Type" => "Pages",
52 "Kids" => vec![],
53 "Count" => 0
54 });
55
56 let catalog_id = doc.add_object(dictionary! {
57 "Type" => "Catalog",
58 "Pages" => pages_id
59 });
60
61 doc.trailer.set("Root", catalog_id);
62
63 // Save the document
64 println!("\nSaving document...");
65 doc.save("test_direct_objstream.pdf").unwrap();
66
67 println!("Saved to test_direct_objstream.pdf");
68
69 // Load it back and check
70 println!("\nLoading back...");
71 let loaded = load_document("test_direct_objstream.pdf");
72
73 for (id, obj) in &loaded.objects {
74 if let Object::Stream(stream) = obj {
75 if let Ok(type_obj) = stream.dict.get(b"Type") {
76 if let Ok(type_name) = type_obj.as_name() {
77 if type_name == b"ObjStm" {
78 println!("Found object stream {} 0 R", id.0);
79 println!(" Dict: {:?}", stream.dict);
80 println!(" Has Filter: {}", stream.dict.get(b"Filter").is_ok());
81 if let Ok(filter) = stream.dict.get(b"Filter") {
82 println!(" Filter value: {:?}", filter);
83 }
84 }
85 }
86 }
87 }
88 }
89 }