Struct lopdf::Document

source ·
pub struct Document {
    pub version: String,
    pub trailer: Dictionary,
    pub reference_table: Xref,
    pub objects: BTreeMap<ObjectId, Object>,
    pub max_id: u32,
    pub max_bookmark_id: u32,
    pub bookmarks: Vec<u32>,
    pub bookmark_table: HashMap<u32, Bookmark>,
    pub xref_start: usize,
}
Expand description

A PDF document.

This can both be a combination of multiple incremental updates or just one (the last) incremental update in a PDF file.

Fields§

§version: String

The version of the PDF specification to which the file conforms.

§trailer: Dictionary

The trailer gives the location of the cross-reference table and of certain special objects.

§reference_table: Xref

The cross-reference table contains locations of the indirect objects.

§objects: BTreeMap<ObjectId, Object>

The objects that make up the document contained in the file.

§max_id: u32

Current maximum object id within the document.

§max_bookmark_id: u32

Current maximum object id within Bookmarks.

§bookmarks: Vec<u32>

The bookmarks in the document. Render at the very end of document after renumbering objects.

§bookmark_table: HashMap<u32, Bookmark>

used to locate a stored Bookmark so children can be appended to it via its id. Otherwise we need to do recursive lookups and returns on the bookmarks internal layout Vec

§xref_start: usize

The byte the cross-reference table starts at. This value is only set during reading, but not when writing the file. It is used to support incremental updates in PDFs. Default value is 0.

Implementations§

source§

impl Document

source

pub fn new() -> Self

Create new PDF document.

source

pub fn new_from_prev(prev: &Document) -> Self

Create a new PDF document that is an incremental update to a previous document.

source

pub fn adjust_zero_pages(&mut self)

Adjusts the Parents that have a ObjectId of (0,_) to that of their first child. will recurse through all entries till all parents of children are set. This should be ran before building the final bookmark objects but after renumbering of objects.

source

pub fn dereference<'a>( &'a self, object: &'a Object ) -> Result<(Option<ObjectId>, &'a Object)>

Follow references if the supplied object is a reference.

Returns a tuple of an optional object id and final object. The object id will be None if the object was not a reference. Otherwise, it will be the last object id in the reference chain.

source

pub fn get_object(&self, id: ObjectId) -> Result<&Object>

Get object by object id, will iteratively dereference a referenced object.

source

pub fn has_object(&self, id: ObjectId) -> bool

Determines if an object exists in the current document (or incremental update.) with the given ObjectId. true if the object exists, false if it does not exist.

source

pub fn get_object_mut(&mut self, id: ObjectId) -> Result<&mut Object>

Get mutable reference to object by object id, will iteratively dereference a referenced object.

source

pub fn get_object_page(&self, id: ObjectId) -> Result<ObjectId>

Get page object_id of the specified object object_id

source

pub fn get_dictionary(&self, id: ObjectId) -> Result<&Dictionary>

Get dictionary object by id.

source

pub fn get_dictionary_mut(&mut self, id: ObjectId) -> Result<&mut Dictionary>

Get a mutable dictionary object by id.

source

pub fn get_dict_in_dict( &self, node: &Dictionary, key: &[u8] ) -> Result<&Dictionary>

Get dictionary in dictionary by key.

source

pub fn traverse_objects<A: Fn(&mut Object)>( &mut self, action: A ) -> Vec<ObjectId>

Traverse objects from trailer recursively, return all referenced object IDs.

source

pub fn get_encrypted(&self) -> Result<&Dictionary>

Return dictionary with encryption information

source

pub fn is_encrypted(&self) -> bool

Return true is PDF document is encrypted

source

pub fn decrypt<P: AsRef<[u8]>>(&mut self, password: P) -> Result<()>

Replaces all encrypted Strings and Streams with their decrypted contents

source

pub fn catalog(&self) -> Result<&Dictionary>

Return the PDF document catalog, which is the root of the document’s object graph.

source

pub fn catalog_mut(&mut self) -> Result<&mut Dictionary>

Return a mutable reference to the PDF document catalog, which is the root of the document’s object graph.

source

pub fn get_pages(&self) -> BTreeMap<u32, ObjectId>

Get page numbers and corresponding object ids.

source

pub fn page_iter(&self) -> impl Iterator<Item = ObjectId> + '_

source

pub fn get_page_contents(&self, page_id: ObjectId) -> Vec<ObjectId>

Get content stream object ids of a page.

source

pub fn add_page_contents( &mut self, page_id: ObjectId, content: Vec<u8> ) -> Result<()>

Add content to a page. All existing content will be unchanged.

source

pub fn get_page_content(&self, page_id: ObjectId) -> Result<Vec<u8>>

Get content of a page.

source

pub fn get_page_resources( &self, page_id: ObjectId ) -> (Option<&Dictionary>, Vec<ObjectId>)

Get resources used by a page.

source

pub fn get_page_fonts( &self, page_id: ObjectId ) -> BTreeMap<Vec<u8>, &Dictionary>

Get fonts used by a page.

source

pub fn get_page_annotations(&self, page_id: ObjectId) -> Vec<&Dictionary>

Get the PDF annotations of a page. The /Subtype of each annotation dictionary defines the annotation type (Text, Link, Highlight, Underline, Ink, Popup, Widget, etc.). The /Rect of an annotation dictionary defines its location on the page.

source

pub fn decode_text(encoding: Option<&str>, bytes: &[u8]) -> String

source

pub fn encode_text(encoding: Option<&str>, text: &str) -> Vec<u8>

source§

impl Document

source

pub fn add_bookmark(&mut self, bookmark: Bookmark, parent: Option<u32>) -> u32

source

pub fn build_outline(&mut self) -> Option<ObjectId>

source§

impl Document

source

pub fn get_outline( &self, node: &Dictionary, named_destinations: &mut BTreeMap<Vec<u8>, Destination> ) -> Result<Option<Outline>>

source

pub fn get_outlines( &self, node: Option<Object>, outlines: Option<Vec<Outline>>, named_destinations: &mut BTreeMap<Vec<u8>, Destination> ) -> Result<Option<Vec<Outline>>>

source§

impl Document

source

pub fn get_named_destinations( &self, tree: &Dictionary, named_destinations: &mut BTreeMap<Vec<u8>, Destination> ) -> Result<()>

source§

impl Document

source

pub fn get_toc(&self) -> Result<Toc>

source§

impl Document

source

pub fn with_version<S: Into<String>>(version: S) -> Document

Create new PDF document with version.

source

pub fn new_object_id(&mut self) -> ObjectId

Create an object ID.

source

pub fn add_object<T: Into<Object>>(&mut self, object: T) -> ObjectId

Add PDF object into document’s object list.

source

pub fn set_object<T: Into<Object>>(&mut self, id: ObjectId, object: T)

source

pub fn remove_object(&mut self, object_id: &ObjectId) -> Result<()>

Remove PDF object from document’s object list.

source

pub fn get_or_create_resources( &mut self, page_id: ObjectId ) -> Result<&mut Object>

Get the pages resources.

Get Object that has the key “Resources”.

source

pub fn add_xobject<N: Into<Vec<u8>>>( &mut self, page_id: ObjectId, xobject_name: N, xobject_id: ObjectId ) -> Result<()>

Add XObject to a page.

Get Object that has the key Resources -> XObject.

source

pub fn add_graphics_state<N: Into<Vec<u8>>>( &mut self, page_id: ObjectId, gs_name: N, gs_id: ObjectId ) -> Result<()>

Add Graphics State to a page.

Get Object that has the key Resources -> ExtGState.

source§

impl Document

source

pub fn get_and_decode_page_content( &self, page_id: ObjectId ) -> Result<Content<Vec<Operation>>>

Get decoded page content;

source

pub fn add_to_page_content( &mut self, page_id: ObjectId, content: Content<Vec<Operation>> ) -> Result<()>

Add content to a page. All existing content will be unchanged.

source

pub fn extract_text(&self, page_numbers: &[u32]) -> Result<String>

source

pub fn replace_text( &mut self, page_number: u32, text: &str, other_text: &str ) -> Result<()>

source

pub fn insert_image( &mut self, page_id: ObjectId, img_object: Stream, position: (f32, f32), size: (f32, f32) ) -> Result<()>

source

pub fn insert_form_object( &mut self, page_id: ObjectId, form_obj: Stream ) -> Result<()>

source§

impl Document

source

pub fn change_producer(&mut self, producer: &str)

Change producer of document information dictionary.

source

pub fn compress(&mut self)

Compress PDF stream objects.

source

pub fn decompress(&mut self)

Decompress PDF stream objects.

source

pub fn delete_pages(&mut self, page_numbers: &[u32])

Delete pages.

source

pub fn prune_objects(&mut self) -> Vec<ObjectId>

Prune all unused objects.

source

pub fn delete_object(&mut self, id: ObjectId) -> Option<Object>

Delete object by object ID.

source

pub fn delete_zero_length_streams(&mut self) -> Vec<ObjectId>

Delete zero length stream objects.

source

pub fn renumber_objects(&mut self)

Renumber objects, normally called after delete_unused_objects.

source

pub fn renumber_bookmarks(&mut self, old: &ObjectId, new: &ObjectId)

source

pub fn renumber_objects_with(&mut self, starting_id: u32)

Renumber objects with a custom starting id, this is very useful in case of multiple document objects insertion in a single main document

source

pub fn change_content_stream(&mut self, stream_id: ObjectId, content: Vec<u8>)

source

pub fn change_page_content( &mut self, page_id: ObjectId, content: Vec<u8> ) -> Result<()>

source

pub fn extract_stream( &self, stream_id: ObjectId, decompress: bool ) -> Result<()>

source§

impl Document

source

pub fn load<P: AsRef<Path>>(path: P) -> Result<Document>

Load a PDF document from a specified file path.

source

pub fn load_filtered<P: AsRef<Path>>( path: P, filter_func: fn(_: (u32, u16), _: &mut Object) -> Option<((u32, u16), Object)> ) -> Result<Document>

source

pub fn load_from<R: Read>(source: R) -> Result<Document>

Load a PDF document from an arbitrary source.

source

pub fn load_mem(buffer: &[u8]) -> Result<Document>

Load a PDF document from a memory slice.

source§

impl Document

source

pub fn save<P: AsRef<Path>>(&mut self, path: P) -> Result<File>

Save PDF document to specified file path.

source

pub fn save_to<W: Write>(&mut self, target: &mut W) -> Result<()>

Save PDF to arbitrary target

Trait Implementations§

source§

impl Clone for Document

source§

fn clone(&self) -> Document

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Document

source§

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

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

impl Default for Document

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl TryInto<Document> for &[u8]

§

type Error = Error

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

fn try_into(self) -> Result<Document>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.