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