Document

Struct Document 

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

A PDF document that can contain multiple pages and metadata.

§Example

use oxidize_pdf::{Document, Page};

let mut doc = Document::new();
doc.set_title("My Document");
doc.set_author("John Doe");

let page = Page::a4();
doc.add_page(page);

doc.save("output.pdf").unwrap();

Implementations§

Source§

impl Document

Source

pub fn new() -> Self

Creates a new empty PDF document.

Source

pub fn add_page(&mut self, page: Page)

Adds a page to the document.

Source

pub fn set_title(&mut self, title: impl Into<String>)

Sets the document title.

Source

pub fn set_author(&mut self, author: impl Into<String>)

Sets the document author.

Source

pub fn set_form_manager(&mut self, form_manager: FormManager)

Sets the form manager for the document.

Source

pub fn set_subject(&mut self, subject: impl Into<String>)

Sets the document subject.

Source

pub fn set_keywords(&mut self, keywords: impl Into<String>)

Sets the document keywords.

Source

pub fn set_encryption(&mut self, encryption: DocumentEncryption)

Set document encryption

Source

pub fn encrypt_with_passwords( &mut self, user_password: impl Into<String>, owner_password: impl Into<String>, )

Set simple encryption with passwords

Source

pub fn is_encrypted(&self) -> bool

Check if document is encrypted

Source

pub fn set_open_action(&mut self, action: Action)

Set the action to execute when the document is opened

Source

pub fn open_action(&self) -> Option<&Action>

Get the document open action

Source

pub fn set_viewer_preferences(&mut self, preferences: ViewerPreferences)

Set viewer preferences for controlling document display

Source

pub fn viewer_preferences(&self) -> Option<&ViewerPreferences>

Get viewer preferences

Source

pub fn set_outline(&mut self, outline: OutlineTree)

Set document outline (bookmarks)

Source

pub fn outline(&self) -> Option<&OutlineTree>

Get document outline

Source

pub fn outline_mut(&mut self) -> Option<&mut OutlineTree>

Get mutable document outline

Source

pub fn set_named_destinations(&mut self, destinations: NamedDestinations)

Set named destinations

Source

pub fn named_destinations(&self) -> Option<&NamedDestinations>

Get named destinations

Source

pub fn named_destinations_mut(&mut self) -> Option<&mut NamedDestinations>

Get mutable named destinations

Source

pub fn set_page_labels(&mut self, labels: PageLabelTree)

Set page labels

Source

pub fn page_labels(&self) -> Option<&PageLabelTree>

Get page labels

Source

pub fn page_labels_mut(&mut self) -> Option<&mut PageLabelTree>

Get mutable page labels

Source

pub fn get_page_label(&self, page_index: u32) -> String

Get page label for a specific page

Source

pub fn get_all_page_labels(&self) -> Vec<String>

Get all page labels

Source

pub fn set_creator(&mut self, creator: impl Into<String>)

Sets the document creator (software that created the original document).

Source

pub fn set_producer(&mut self, producer: impl Into<String>)

Sets the document producer (software that produced the PDF).

Source

pub fn set_creation_date(&mut self, date: DateTime<Utc>)

Sets the document creation date.

Source

pub fn set_creation_date_local(&mut self, date: DateTime<Local>)

Sets the document creation date using local time.

Source

pub fn set_modification_date(&mut self, date: DateTime<Utc>)

Sets the document modification date.

Source

pub fn set_modification_date_local(&mut self, date: DateTime<Local>)

Sets the document modification date using local time.

Source

pub fn update_modification_date(&mut self)

Sets the modification date to the current time.

Source

pub fn set_default_font_encoding(&mut self, encoding: Option<FontEncoding>)

Sets the default font encoding for fonts that don’t specify an encoding.

This encoding will be applied to fonts in the PDF font dictionary when no explicit encoding is specified. Setting this to None (the default) means no encoding metadata will be added to fonts unless explicitly specified.

§Example
use oxidize_pdf::{Document, text::FontEncoding};

let mut doc = Document::new();
doc.set_default_font_encoding(Some(FontEncoding::WinAnsiEncoding));
Source

pub fn default_font_encoding(&self) -> Option<FontEncoding>

Gets the current default font encoding.

Source

pub fn add_font( &mut self, name: impl Into<String>, path: impl AsRef<Path>, ) -> Result<()>

Add a custom font from a file path

§Example
use oxidize_pdf::Document;

let mut doc = Document::new();
doc.add_font("MyFont", "path/to/font.ttf").unwrap();
Source

pub fn add_font_from_bytes( &mut self, name: impl Into<String>, data: Vec<u8>, ) -> Result<()>

Add a custom font from byte data

§Example
use oxidize_pdf::Document;

let mut doc = Document::new();
let font_data = vec![0; 1000]; // Your font data
doc.add_font_from_bytes("MyFont", font_data).unwrap();
Source

pub fn has_custom_font(&self, name: &str) -> bool

Check if a custom font is loaded

Source

pub fn custom_font_names(&self) -> Vec<String>

Get all loaded custom font names

Source

pub fn page_count(&self) -> usize

Gets the number of pages in the document.

Source

pub fn acro_form(&self) -> Option<&AcroForm>

Gets a reference to the AcroForm (interactive form) if present.

Source

pub fn acro_form_mut(&mut self) -> Option<&mut AcroForm>

Gets a mutable reference to the AcroForm (interactive form) if present.

Source

pub fn enable_forms(&mut self) -> &mut FormManager

Enables interactive forms by creating a FormManager if not already present. The FormManager handles both the AcroForm and the connection with page widgets.

Source

pub fn disable_forms(&mut self)

Disables interactive forms by removing both the AcroForm and FormManager.

Source

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

Saves the document to a file.

§Errors

Returns an error if the file cannot be created or written.

Source

pub fn save_with_config( &mut self, path: impl AsRef<Path>, config: WriterConfig, ) -> Result<()>

Saves the document to a file with custom writer configuration.

§Errors

Returns an error if the file cannot be created or written.

Source

pub fn save_with_custom_values( &mut self, path: impl AsRef<Path>, custom_values: &HashMap<String, String>, ) -> Result<()>

Saves the document to a file with custom values for headers/footers.

This method processes all pages to replace custom placeholders in headers and footers before saving the document.

§Arguments
  • path - The path where the document should be saved
  • custom_values - A map of placeholder names to their replacement values
§Errors

Returns an error if the file cannot be created or written.

Source

pub fn write(&mut self, buffer: &mut Vec<u8>) -> Result<()>

Writes the document to a buffer.

§Errors

Returns an error if the PDF cannot be generated.

Source

pub fn set_compress(&mut self, compress: bool)

Enables or disables compression for PDF streams.

When compression is enabled (default), content streams and XRef streams are compressed using Flate/Zlib compression to reduce file size. When disabled, streams are written uncompressed, making the PDF larger but easier to debug.

§Arguments
  • compress - Whether to enable compression
§Example
use oxidize_pdf::{Document, Page};

let mut doc = Document::new();

// Disable compression for debugging
doc.set_compress(false);

doc.set_title("My Document");
doc.add_page(Page::a4());

let pdf_bytes = doc.to_bytes().unwrap();
println!("Uncompressed PDF size: {} bytes", pdf_bytes.len());
Source

pub fn enable_xref_streams(&mut self, enable: bool) -> &mut Self

Enable or disable compressed cross-reference streams (PDF 1.5+).

Cross-reference streams provide more compact representation of the cross-reference table and support additional features like compressed object streams.

§Arguments
  • enable - Whether to enable compressed cross-reference streams
§Example
use oxidize_pdf::Document;

let mut doc = Document::new();
doc.enable_xref_streams(true);
Source

pub fn get_compress(&self) -> bool

Gets the current compression setting.

§Returns

Returns true if compression is enabled, false otherwise.

Source

pub fn to_bytes(&mut self) -> Result<Vec<u8>>

Generates the PDF document as bytes in memory.

This method provides in-memory PDF generation without requiring file I/O. The document is serialized to bytes and returned as a Vec<u8>.

§Returns

Returns the PDF document as bytes on success.

§Errors

Returns an error if the document cannot be serialized.

§Example
use oxidize_pdf::{Document, Page};

let mut doc = Document::new();
doc.set_title("My Document");

let page = Page::a4();
doc.add_page(page);

let pdf_bytes = doc.to_bytes().unwrap();
println!("Generated PDF size: {} bytes", pdf_bytes.len());
Source

pub fn to_bytes_with_config(&mut self, config: WriterConfig) -> Result<Vec<u8>>

Generates the PDF document as bytes with custom writer configuration.

This method allows customizing the PDF output (e.g., using XRef streams) while still generating the document in memory.

§Arguments
  • config - Writer configuration options
§Returns

Returns the PDF document as bytes on success.

§Errors

Returns an error if the document cannot be serialized.

§Example
use oxidize_pdf::{Document, Page};
use oxidize_pdf::writer::WriterConfig;

let mut doc = Document::new();
doc.set_title("My Document");

let page = Page::a4();
doc.add_page(page);

let config = WriterConfig {
    use_xref_streams: true,
    pdf_version: "1.5".to_string(),
    compress_streams: true,
};

let pdf_bytes = doc.to_bytes_with_config(config).unwrap();
println!("Generated PDF size: {} bytes", pdf_bytes.len());
Source

pub fn mark_entity( &mut self, id: impl Into<String>, entity_type: EntityType, bounds: BoundingBox, ) -> String

Mark a region of the PDF with semantic meaning for AI processing.

This creates an AI-Ready PDF that contains machine-readable metadata alongside the visual content, enabling automated document processing.

§Example
use oxidize_pdf::{Document, semantic::{EntityType, BoundingBox}};

let mut doc = Document::new();

// Mark an invoice number region
let entity_id = doc.mark_entity(
    "invoice_001".to_string(),
    EntityType::InvoiceNumber,
    BoundingBox::new(100.0, 700.0, 150.0, 20.0, 1)
);

// Add content and metadata
doc.set_entity_content(&entity_id, "INV-2024-001");
doc.add_entity_metadata(&entity_id, "confidence", "0.98");
Source

pub fn set_entity_content( &mut self, entity_id: &str, content: impl Into<String>, ) -> bool

Set the content text for an entity

Source

pub fn add_entity_metadata( &mut self, entity_id: &str, key: impl Into<String>, value: impl Into<String>, ) -> bool

Add metadata to an entity

Source

pub fn set_entity_confidence( &mut self, entity_id: &str, confidence: f32, ) -> bool

Set confidence score for an entity

Source

pub fn relate_entities( &mut self, from_id: &str, to_id: &str, relation_type: RelationType, ) -> bool

Add a relationship between two entities

Source

pub fn get_semantic_entities(&self) -> &[SemanticEntity]

Get all semantic entities in the document

Source

pub fn get_entities_by_type( &self, entity_type: EntityType, ) -> Vec<&SemanticEntity>

Get entities by type

Source

pub fn find_entity(&self, entity_id: &str) -> Option<&SemanticEntity>

Find an entity by ID

Source

pub fn remove_entity(&mut self, entity_id: &str) -> bool

Remove an entity by ID

Source

pub fn semantic_entity_count(&self) -> usize

Get the count of semantic entities

Source

pub fn add_xmp_metadata(&mut self, _xmp_data: &str) -> Result<ObjectId>

Add XMP metadata stream to the document (Pro feature placeholder)

Source

pub fn get_xmp_metadata(&self) -> Result<Option<String>>

Get XMP metadata from the document (Pro feature placeholder)

Source

pub fn extract_text(&self) -> Result<String>

Extract text content from all pages (placeholder implementation)

Source

pub fn extract_page_text(&self, page_index: usize) -> Result<String>

Extract text content from a specific page (placeholder implementation)

Trait Implementations§

Source§

impl Default for Document

Source§

fn default() -> Self

Returns the “default value” for a type. 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more