Skip to main content

write/
write.rs

1//! Write a small PDF 2.0 file and read it back.
2//!
3//! Run with `cargo run --example write`; it writes `hello.pdf` next to the
4//! working directory.
5
6use pdf2::{Document, Rect};
7
8fn main() -> Result<(), pdf2::Error> {
9    let mut document = Document::new();
10    document.add_page(
11        Rect::new(0.0, 0.0, 612.0, 792.0),
12        "BT /F1 24 Tf 72 700 Td (Hello from pdf2) Tj ET",
13    );
14    let bytes = document.save()?;
15    std::fs::write("hello.pdf", &bytes)?;
16
17    let reopened = Document::open(bytes)?;
18    let page = reopened.page(0)?;
19    println!(
20        "hello.pdf: PDF {}, {} page(s), {}",
21        reopened.version(),
22        reopened.page_count()?,
23        reopened.extract_text(&page)?
24    );
25    Ok(())
26}