-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathrotate.rs
63 lines (55 loc) · 2.52 KB
/
rotate.rs
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
use lopdf::Document;
use std::str::FromStr;
#[cfg(not(feature = "async"))]
fn main() {
// Collect command line arguments: input_file angle output_file
let args: Vec<String> = std::env::args().collect();
assert!(args.len() == 4, "Not enough arguments: input_file angle output_file");
let input_file = &args[1];
let angle = i64::from_str(&args[2]).expect("error in parsing angle argument");
assert!(angle % 90 == 0, "angle must be a multiple of 90");
let output_file = &args[3];
let mut doc = Document::load(input_file).unwrap();
// Note: this example sets Rotate on each page individually for flexibility,
// but you can also set it on any node in the page tree and child pages will
// inherit the value.
for (_, page_id) in doc.get_pages() {
let page_dict = doc
.get_object_mut(page_id)
.and_then(|obj| obj.as_dict_mut())
.expect("Missing page!");
// Get the current rotation if any; the default is 0
let current_rotation = page_dict.get(b"Rotate").and_then(|obj| obj.as_i64()).unwrap_or(0);
// Add the angle and update
page_dict.set("Rotate", (current_rotation + angle) % 360);
}
// Store file in current working directory.
doc.save(output_file).unwrap();
}
#[cfg(feature = "async")]
#[tokio::main]
async fn main() {
// Collect command line arguments: input_file angle output_file
let args: Vec<String> = std::env::args().collect();
assert!(args.len() == 4, "Not enough arguments: input_file angle output_file");
let input_file = &args[1];
let angle = i64::from_str(&args[2]).expect("error in parsing angle argument");
assert!(angle % 90 == 0, "angle must be a multiple of 90");
let output_file = &args[3];
let mut doc = Document::load(input_file).await.unwrap();
// Note: this example sets Rotate on each page individually for flexibility,
// but you can also set it on any node in the page tree and child pages will
// inherit the value.
for (_, page_id) in doc.get_pages() {
let page_dict = doc
.get_object_mut(page_id)
.and_then(|obj| obj.as_dict_mut())
.expect("Missing page!");
// Get the current rotation if any; the default is 0
let current_rotation = page_dict.get(b"Rotate").and_then(|obj| obj.as_i64()).unwrap_or(0);
// Add the angle and update
page_dict.set("Rotate", (current_rotation + angle) % 360);
}
// Store file in current working directory.
doc.save(output_file).unwrap();
}