| 1 |
use lopdf::{Document, SaveOptions}; |
| 2 |
use std::fs::File; |
| 3 |
use std::io::Write; |
| 4 |
|
| 5 |
fn main() { |
| 6 |
println!("Testing object stream round-trip...\n"); |
| 7 |
|
| 8 |
// First, create a simple PDF with object streams |
| 9 |
let mut doc = Document::with_version("1.5"); |
| 10 |
|
| 11 |
// Add a simple page |
| 12 |
let pages_id = doc.new_object_id(); |
| 13 |
let page_id = doc.new_object_id(); |
| 14 |
let content_id = doc.new_object_id(); |
| 15 |
let font_id = doc.new_object_id(); |
| 16 |
|
| 17 |
// Page content |
| 18 |
doc.objects.insert( |
| 19 |
content_id, |
| 20 |
lopdf::Stream::new( |
| 21 |
lopdf::dictionary! {}, |
| 22 |
b"BT /F1 12 Tf 100 700 Td (Hello World) Tj ET".to_vec() |
| 23 |
).into() |
| 24 |
); |
| 25 |
|
| 26 |
// Font |
| 27 |
doc.objects.insert( |
| 28 |
font_id, |
| 29 |
lopdf::dictionary! { |
| 30 |
"Type" => "Font", |
| 31 |
"Subtype" => "Type1", |
| 32 |
"BaseFont" => "Helvetica" |
| 33 |
}.into() |
| 34 |
); |
| 35 |
|
| 36 |
// Page |
| 37 |
doc.objects.insert( |
| 38 |
page_id, |
| 39 |
lopdf::dictionary! { |
| 40 |
"Type" => "Page", |
| 41 |
"Parent" => pages_id, |
| 42 |
"MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 43 |
"Contents" => content_id, |
| 44 |
"Resources" => lopdf::dictionary! { |
| 45 |
"Font" => lopdf::dictionary! { |
| 46 |
"F1" => font_id |
| 47 |
} |
| 48 |
} |
| 49 |
}.into() |
| 50 |
); |
| 51 |
|
| 52 |
// Pages |
| 53 |
doc.objects.insert( |
| 54 |
pages_id, |
| 55 |
lopdf::dictionary! { |
| 56 |
"Type" => "Pages", |
| 57 |
"Kids" => vec![page_id.into()], |
| 58 |
"Count" => 1 |
| 59 |
}.into() |
| 60 |
); |
| 61 |
|
| 62 |
// Catalog |
| 63 |
let catalog_id = doc.add_object(lopdf::dictionary! { |
| 64 |
"Type" => "Catalog", |
| 65 |
"Pages" => pages_id |
| 66 |
}); |
| 67 |
|
| 68 |
doc.trailer.set("Root", catalog_id); |
| 69 |
|
| 70 |
// Save with object streams |
| 71 |
println!("Saving original PDF with object streams..."); |
| 72 |
let options = SaveOptions { |
| 73 |
use_object_streams: true, |
| 74 |
..Default::default() |
| 75 |
}; |
| 76 |
|
| 77 |
let mut original_bytes = Vec::new(); |
| 78 |
doc.save_with_options(&mut original_bytes, options.clone()).unwrap(); |
| 79 |
println!("Original size: {} bytes", original_bytes.len()); |
| 80 |
|
| 81 |
// Write to file for inspection |
| 82 |
let mut file = File::create("test_roundtrip_original.pdf").unwrap(); |
| 83 |
file.write_all(&original_bytes).unwrap(); |
| 84 |
|
| 85 |
// Now load it back |
| 86 |
println!("\nLoading PDF back..."); |
| 87 |
let mut loaded_doc = Document::load_mem(&original_bytes).unwrap(); |
| 88 |
println!("Loaded {} objects", loaded_doc.objects.len()); |
| 89 |
|
| 90 |
// Check for object streams |
| 91 |
let mut obj_stream_count = 0; |
| 92 |
for (id, obj) in &loaded_doc.objects { |
| 93 |
if let lopdf::Object::Stream(stream) = obj { |
| 94 |
if let Ok(type_obj) = stream.dict.get(b"Type") { |
| 95 |
if let Ok(type_name) = type_obj.as_name() { |
| 96 |
if type_name == b"ObjStm" { |
| 97 |
obj_stream_count += 1; |
| 98 |
println!("Found object stream {} 0 R", id.0); |
| 99 |
if let Ok(filter) = stream.dict.get(b"Filter") { |
| 100 |
println!(" Has Filter: {:?}", filter); |
| 101 |
} else { |
| 102 |
println!(" WARNING: No Filter!"); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
println!("Total object streams found: {}", obj_stream_count); |
| 110 |
|
| 111 |
// Save again with object streams |
| 112 |
println!("\nSaving loaded PDF with object streams again..."); |
| 113 |
let mut resaved_bytes = Vec::new(); |
| 114 |
loaded_doc.save_with_options(&mut resaved_bytes, options).unwrap(); |
| 115 |
println!("Resaved size: {} bytes", resaved_bytes.len()); |
| 116 |
|
| 117 |
// Write to file for inspection |
| 118 |
let mut file = File::create("test_roundtrip_resaved.pdf").unwrap(); |
| 119 |
file.write_all(&resaved_bytes).unwrap(); |
| 120 |
|
| 121 |
println!("\nDone! Check test_roundtrip_original.pdf and test_roundtrip_resaved.pdf"); |
| 122 |
} |