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());