| 1 |
use lopdf::dictionary; |
| 2 |
use lopdf::{Document, Object, SaveOptions, Stream}; |
| 3 |
use lopdf::content::{Content, Operation}; |
| 4 |
|
| 5 |
fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 6 |
// Create a simple PDF document |
| 7 |
let mut doc = Document::with_version("1.5"); |
| 8 |
let pages_id = doc.new_object_id(); |
| 9 |
|
| 10 |
// Add many objects to demonstrate object stream compression |
| 11 |
let font_id = doc.add_object(dictionary! { |
| 12 |
"Type" => "Font", |
| 13 |
"Subtype" => "Type1", |
| 14 |
"BaseFont" => "Helvetica" |
| 15 |
}); |
| 16 |
|
| 17 |
let bold_font_id = doc.add_object(dictionary! { |
| 18 |
"Type" => "Font", |
| 19 |
"Subtype" => "Type1", |
| 20 |
"BaseFont" => "Helvetica-Bold" |
| 21 |
}); |
| 22 |
|
| 23 |
let italic_font_id = doc.add_object(dictionary! { |
| 24 |
"Type" => "Font", |
| 25 |
"Subtype" => "Type1", |
| 26 |
"BaseFont" => "Helvetica-Oblique" |
| 27 |
}); |
| 28 |
|
| 29 |
// Add some metadata objects |
| 30 |
for i in 0..20 { |
| 31 |
doc.add_object(dictionary! { |
| 32 |
"Type" => format!("CustomData{}", i), |
| 33 |
"Value" => i, |
| 34 |
"Description" => format!("This is custom data object number {}", i) |
| 35 |
}); |
| 36 |
} |
| 37 |
|
| 38 |
let resources_id = doc.add_object(dictionary! { |
| 39 |
"Font" => dictionary! { |
| 40 |
"F1" => font_id, |
| 41 |
"F2" => bold_font_id, |
| 42 |
"F3" => italic_font_id, |
| 43 |
}, |
| 44 |
}); |
| 45 |
|
| 46 |
// Create page content |
| 47 |
let content = Content { |
| 48 |
operations: vec![ |
| 49 |
Operation::new("BT", vec![]), |
| 50 |
Operation::new("Tf", vec!["F1".into(), 24.into()]), |
| 51 |
Operation::new("Td", vec![50.into(), 700.into()]), |
| 52 |
Operation::new("Tj", vec![Object::string_literal("Object Streams Demo")]), |
| 53 |
Operation::new("ET", vec![]), |
| 54 |
|
| 55 |
Operation::new("BT", vec![]), |
| 56 |
Operation::new("Tf", vec!["F2".into(), 16.into()]), |
| 57 |
Operation::new("Td", vec![50.into(), 650.into()]), |
| 58 |
Operation::new("Tj", vec![Object::string_literal("This PDF uses object streams!")]), |
| 59 |
Operation::new("ET", vec![]), |
| 60 |
|
| 61 |
Operation::new("BT", vec![]), |
| 62 |
Operation::new("Tf", vec!["F1".into(), 12.into()]), |
| 63 |
Operation::new("Td", vec![50.into(), 600.into()]), |
| 64 |
Operation::new("Tj", vec![Object::string_literal("Multiple non-stream objects are compressed together.")]), |
| 65 |
Operation::new("ET", vec![]), |
| 66 |
], |
| 67 |
}; |
| 68 |
|
| 69 |
let content_id = doc.add_object(Stream::new( |
| 70 |
dictionary! {}, |
| 71 |
content.encode()? |
| 72 |
)); |
| 73 |
|
| 74 |
let page_id = doc.add_object(dictionary! { |
| 75 |
"Type" => "Page", |
| 76 |
"Parent" => pages_id, |
| 77 |
"Contents" => content_id, |
| 78 |
"Resources" => resources_id, |
| 79 |
"MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 80 |
}); |
| 81 |
|
| 82 |
doc.objects.insert(pages_id, Object::Dictionary(dictionary! { |
| 83 |
"Type" => "Pages", |
| 84 |
"Kids" => vec![page_id.into()], |
| 85 |
"Count" => 1, |
| 86 |
})); |
| 87 |
|
| 88 |
let catalog_id = doc.add_object(dictionary! { |
| 89 |
"Type" => "Catalog", |
| 90 |
"Pages" => pages_id, |
| 91 |
}); |
| 92 |
|
| 93 |
doc.trailer.set("Root", catalog_id); |
| 94 |
|
| 95 |
// Save with traditional format |
| 96 |
let mut traditional_buffer = Vec::new(); |
| 97 |
doc.save_to(&mut traditional_buffer)?; |
| 98 |
let traditional_size = traditional_buffer.len(); |
| 99 |
|
| 100 |
// Save with object streams |
| 101 |
let mut modern_buffer = Vec::new(); |
| 102 |
doc.save_modern(&mut modern_buffer)?; |
| 103 |
let modern_size = modern_buffer.len(); |
| 104 |
|
| 105 |
// Save with custom options |
| 106 |
let mut custom_buffer = Vec::new(); |
| 107 |
let options = SaveOptions::builder() |
| 108 |
.use_object_streams(true) |
| 109 |
.use_xref_streams(true) |
| 110 |
.max_objects_per_stream(10) |
| 111 |
.compression_level(9) |
| 112 |
.build(); |
| 113 |
doc.save_with_options(&mut custom_buffer, options)?; |
| 114 |
let custom_size = custom_buffer.len(); |
| 115 |
|
| 116 |
// Compare sizes |
| 117 |
println!("PDF Size Comparison:"); |
| 118 |
println!("Traditional format: {} bytes", traditional_size); |
| 119 |
println!("With object streams: {} bytes", modern_size); |
| 120 |
println!("With custom options: {} bytes", custom_size); |
| 121 |
|
| 122 |
let reduction = 100.0 - (modern_size as f64 / traditional_size as f64 * 100.0); |
| 123 |
println!("\nSize reduction: {:.1}%", reduction); |
| 124 |
|
| 125 |
// Save the modern version to a file |
| 126 |
std::fs::write("object_streams_demo.pdf", &modern_buffer)?; |
| 127 |
println!("\nSaved modern PDF to: object_streams_demo.pdf"); |
| 128 |
|
| 129 |
Ok(()) |
| 130 |
} |