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: DiagnosticsEverything repaired while opening the file.
Implementations§
Source§impl Document
impl Document
Sourcepub fn page_count(&self) -> u32
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.
Sourcepub fn page(&self, index: impl Into<PageIndex>) -> Result<PageDict, Error>
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.
Sourcepub fn trailer_object_number(&self) -> u32
pub fn trailer_object_number(&self) -> u32
The object number the trailer came from; zero for a bare trailer
dictionary.
Sourcepub fn version(&self) -> Option<PdfVersion>
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.
Sourcepub fn header_offset(&self) -> u64
pub fn header_offset(&self) -> u64
Where the %PDF header sat in the original file. Non-zero means
everything before it was ignored.
Sourcepub fn lazy_diagnostics(&self) -> Diagnostics
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());Sourcepub fn xref_was_rebuilt(&self) -> bool
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.
Sourcepub fn last_xref_offset(&self) -> u64
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());Sourcepub fn main_xref_is_stream(&self) -> bool
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.
Sourcepub fn encrypt_dict(&self) -> Option<(&Dict, bool)>
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.
Sourcepub fn permissions(&self) -> Permissions
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.
Sourcepub fn owner_permissions(&self) -> Permissions
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.
Sourcepub fn is_encrypted(&self) -> bool
pub fn is_encrypted(&self) -> bool
Whether the document is encrypted.
Sourcepub fn security_handler(&self) -> &SecurityHandler
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.
Sourcepub fn store(&self) -> &Arc<ObjectStore> ⓘ
pub fn store(&self) -> &Arc<ObjectStore> ⓘ
The object store, for fetching references.