use lopdf::{Document, Object, Stream, dictionary};
fn main() {
println!("Testing Filter round-trip...\n");
let mut doc = Document::with_version("1.5");
let content = b"This is test content that should be compressed";
let mut stream = Stream::new(dictionary! {
"Type" => "TestStream"
}, content.to_vec());
stream.compress().unwrap();
println!("Created stream:");
println!(" Dictionary: {:?}", stream.dict);
println!(" Has Filter: {}", stream.dict.get(b"Filter").is_ok());
println!(" Content size: {} bytes", stream.content.len());
let stream_id = doc.add_object(stream);
let catalog_id = doc.add_object(dictionary! {
"Type" => "Catalog",
"TestStream" => stream_id
});
doc.trailer.set("Root", catalog_id);
println!("\nSaving...");
let mut buffer = Vec::new();
doc.save_to(&mut buffer).unwrap();
println!("\nLoading back...");
let loaded = Document::load_mem(&buffer).unwrap();
if let Ok(Object::Stream(stream)) = loaded.get_object(stream_id) {
println!("\nLoaded stream:");
println!(" Dictionary: {:?}", stream.dict);
println!(" Has Filter: {}", stream.dict.get(b"Filter").is_ok());
println!(" Content size: {} bytes", stream.content.len());
match stream.decompressed_content() {
Ok(decompressed) => {
println!(" Decompressed successfully!");
println!(" Decompressed content: {:?}", String::from_utf8_lossy(&decompressed));
}
Err(e) => {
println!(" Failed to decompress: {}", e);
}
}
} else {
println!("ERROR: Could not find stream!");
}
}