use hipdf::lopdf::{
content::{Content, Operation},
dictionary, Document,
};
use hipdf::ocg::{Layer, OCGManager};
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("🚀 Creating basic PDF with hipdf...");
let mut doc = Document::with_version("1.7");
let mut ocg_manager = OCGManager::with_config(Default::default());
ocg_manager.add_layer(Layer::new("Background", true));
ocg_manager.add_layer(Layer::new("Main Content", true));
ocg_manager.add_layer(Layer::new("Overlay", false));
ocg_manager.initialize(&mut doc);
let pages_id = doc.add_object(dictionary! {
"Type" => "Pages",
});
let mut operations = Vec::new();
operations.extend(vec![
Operation::new("BT", vec![]),
Operation::new(
"Tf",
vec![hipdf::lopdf::Object::Name(b"F1".to_vec()), 12.into()],
),
Operation::new("Td", vec![50.into(), 750.into()]),
Operation::new(
"Tj",
vec![hipdf::lopdf::Object::string_literal("Hello from hipdf!")],
),
Operation::new("ET", vec![]),
]);
operations.extend(vec![
Operation::new("q", vec![]),
Operation::new("0.8", vec![]),
Operation::new("w", vec![]),
Operation::new("re", vec![100.into(), 700.into(), 200.into(), 100.into()]),
Operation::new("S", vec![]),
Operation::new("Q", vec![]),
]);
let content = Content { operations };
let content_id = doc.add_object(hipdf::lopdf::Stream::new(dictionary! {}, content.encode()?));
let page_id = doc.add_object(dictionary! {
"Type" => "Page",
"Parent" => pages_id,
"MediaBox" => vec![0.into(), 0.into(), 595.into(), 842.into()],
"Contents" => content_id,
});
doc.get_object_mut(pages_id)
.and_then(hipdf::lopdf::Object::as_dict_mut)
.unwrap()
.set("Kids", vec![hipdf::lopdf::Object::Reference(page_id)]);
doc.get_object_mut(pages_id)
.and_then(hipdf::lopdf::Object::as_dict_mut)
.unwrap()
.set("Count", 1);
let catalog_id = doc.add_object(dictionary! {
"Type" => "Catalog",
"Pages" => hipdf::lopdf::Object::Reference(pages_id),
});
doc.trailer
.set("Root", hipdf::lopdf::Object::Reference(catalog_id));
let output_path = "examples/basic_example.pdf";
doc.save(output_path)?;
println!("✅ Basic PDF created successfully: {}", output_path);
println!("📊 Layers created: {}", ocg_manager.len());
println!("🔧 Features tested:");
println!(" • PDF document creation");
println!(" • Optional Content Groups (layers)");
println!(" • Content operations (text, shapes)");
println!(" • Document structure");
Ok(())
}