Debian Codegraph @

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

raw
1 use lopdf::Document;
2
3 #[cfg(not(feature = "async"))]
4 fn main() {
5 // Test loading the encrypted.pdf file
6 println!("Loading encrypted.pdf...");
7 let doc = match Document::load("assets/encrypted.pdf") {
8 Ok(d) => d,
9 Err(e) => {
10 eprintln!("Failed to load encrypted.pdf: {:?}", e);
11 return;
12 }
13 };
14
15 println!("Document loaded successfully!");
16 println!("Is encrypted: {}", doc.is_encrypted());
17
18 // Try to get pages
19 let pages = doc.get_pages();
20 println!("Number of pages: {}", pages.len());
21
22 // Try to extract text
23 if pages.len() > 0 {
24 let page_numbers: Vec<u32> = pages.keys().cloned().collect();
25 match doc.extract_text(&page_numbers) {
26 Ok(text) => {
27 println!("Text extraction successful!");
28 println!("Text preview (first 200 chars): {}",
29 text.chars().take(200).collect::<String>());
30 }
31 Err(e) => {
32 println!("Text extraction failed: {:?}", e);
33 }
34 }
35 }
36
37 // Check if we can access objects
38 println!("\nChecking object access:");
39 let max_check = 10;
40 for i in 1..=max_check {
41 if let Ok(_) = doc.get_object((i, 0)) {
42 println!(" Object ({}, 0) found", i);
43 }
44 }
45
46 // Check trailer
47 println!("\nTrailer entries:");
48 if let Ok(root) = doc.trailer.get(b"Root") {
49 println!(" Root: {:?}", root);
50 }
51 if let Ok(encrypt) = doc.trailer.get(b"Encrypt") {
52 println!(" Encrypt: {:?}", encrypt);
53 }
54 if let Ok(info) = doc.trailer.get(b"Info") {
55 println!(" Info: {:?}", info);
56 }
57 }
58
59 #[cfg(feature = "async")]
60 #[tokio::main]
61 async fn main() {
62 // Test loading the encrypted.pdf file
63 println!("Loading encrypted.pdf...");
64 let doc = match Document::load("assets/encrypted.pdf").await {
65 Ok(d) => d,
66 Err(e) => {
67 eprintln!("Failed to load encrypted.pdf: {:?}", e);
68 return;
69 }
70 };
71
72 println!("Document loaded successfully!");
73 println!("Is encrypted: {}", doc.is_encrypted());
74
75 // Try to get pages
76 let pages = doc.get_pages();
77 println!("Number of pages: {}", pages.len());
78
79 // Try to extract text
80 if pages.len() > 0 {
81 let page_numbers: Vec<u32> = pages.keys().cloned().collect();
82 match doc.extract_text(&page_numbers) {
83 Ok(text) => {
84 println!("Text extraction successful!");
85 println!("Text preview (first 200 chars): {}",
86 text.chars().take(200).collect::<String>());
87 }
88 Err(e) => {
89 println!("Text extraction failed: {:?}", e);
90 }
91 }
92 }
93
94 // Check if we can access objects
95 println!("\nChecking object access:");
96 let max_check = 10;
97 for i in 1..=max_check {
98 if let Ok(_obj) = doc.get_object((i, 0)) {
99 println!(" Object ({}, 0) found", i);
100 }
101 }
102
103 // Check trailer
104 println!("\nTrailer entries:");
105 if let Ok(root) = doc.trailer.get(b"Root") {
106 println!(" Root: {:?}", root);
107 }
108 if let Ok(encrypt) = doc.trailer.get(b"Encrypt") {
109 println!(" Encrypt: {:?}", encrypt);
110 }
111 if let Ok(info) = doc.trailer.get(b"Info") {
112 println!(" Info: {:?}", info);
113 }
114 }