Debian Codegraph @

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

raw
1 use lopdf::{Document, Object, ObjectStream};
2
3 #[cfg(feature = "async")]
4 use tokio::runtime::Builder;
5
6 #[cfg(not(feature = "async"))]
7 fn load_document(path: &str) -> Result<Document, lopdf::Error> {
8 Document::load(path)
9 }
10
11 #[cfg(feature = "async")]
12 fn load_document(path: &str) -> Result<Document, lopdf::Error> {
13 Builder::new_current_thread()
14 .enable_all()
15 .build()
16 .unwrap()
17 .block_on(async move {
18 Document::load(path).await
19 })
20 }
21
22 fn main() {
23 let pdf_path = "/Users/nicolasdao/Downloads/pdfs/RFQ - SDS WebApp.docx_compressed.pdf";
24 println!("Inspecting object stream in: {}", pdf_path);
25
26 match load_document(pdf_path) {
27 Ok(doc) => {
28 // Find object stream 511
29 if let Ok(Object::Stream(stream)) = doc.get_object((511, 0)) {
30 println!("\nObject Stream 511 0 R found!");
31 println!("Dictionary: {:?}", stream.dict);
32
33 // Try to parse it
34 let mut stream_clone = stream.clone();
35 match ObjectStream::new(&mut stream_clone) {
36 Ok(obj_stream) => {
37 println!("\nSuccessfully parsed object stream!");
38 println!("Contains {} objects", obj_stream.objects.len());
39
40 // List first 20 objects
41 println!("\nFirst 20 objects in stream:");
42 let mut count = 0;
43 for ((id, generation), obj) in &obj_stream.objects {
44 if count >= 20 { break; }
45 println!(" {} {} R: {:?}", id, generation, obj.type_name().unwrap_or(b"Unknown"));
46
47 // If it's a dictionary, show some keys
48 if let Object::Dictionary(dict) = obj {
49 let key_count = dict.len();
50 println!(" Dictionary with {} keys", key_count);
51 }
52 count += 1;
53 }
54
55 // Check if any page-related objects are in there
56 println!("\nChecking for page-related objects:");
57 for ((id, generation), obj) in &obj_stream.objects {
58 if let Object::Dictionary(dict) = obj {
59 if let Ok(type_obj) = dict.get(b"Type") {
60 if let Ok(type_name) = type_obj.as_name() {
61 if type_name == b"Page" {
62 println!(" WARNING: Page object {} {} R is in object stream!", id, generation);
63 }
64 }
65 }
66
67 // Check for font descriptors
68 if dict.has(b"FontDescriptor") || dict.has(b"BaseFont") {
69 println!(" Font-related object {} {} R", id, generation);
70 }
71 }
72 }
73 }
74 Err(e) => {
75 println!("ERROR parsing object stream: {}", e);
76 }
77 }
78 } else {
79 println!("Object stream 511 not found or not a stream!");
80 }
81
82 // Also check what critical objects might be compressed
83 println!("\n\nChecking critical object locations:");
84
85 // Check catalog
86 if let Ok(root_ref) = doc.trailer.get(b"Root") {
87 if let Object::Reference(root_id) = root_ref {
88 check_object_status(&doc, *root_id, "Catalog (Root)");
89 }
90 }
91
92 // Check pages tree
93 if let Ok(catalog) = doc.catalog() {
94 if let Ok(pages_ref) = catalog.get(b"Pages") {
95 if let Object::Reference(pages_id) = pages_ref {
96 check_object_status(&doc, *pages_id, "Pages tree root");
97 }
98 }
99 }
100
101 // Check specific page objects
102 let pages = doc.get_pages();
103 for (num, &id) in pages.iter().take(2) {
104 check_object_status(&doc, id, &format!("Page {}", num));
105 }
106 }
107 Err(e) => {
108 eprintln!("Failed to load PDF: {}", e);
109 }
110 }
111 }
112
113 fn check_object_status(doc: &Document, id: (u32, u16), name: &str) {
114 match doc.get_object(id) {
115 Ok(_) => {
116 // Check xref entry
117 if let Some(xref_entry) = doc.reference_table.get(id.0) {
118 match xref_entry {
119 lopdf::xref::XrefEntry::Normal { offset, generation: _ } => {
120 println!("{} ({} {} R): Normal entry at offset {}", name, id.0, id.1, offset);
121 }
122 lopdf::xref::XrefEntry::Compressed { container, index } => {
123 println!("{} ({} {} R): COMPRESSED in stream {} at index {}", name, id.0, id.1, container, index);
124 }
125 _ => {
126 println!("{} ({} {} R): Other xref type", name, id.0, id.1);
127 }
128 }
129 } else {
130 println!("{} ({} {} R): No xref entry!", name, id.0, id.1);
131 }
132 }
133 Err(e) => {
134 println!("{} ({} {} R): ERROR - {}", name, id.0, id.1, e);
135 }
136 }
137 }