Created
September 30, 2018 16:00
-
-
Save bn3t/1508f3526bc4ca894f818182bf23e602 to your computer and use it in GitHub Desktop.
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
#[macro_use] | |
extern crate lopdf; | |
use std::io::Result; | |
use lopdf::content::{Content, Operation}; | |
use lopdf::{Document, Object, Stream}; | |
pub fn do_it() -> Result<()> { | |
let mut doc = Document::with_version("1.5"); | |
doc.change_producer("PDFMERGE-RS"); | |
let mut input1 = Document::load("test-data/input1.pdf")?; | |
let pages = input1.get_pages(); | |
for page in pages.iter() { | |
let key = page.0; | |
let objectid = page.1; | |
println!("key: {}, objectid: {:?}", key, objectid); | |
} | |
let page_1 = pages.get(&1).unwrap(); | |
let page_1_content = input1.get_and_decode_page_content(*page_1); | |
let pages_id = doc.new_object_id(); | |
let content_id = doc.add_object(Stream::new( | |
dictionary!{}, | |
page_1_content.clone().encode().unwrap(), | |
)); | |
let page_id = doc.add_object(dictionary! { | |
"Type" => "Page", | |
"Parent" => pages_id, | |
"Contents" => content_id, | |
}); | |
let resources_id = doc.add_object(dictionary!{}); | |
let pages = dictionary! { | |
"Type" => "Pages", | |
"Kids" => vec![page_id.into()], | |
"Count" => 1, | |
"Resources" => resources_id, | |
"MediaBox" => vec![0.into(), 0.into(), 595.into(), 842.into()], | |
}; | |
doc.objects.insert(pages_id, Object::Dictionary(pages)); | |
let catalog_id = doc.add_object(dictionary! { | |
"Type" => "Catalog", | |
"Pages" => pages_id, | |
}); | |
doc.trailer.set("Root", catalog_id); | |
doc.compress(); | |
doc.save("example.pdf").unwrap(); | |
Ok(()) | |
} | |
fn main() { | |
do_it().expect("Failed pdf"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment