Rust Requires Chilkat v11.7.0+
Rust
PDF Object Streams: generate, preserve or disable
See more PDF Optimization Examples
Demonstrates the objectStreams option. An object stream packs many small objects (dictionaries, not stream data) into one compressed stream, which is usually a worthwhile saving in a document with many pages or tags. "generate" (the default) packs them and writes a cross-reference stream; "preserve" keeps the file as it is -- packed if it already was, not packed if it was not; "disable" writes every object on its own with a classic cross-reference table, which is what a PDF 1.4 reader needs. Object streams require PDF 1.5 and are skipped for PDF/A-1.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at grand_canyon_hiking_guide.pdf
let fac = chilkat::FileAccess::new();
let json = chilkat::JsonObject::new();
let json_out = chilkat::JsonObject::new();
// generate: pack the objects (the default).
let pdf1 = chilkat::Pdf::new();
if pdf1.load_file("qa_data/pdf/grand_canyon_hiking_guide.pdf").is_err() {
println!("{}", pdf1.last_error_text());
return;
}
let _ = json.update_string("objectStreams", "generate");
if pdf1.optimize("optimize", &json, &json_out).is_err() {
println!("{}", pdf1.last_error_text());
return;
}
if pdf1.write_file("qa_output/grand_canyon_objstm_generate.pdf").is_err() {
println!("{}", pdf1.last_error_text());
return;
}
println!("generate: {} bytes, objects packed: {}", fac.file_size("qa_output/grand_canyon_objstm_generate.pdf"), json_out.int_of("actions.objectStreams.count"));
// disable: every object on its own, with a classic cross-reference table.
let pdf2 = chilkat::Pdf::new();
if pdf2.load_file("qa_data/pdf/grand_canyon_hiking_guide.pdf").is_err() {
println!("{}", pdf2.last_error_text());
return;
}
let _ = json.update_string("objectStreams", "disable");
if pdf2.optimize("optimize", &json, &json_out).is_err() {
println!("{}", pdf2.last_error_text());
return;
}
if pdf2.write_file("qa_output/grand_canyon_objstm_disable.pdf").is_err() {
println!("{}", pdf2.last_error_text());
return;
}
println!("disable: {} bytes", fac.file_size("qa_output/grand_canyon_objstm_disable.pdf"));
// preserve: leave the file's own structure alone. Use it when the output must stay readable by
// whatever reads the input today.
let pdf3 = chilkat::Pdf::new();
if pdf3.load_file("qa_data/pdf/grand_canyon_hiking_guide.pdf").is_err() {
println!("{}", pdf3.last_error_text());
return;
}
let _ = json.update_string("objectStreams", "preserve");
if pdf3.optimize("optimize", &json, &json_out).is_err() {
println!("{}", pdf3.last_error_text());
return;
}
if pdf3.write_file("qa_output/grand_canyon_objstm_preserve.pdf").is_err() {
println!("{}", pdf3.last_error_text());
return;
}
println!("preserve: {} bytes", fac.file_size("qa_output/grand_canyon_objstm_preserve.pdf"));