File: print_annotations.rs

package info (click to toggle)
rust-lopdf 0.34.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,620 kB
  • sloc: makefile: 2
file content (88 lines) | stat: -rw-r--r-- 2,657 bytes parent folder | download
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
// Display a summary of the annotations in a PDF file to the terminal
//
//   Run with `cargo run --example print_annotations <pdf-file>`

use env_logger::Env;
use lopdf::{Document, Object};
use std::env;
use std::process;

fn logging() {
    env_logger::Builder::from_env(Env::default().default_filter_or("info")).init()
}

fn args() -> Vec<String> {
    let args: Vec<String> = env::args().collect();

    if args.len() != 2 {
        eprintln!("Usage: {} <pdf-file>", &args[0]);
        process::exit(1);
    }

    args
}

fn handle_pdf_page(doc: Document) -> u32 {
    let mut page_counter = 1;

    for page in doc.page_iter() {
        for a in doc.get_page_annotations(page).unwrap() {
            let subtype = a
                .get_deref(b"Subtype", &doc)
                .and_then(Object::as_name_str)
                .unwrap_or("");
            println!(
                "Page {}, {} annotation at {:?}",
                page_counter,
                subtype,
                a.get_deref(b"Rect", &doc).and_then(Object::as_array).unwrap()
            );
            if let Ok(Object::String(c, _)) = a.get_deref(b"Contents", &doc) {
                println!("  Contents: {:.60}", String::from_utf8_lossy(c).lines().next().unwrap());
            }
            if subtype.eq("Link") {
                if let Ok(ahref) = a.get_deref(b"A", &doc).and_then(Object::as_dict) {
                    print!(
                        "  {} -> ",
                        ahref.get_deref(b"S", &doc).and_then(Object::as_name_str).unwrap()
                    );
                    if let Ok(d) = ahref.get_deref(b"D", &doc).and_then(Object::as_array) {
                        println!("{:?}", d);
                    } else if let Ok(Object::String(u, _)) = ahref.get_deref(b"URI", &doc) {
                        println!("{}", String::from_utf8_lossy(u));
                    } else if let Ok(n) = ahref.get_deref(b"N", &doc).and_then(Object::as_name_str) {
                        println!("{}", n);
                    }
                }
            }
        }
        page_counter += 1;
    }

    page_counter
}

#[cfg(not(feature = "async"))]
fn main() {
    logging();

    let args: Vec<String> = args();

    match Document::load(&args[1]) {
        Ok(doc) => _ = handle_pdf_page(doc),
        Err(e) => eprintln!("Error opening {:?}: {:?}", &args[1], e),
    }
}

#[cfg(feature = "async")]
#[tokio::main]
async fn main() {
    logging();

    let args: Vec<String> = args();

    match Document::load(&args[1]).await {
        Ok(doc) => _ = handle_pdf_page(doc),
        Err(e) => eprintln!("Error opening {:?}: {:?}", &args[1], e),
    }
}