Created
July 15, 2022 14:58
-
-
Save kotatsuyaki/b85ca82c6829c8069b87624adaff419f to your computer and use it in GitHub Desktop.
Use lopdf to add bookmarks to PDF file in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use lopdf::{Bookmark, Document, Object}; | |
fn main() { | |
let mut doc = Document::load("input.pdf").unwrap(); | |
let pages = doc.get_pages(); | |
let page_3_obj = pages.get(&3).unwrap().clone(); | |
let _bm = doc.add_bookmark( | |
Bookmark::new("Test Bookmark Title".into(), [0., 0., 1.], 0, page_3_obj), | |
None, | |
); | |
let catalog_obj_id = doc | |
.objects | |
.iter() | |
.find(|(_, obj)| obj.type_name().unwrap_or("") == "Catalog") | |
.map(|(&id, _)| id) | |
.unwrap(); | |
let outline = doc.build_outline().unwrap(); | |
let co = doc.get_object_mut(catalog_obj_id).unwrap(); | |
if let Object::Dictionary(ref mut dict) = co { | |
dict.set("Outlines", Object::Reference(outline)); | |
} | |
doc.save("output.pdf").unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment