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
impl Document
Sets the document author.
Sourcepub fn set_form_manager(&mut self, form_manager: FormManager)
pub fn set_form_manager(&mut self, form_manager: FormManager)
Sets the form manager for the document.
Sourcepub fn set_subject(&mut self, subject: impl Into<String>)
pub fn set_subject(&mut self, subject: impl Into<String>)
Sets the document subject.
Sourcepub fn set_keywords(&mut self, keywords: impl Into<String>)
pub fn set_keywords(&mut self, keywords: impl Into<String>)
Sets the document keywords.
Sourcepub fn set_encryption(&mut self, encryption: DocumentEncryption)
pub fn set_encryption(&mut self, encryption: DocumentEncryption)
Set document encryption
Sourcepub fn encrypt_with_passwords(
&mut self,
user_password: impl Into<String>,
owner_password: impl Into<String>,
)
pub fn encrypt_with_passwords( &mut self, user_password: impl Into<String>, owner_password: impl Into<String>, )
Set simple encryption with passwords
Sourcepub fn is_encrypted(&self) -> bool
pub fn is_encrypted(&self) -> bool
Check if document is encrypted
Sourcepub fn set_open_action(&mut self, action: Action)
pub fn set_open_action(&mut self, action: Action)
Set the action to execute when the document is opened
Sourcepub fn open_action(&self) -> Option<&Action>
pub fn open_action(&self) -> Option<&Action>
Get the document open action
Sourcepub fn set_viewer_preferences(&mut self, preferences: ViewerPreferences)
pub fn set_viewer_preferences(&mut self, preferences: ViewerPreferences)
Set viewer preferences for controlling document display
Sourcepub fn viewer_preferences(&self) -> Option<&ViewerPreferences>
pub fn viewer_preferences(&self) -> Option<&ViewerPreferences>
Get viewer preferences
Sourcepub fn set_outline(&mut self, outline: OutlineTree)
pub fn set_outline(&mut self, outline: OutlineTree)
Set document outline (bookmarks)
Sourcepub fn outline(&self) -> Option<&OutlineTree>
pub fn outline(&self) -> Option<&OutlineTree>
Get document outline
Sourcepub fn outline_mut(&mut self) -> Option<&mut OutlineTree>
pub fn outline_mut(&mut self) -> Option<&mut OutlineTree>
Get mutable document outline
Sourcepub fn set_named_destinations(&mut self, destinations: NamedDestinations)
pub fn set_named_destinations(&mut self, destinations: NamedDestinations)
Set named destinations
Sourcepub fn named_destinations(&self) -> Option<&NamedDestinations>
pub fn named_destinations(&self) -> Option<&NamedDestinations>
Get named destinations
Sourcepub fn named_destinations_mut(&mut self) -> Option<&mut NamedDestinations>
pub fn named_destinations_mut(&mut self) -> Option<&mut NamedDestinations>
Get mutable named destinations
Sourcepub fn set_page_labels(&mut self, labels: PageLabelTree)
pub fn set_page_labels(&mut self, labels: PageLabelTree)
Set page labels
Sourcepub fn page_labels(&self) -> Option<&PageLabelTree>
pub fn page_labels(&self) -> Option<&PageLabelTree>
Get page labels
Sourcepub fn page_labels_mut(&mut self) -> Option<&mut PageLabelTree>
pub fn page_labels_mut(&mut self) -> Option<&mut PageLabelTree>
Get mutable page labels
Sourcepub fn get_page_label(&self, page_index: u32) -> String
pub fn get_page_label(&self, page_index: u32) -> String
Get page label for a specific page
Sourcepub fn get_all_page_labels(&self) -> Vec<String>
pub fn get_all_page_labels(&self) -> Vec<String>
Get all page labels
Sourcepub fn set_creator(&mut self, creator: impl Into<String>)
pub fn set_creator(&mut self, creator: impl Into<String>)
Sets the document creator (software that created the original document).
Sourcepub fn set_producer(&mut self, producer: impl Into<String>)
pub fn set_producer(&mut self, producer: impl Into<String>)
Sets the document producer (software that produced the PDF).
Sourcepub fn set_creation_date(&mut self, date: DateTime<Utc>)
pub fn set_creation_date(&mut self, date: DateTime<Utc>)
Sets the document creation date.
Sourcepub fn set_creation_date_local(&mut self, date: DateTime<Local>)
pub fn set_creation_date_local(&mut self, date: DateTime<Local>)
Sets the document creation date using local time.
Sourcepub fn set_modification_date(&mut self, date: DateTime<Utc>)
pub fn set_modification_date(&mut self, date: DateTime<Utc>)
Sets the document modification date.
Sourcepub fn set_modification_date_local(&mut self, date: DateTime<Local>)
pub fn set_modification_date_local(&mut self, date: DateTime<Local>)
Sets the document modification date using local time.
Sourcepub fn update_modification_date(&mut self)
pub fn update_modification_date(&mut self)
Sets the modification date to the current time.
Sourcepub fn set_default_font_encoding(&mut self, encoding: Option<FontEncoding>)
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));Sourcepub fn default_font_encoding(&self) -> Option<FontEncoding>
pub fn default_font_encoding(&self) -> Option<FontEncoding>
Gets the current default font encoding.
Sourcepub fn add_font(
&mut self,
name: impl Into<String>,
path: impl AsRef<Path>,
) -> Result<()>
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();Sourcepub fn add_font_from_bytes(
&mut self,
name: impl Into<String>,
data: Vec<u8>,
) -> Result<()>
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();Sourcepub fn has_custom_font(&self, name: &str) -> bool
pub fn has_custom_font(&self, name: &str) -> bool
Check if a custom font is loaded
Sourcepub fn custom_font_names(&self) -> Vec<String>
pub fn custom_font_names(&self) -> Vec<String>
Get all loaded custom font names
Sourcepub fn page_count(&self) -> usize
pub fn page_count(&self) -> usize
Gets the number of pages in the document.
Sourcepub fn acro_form(&self) -> Option<&AcroForm>
pub fn acro_form(&self) -> Option<&AcroForm>
Gets a reference to the AcroForm (interactive form) if present.
Sourcepub fn acro_form_mut(&mut self) -> Option<&mut AcroForm>
pub fn acro_form_mut(&mut self) -> Option<&mut AcroForm>
Gets a mutable reference to the AcroForm (interactive form) if present.
Sourcepub fn enable_forms(&mut self) -> &mut FormManager
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.
Sourcepub fn disable_forms(&mut self)
pub fn disable_forms(&mut self)
Disables interactive forms by removing both the AcroForm and FormManager.
Sourcepub fn save_with_config(
&mut self,
path: impl AsRef<Path>,
config: WriterConfig,
) -> Result<()>
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.
Sourcepub fn save_with_custom_values(
&mut self,
path: impl AsRef<Path>,
custom_values: &HashMap<String, String>,
) -> Result<()>
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 savedcustom_values- A map of placeholder names to their replacement values
§Errors
Returns an error if the file cannot be created or written.
Sourcepub fn set_compress(&mut self, compress: bool)
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());Sourcepub fn enable_xref_streams(&mut self, enable: bool) -> &mut Self
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);Sourcepub fn get_compress(&self) -> bool
pub fn get_compress(&self) -> bool
Gets the current compression setting.
§Returns
Returns true if compression is enabled, false otherwise.
Sourcepub fn to_bytes(&mut self) -> Result<Vec<u8>>
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());Sourcepub fn to_bytes_with_config(&mut self, config: WriterConfig) -> Result<Vec<u8>>
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());Sourcepub fn mark_entity(
&mut self,
id: impl Into<String>,
entity_type: EntityType,
bounds: BoundingBox,
) -> String
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");Sourcepub fn set_entity_content(
&mut self,
entity_id: &str,
content: impl Into<String>,
) -> bool
pub fn set_entity_content( &mut self, entity_id: &str, content: impl Into<String>, ) -> bool
Set the content text for an entity
Sourcepub fn add_entity_metadata(
&mut self,
entity_id: &str,
key: impl Into<String>,
value: impl Into<String>,
) -> bool
pub fn add_entity_metadata( &mut self, entity_id: &str, key: impl Into<String>, value: impl Into<String>, ) -> bool
Add metadata to an entity
Sourcepub fn set_entity_confidence(
&mut self,
entity_id: &str,
confidence: f32,
) -> bool
pub fn set_entity_confidence( &mut self, entity_id: &str, confidence: f32, ) -> bool
Set confidence score for an entity
Sourcepub fn relate_entities(
&mut self,
from_id: &str,
to_id: &str,
relation_type: RelationType,
) -> bool
pub fn relate_entities( &mut self, from_id: &str, to_id: &str, relation_type: RelationType, ) -> bool
Add a relationship between two entities
Sourcepub fn get_semantic_entities(&self) -> &[SemanticEntity]
pub fn get_semantic_entities(&self) -> &[SemanticEntity]
Get all semantic entities in the document
Sourcepub fn get_entities_by_type(
&self,
entity_type: EntityType,
) -> Vec<&SemanticEntity>
pub fn get_entities_by_type( &self, entity_type: EntityType, ) -> Vec<&SemanticEntity>
Get entities by type
Sourcepub fn find_entity(&self, entity_id: &str) -> Option<&SemanticEntity>
pub fn find_entity(&self, entity_id: &str) -> Option<&SemanticEntity>
Find an entity by ID
Sourcepub fn remove_entity(&mut self, entity_id: &str) -> bool
pub fn remove_entity(&mut self, entity_id: &str) -> bool
Remove an entity by ID
Sourcepub fn semantic_entity_count(&self) -> usize
pub fn semantic_entity_count(&self) -> usize
Get the count of semantic entities
Sourcepub fn add_xmp_metadata(&mut self, _xmp_data: &str) -> Result<ObjectId>
pub fn add_xmp_metadata(&mut self, _xmp_data: &str) -> Result<ObjectId>
Add XMP metadata stream to the document (Pro feature placeholder)
Sourcepub fn get_xmp_metadata(&self) -> Result<Option<String>>
pub fn get_xmp_metadata(&self) -> Result<Option<String>>
Get XMP metadata from the document (Pro feature placeholder)
Sourcepub fn extract_text(&self) -> Result<String>
pub fn extract_text(&self) -> Result<String>
Extract text content from all pages (placeholder implementation)
Sourcepub fn extract_page_text(&self, page_index: usize) -> Result<String>
pub fn extract_page_text(&self, page_index: usize) -> Result<String>
Extract text content from a specific page (placeholder implementation)
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Document
impl RefUnwindSafe for Document
impl Send for Document
impl Sync for Document
impl Unpin for Document
impl UnwindSafe for Document
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().