| 1 |
use lopdf::Document; |
| 2 |
use std::str::FromStr; |
| 3 |
|
| 4 |
#[cfg(not(feature = "async"))] |
| 5 |
fn main() { |
| 6 |
// Collect command line arguments: input_file angle output_file |
| 7 |
let args: Vec<String> = std::env::args().collect(); |
| 8 |
assert!(args.len() == 4, "Not enough arguments: input_file angle output_file"); |
| 9 |
let input_file = &args[1]; |
| 10 |
let angle = i64::from_str(&args[2]).expect("error in parsing angle argument"); |
| 11 |
assert!(angle % 90 == 0, "angle must be a multiple of 90"); |
| 12 |
let output_file = &args[3]; |
| 13 |
let mut doc = Document::load(input_file).unwrap(); |
| 14 |
|
| 15 |
// Note: this example sets Rotate on each page individually for flexibility, |
| 16 |
// but you can also set it on any node in the page tree and child pages will |
| 17 |
// inherit the value. |
| 18 |
for (_, page_id) in doc.get_pages() { |
| 19 |
let page_dict = doc |
| 20 |
.get_object_mut(page_id) |
| 21 |
.and_then(|obj| obj.as_dict_mut()) |
| 22 |
.expect("Missing page!"); |
| 23 |
|
| 24 |
// Get the current rotation if any; the default is 0 |
| 25 |
let current_rotation = page_dict.get(b"Rotate").and_then(|obj| obj.as_i64()).unwrap_or(0); |
| 26 |
|
| 27 |
// Add the angle and update |
| 28 |
page_dict.set("Rotate", (current_rotation + angle) % 360); |
| 29 |
} |
| 30 |
// Store file in current working directory. |
| 31 |
doc.save(output_file).unwrap(); |
| 32 |
} |
| 33 |
|
| 34 |
#[cfg(feature = "async")] |
| 35 |
#[tokio::main] |
| 36 |
async fn main() { |
| 37 |
// Collect command line arguments: input_file angle output_file |
| 38 |
let args: Vec<String> = std::env::args().collect(); |
| 39 |
assert!(args.len() == 4, "Not enough arguments: input_file angle output_file"); |
| 40 |
let input_file = &args[1]; |
| 41 |
let angle = i64::from_str(&args[2]).expect("error in parsing angle argument"); |
| 42 |
assert!(angle % 90 == 0, "angle must be a multiple of 90"); |
| 43 |
let output_file = &args[3]; |
| 44 |
let mut doc = Document::load(input_file).await.unwrap(); |
| 45 |
|
| 46 |
// Note: this example sets Rotate on each page individually for flexibility, |
| 47 |
// but you can also set it on any node in the page tree and child pages will |
| 48 |
// inherit the value. |
| 49 |
for (_, page_id) in doc.get_pages() { |
| 50 |
let page_dict = doc |
| 51 |
.get_object_mut(page_id) |
| 52 |
.and_then(|obj| obj.as_dict_mut()) |
| 53 |
.expect("Missing page!"); |
| 54 |
|
| 55 |
// Get the current rotation if any; the default is 0 |
| 56 |
let current_rotation = page_dict.get(b"Rotate").and_then(|obj| obj.as_i64()).unwrap_or(0); |
| 57 |
|
| 58 |
// Add the angle and update |
| 59 |
page_dict.set("Rotate", (current_rotation + angle) % 360); |
| 60 |
} |
| 61 |
// Store file in current working directory. |
| 62 |
doc.save(output_file).unwrap(); |
| 63 |
} |