Debian Codegraph @

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

raw
1 use lopdf::{Document, Object};
2 use std::env;
3
4 #[cfg(feature = "async")]
5 use tokio::runtime::Builder;
6
7 #[cfg(not(feature = "async"))]
8 fn load_document(path: &str) -> Result<Document, lopdf::Error> {
9 Document::load(path)
10 }
11
12 #[cfg(feature = "async")]
13 fn load_document(path: &str) -> Result<Document, lopdf::Error> {
14 Builder::new_current_thread()
15 .enable_all()
16 .build()
17 .unwrap()
18 .block_on(async move {
19 Document::load(path).await
20 })
21 }
22
23 fn main() {
24 let args: Vec<String> = env::args().collect();
25 if args.len() < 2 {
26 eprintln!("Usage: {} <pdf_file>", args[0]);
27 std::process::exit(1);
28 }
29
30 let pdf_path = &args[1];
31 println!("Analyzing page contents in: {}", pdf_path);
32 println!("{}", "=".repeat(80));
33
34 match load_document(pdf_path) {
35 Ok(doc) => {
36 analyze_pages(&doc);
37 }
38 Err(e) => {
39 eprintln!("Failed to load PDF: {}", e);
40 std::process::exit(1);
41 }
42 }
43 }
44
45 fn analyze_pages(doc: &Document) {
46 let pages = doc.get_pages();
47 println!("Total pages: {}", pages.len());
48
49 // Check if we have object streams
50 let mut obj_streams = Vec::new();
51 for (id, obj) in &doc.objects {
52 if let Object::Stream(stream) = obj {
53 if let Ok(type_obj) = stream.dict.get(b"Type") {
54 if let Ok(type_name) = type_obj.as_name() {
55 if type_name == b"ObjStm" {
56 obj_streams.push(id.0);
57 }
58 }
59 }
60 }
61 }
62
63 if !obj_streams.is_empty() {
64 println!("\nObject streams found: {:?}", obj_streams);
65 }
66
67 println!("\nAnalyzing pages:");
68 for (page_num, &page_id) in pages.iter() {
69 println!("\n{}", "-".repeat(60));
70 println!("Page {}: Object {} 0 R", page_num, page_id.0);
71
72 match doc.get_object(page_id) {
73 Ok(Object::Dictionary(page_dict)) => {
74 // Check page type
75 if let Ok(type_obj) = page_dict.get(b"Type") {
76 println!(" Type: {:?}", type_obj);
77 }
78
79 // Check Contents
80 match page_dict.get(b"Contents") {
81 Ok(Object::Reference(content_id)) => {
82 println!(" Contents: {} {} R", content_id.0, content_id.1);
83 check_content_stream(&doc, *content_id);
84 }
85 Ok(Object::Array(contents)) => {
86 println!(" Contents array with {} elements:", contents.len());
87 for (i, content_ref) in contents.iter().enumerate() {
88 if let Object::Reference(content_id) = content_ref {
89 println!(" [{}] {} {} R", i, content_id.0, content_id.1);
90 check_content_stream(&doc, *content_id);
91 }
92 }
93 }
94 Ok(other) => {
95 println!(" Contents: Unexpected type: {:?}", other);
96 }
97 Err(e) => {
98 println!(" Contents: ERROR - {}", e);
99 }
100 }
101
102 // Check Resources
103 match page_dict.get(b"Resources") {
104 Ok(Object::Reference(res_id)) => {
105 println!(" Resources: {} {} R", res_id.0, res_id.1);
106 check_object_location(&doc, *res_id);
107 }
108 Ok(Object::Dictionary(_)) => {
109 println!(" Resources: Inline dictionary");
110 }
111 Ok(other) => {
112 println!(" Resources: Unexpected type: {:?}", other);
113 }
114 Err(e) => {
115 println!(" Resources: ERROR - {}", e);
116 }
117 }
118 }
119 Ok(other) => {
120 println!(" ERROR: Page is not a dictionary! Type: {:?}", other);
121 }
122 Err(e) => {
123 println!(" ERROR: Cannot get page object: {}", e);
124 check_object_location(&doc, page_id);
125 }
126 }
127 }
128 }
129
130 fn check_content_stream(doc: &Document, content_id: (u32, u16)) {
131 match doc.get_object(content_id) {
132 Ok(Object::Stream(stream)) => {
133 println!(" ✓ Stream exists");
134 println!(" Length: {}", stream.content.len());
135 if let Ok(filter) = stream.dict.get(b"Filter") {
136 println!(" Filter: {:?}", filter);
137 }
138
139 // Try to get decompressed content
140 match stream.decompressed_content() {
141 Ok(content) => {
142 let preview = String::from_utf8_lossy(&content[..content.len().min(100)]);
143 println!(" Preview: {:?}", preview);
144 }
145 Err(e) => {
146 println!(" ERROR decompressing: {}", e);
147 }
148 }
149 }
150 Ok(other) => {
151 println!(" ERROR: Content is not a stream! Type: {:?}", other);
152 }
153 Err(e) => {
154 println!(" ERROR: Cannot get content stream: {}", e);
155 check_object_location(doc, content_id);
156 }
157 }
158 }
159
160 fn check_object_location(doc: &Document, obj_id: (u32, u16)) {
161 // Check if object exists
162 if doc.objects.contains_key(&obj_id) {
163 println!(" Note: Object exists in document");
164 } else {
165 println!(" WARNING: Object {} {} R not found in document!", obj_id.0, obj_id.1);
166
167 // Check xref
168 if let Some(xref_entry) = doc.reference_table.get(obj_id.0) {
169 println!(" Xref entry: {:?}", xref_entry);
170 } else {
171 println!(" No xref entry found!");
172 }
173 }
174 }