Optimize PDF Resources using Rust via C++
Contents
[
Hide
]
Optimize PDF Resources
Optimize resources in the document:
- Resources that are not used on the document pages are removed.
- Equal resources are joined into a single object.
- Unused objects are deleted.
Optimization may include compressing images, removing unused objects, and optimizing fonts for reduced file size and improved performance. Any error during this operation is logged and terminates the program.
- The open method loads the specified PDF file (sample.pdf) into memory.
- Optimizes the resources within the PDF for efficiency using optimize_resource method.
- The save_as method saves the optimized PDF to a new file.
The following code snippet shows how to optimize a PDF document.
use asposepdf::Document;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Open a PDF-document named "sample.pdf"
let pdf = Document::open("sample.pdf")?;
// Save the previously opened PDF-document with new filename
pdf.save_as("sample_open.pdf")?;
Ok(())
}