hayro_syntax/
pdf.rs

1//! The starting point for reading PDF files.
2
3use crate::PdfData;
4use crate::document::page::{Page, Pages};
5use crate::object::Object;
6use crate::xref::{XRef, fallback, root_xref};
7
8/// A PDF file.
9pub struct Pdf {
10    xref: XRef,
11}
12
13impl Pdf {
14    /// Try to read the given PDF file.
15    ///
16    /// Returns `None` if it was unable to read it.
17    pub fn new(data: PdfData) -> Option<Self> {
18        let xref = root_xref(data.clone()).or_else(|| fallback(data))?;
19
20        Some(Self { xref })
21    }
22
23    /// Return the number of objects present in the PDF file.
24    pub fn len(&self) -> usize {
25        self.xref.len()
26    }
27
28    /// Return an iterator over all objects defined in the PDF file.
29    pub fn objects(&self) -> impl IntoIterator<Item = Object> {
30        self.xref.objects()
31    }
32
33    /// Return the pages of the PDF file.
34    pub fn pages(&self) -> Option<Vec<Page>> {
35        self.xref
36            .get(self.xref.trailer_data().pages_ref)
37            .and_then(|p| Pages::new(p, &self.xref).map(|p| p.pages))
38    }
39}