Skip to main content

Document

Struct Document 

Source
pub struct Document {
    pub diags: Diagnostics,
    /* private fields */
}
Expand description

An opened document.

Holds the file, everything the reader learned about where its objects are, and the lazy store that turns references into objects. Send + Sync, so pages can be rendered in parallel.

Fields§

§diags: Diagnostics

Everything repaired while opening the file.

Implementations§

Source§

impl Document

Source

pub fn page_count(&self) -> u32

How many pages the document has.

A u32, deliberately, and not a PageIndex: a count answers “how many” and an index answers “which one”, and the last valid index of a three-page document is 2, not 3. Giving them one type would let each be passed where the other is meant, which is what the newtype exists to stop.

Source

pub fn page(&self, index: impl Into<PageIndex>) -> Result<PageDict, Error>

The page at index, counting from zero.

The tree is walked in order and the pages found along the way are remembered, so reading a document front to back costs one traversal. A kid that will not load as a dictionary still consumes its slot: a missing page leaves a hole rather than shifting every page after it.

Takes impl Into<PageIndex>, so doc.page(0) reads as it always has.

§Errors

Error::NoPage for an index past the count, or one the walk could not reach.

Source

pub fn trailer(&self) -> &Dict

The trailer dictionary, merged across every section.

Source

pub fn trailer_object_number(&self) -> u32

The object number the trailer came from; zero for a bare trailer dictionary.

Source

pub fn catalog(&self) -> Result<Dict, Error>

The document catalog.

§Errors

Error::NoCatalog when the trailer names none.

Source

pub fn version(&self) -> Option<PdfVersion>

The version the header declared: PdfVersion::PDF_1_7 for %PDF-1.7.

Never validated — a header claiming 9.9 opens like any other and reports 9.9. None means the header carried no readable digits at all, which a file with no %PDF line and one with %PDF-x.y both produce; the writer’s fallback for that case is 1.7.

Source

pub fn header_offset(&self) -> u64

Where the %PDF header sat in the original file. Non-zero means everything before it was ignored.

Source

pub fn lazy_diagnostics(&self) -> Diagnostics

What the object store has repaired since the file opened, as a running total — read it after the work, not at load.

Document::diags is the load-time snapshot and never changes. This one grows, because the store is lazy: a wrong /Length or a bad table offset is only discovered when a caller first reaches that object. It clones rather than draining, so asking twice between two fetches gives the same answer twice.

let bytes: Arc<[u8]> = Arc::from(&include_bytes!("../tests/files/minimal.pdf")[..]);
let doc = load(bytes, &LoadOptions::default())?;
// A clean file repairs nothing, before or after its pages are read.
let _ = doc.page(0);
assert!(doc.lazy_diagnostics().is_empty());
Source

pub fn xref_was_rebuilt(&self) -> bool

Whether the cross-reference table came from the recovery scan rather than the file’s own sections. An incremental save is unsafe when it did.

Source

pub fn last_xref_offset(&self) -> u64

Byte offset of the newest cross-reference section the load chained from, or 0 when the table was rebuilt by scanning.

This is what an incremental update writes as its /Prev, so the zero carries meaning rather than being an absence: a rebuilt document has no previous section worth naming, and the writer answers by emitting a full table after the original bytes instead of a delta.

let bytes: Arc<[u8]> = Arc::from(&include_bytes!("../tests/files/minimal.pdf")[..]);
let doc = load(bytes, &LoadOptions::default())?;
assert!(doc.last_xref_offset() > 0);
assert!(!doc.xref_was_rebuilt());
Source

pub fn main_xref_is_stream(&self) -> bool

Whether the document’s main cross-reference — the newest section, the one startxref names — was a stream rather than a classic table.

Not “the chain contained a stream somewhere”: a hybrid file whose newest section is a classic table answers false. The writer reads it to decide whether an incremental update appends a classic delta table or folds the cross-reference into a stream object, and matching the original keeps a reader that only understands one of the two working.

Source

pub fn encrypt_dict(&self) -> Option<(&Dict, bool)>

The /Encrypt dictionary as the file wrote it, and whether the trailer held it directly rather than by reference.

Returned raw and undecrypted, because the encryption dictionary is the one object in a document that is always plaintext. Some here does not imply the document opened encrypted — a file can declare a handler this reader answered with SecurityHandler::Identity.

Source

pub fn permissions(&self) -> Permissions

What the document permits, for the password that opened it.

The owner’s own unrestricted view is Document::owner_permissions. An unencrypted document permits everything.

Source

pub fn owner_permissions(&self) -> Permissions

What the document permits under the owner’s view.

Every permission, for a document the owner password opened; otherwise the same answer as Document::permissions.

Source

pub fn is_encrypted(&self) -> bool

Whether the document is encrypted.

Source

pub fn security_handler(&self) -> &SecurityHandler

The security handler the password opened this document with.

SecurityHandler::Identity for an unencrypted document, and for one whose crypt filter is /Identity.

The writer needs this to save an encrypted document as encrypted: it re-enciphers every string and stream under the same handler, so the result opens with the same password. It carries the file key, so it is deliberately not Clone-friendly to hold onto — borrow it for the length of a save and let it go.

Source

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

The file, from its header onwards.

Source

pub fn store(&self) -> &Arc<ObjectStore>

The object store, for fetching references.

Source

pub fn xref(&self) -> &Xref

Where every object lives.

Trait Implementations§

Source§

impl Debug for Document

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Resolve for Document

Source§

fn fetch(&self, r: ObjRef) -> Result<Arc<Object>, Error>

The object stored under r, or why it could not be produced. Read more

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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.