Skip to main content

Document

Struct Document 

Source
pub struct Document { /* private fields */ }
Expand description

A loaded PDF document.

Implementations§

Source§

impl Document

Source

pub fn load(data: Vec<u8>) -> Result<Document>

Loads a document from bytes: locates the %PDF-x.y header (scanning the first 1 KiB, defaulting to 1.4), loads the xref, and sets up decryption for files using the Standard security handler (RC4 or AES) under the empty user password (password-protected files yield Error::Encrypted).

The page tree is not walked here: it is flattened lazily on the first page access, so opening a document (or reading page_count) does not parse every page dictionary.

Source

pub fn open(path: impl AsRef<Path>) -> Result<Document>

Reads the file at path and loads it via Document::load.

Source

pub fn version(&self) -> (u8, u8)

The PDF version from the header, e.g. (1, 7).

Source

pub fn bytes(&self) -> &[u8]

Raw bytes of the loaded file.

Source

pub fn xref(&self) -> &Xref

The merged cross-reference table and trailer.

Source

pub fn get(&self, r: ObjRef) -> Result<Object>

Fetches an indirect object by reference (xref lookup, object-stream indirection, cached). A generation mismatch between the request and the file is tolerated (lenient).

Source

pub fn resolve(&self, o: &Object) -> Result<Object>

Chases reference chains with a depth guard of MAX_RESOLVE_DEPTH (beyond that: Error::CircularReference); a reference to a missing or unreadable object resolves to Null (lenient).

Source

pub fn stream_data(&self, s: &Stream) -> Result<Vec<u8>>

Decodes a stream’s data through its filter chain, resolving indirect filter parameters against this document.

Source

pub fn page_count(&self) -> usize

Number of pages.

Reports the page tree’s declared /Count — the same value mature engines return — without walking the tree, so it is cheap on an otherwise-untouched document. If /Count is absent or implausible the tree is flattened and its true (lenient, cycle- and depth-guarded) length is returned instead. Once the tree has been flattened for any reason, that flattened length is authoritative.

Source

pub fn page(&self, index: usize) -> Result<Page>

The page at 0-based index.

Source

pub fn metadata(&self) -> Metadata

Document metadata from the trailer /Info dictionary (lenient: absent or malformed entries are simply None).

Source§

impl Document

Source

pub fn elements(&self, opts: ElementOpts) -> Elements<'_>

Lazy iteration over the document’s elements. Physical elements come in file order (header, objects by offset with object-stream members after their container, xref sections newest→oldest, trailer, startxref, eof); logical elements follow in document order. Nothing is parsed or decoded before it is yielded; an element that fails to parse yields Err for that item and iteration continues.

Physical objects sort by (file offset, member index, object number): in-file objects use their own offset with member index 0; object-stream members use their container’s offset with member index 1 + index so they directly follow their container in stream-index order; members whose container is missing or free sort last (offset u64::MAX) and surface as Err items; ties on offset and member index break by ascending object number.

Trait Implementations§

Source§

impl Resolve for Document

Source§

fn resolve_ref(&self, r: ObjRef) -> Option<Object>

Returns the referenced object, or None if it cannot be resolved.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.