You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I had a PDF fie with 58 pages of size 4.5 MB , I removed 50 pages from the file the size was like 4.4 MB for 8 pages, I did use compress and other things but he size remained unchanged.
use lopdf::dictionary;
use std::collections::BTreeMap;
use std::vec;
use lopdf::content::{Content, Operation};
use lopdf::{Bookmark, Document, Object, ObjectId, Stream};
fn main() {
let mut doc = Document::load("PATH_TO_58_page.pdf").unwrap();
doc.compress();
let count = doc.get_pages().len();
let mut page_numbers: Vec<u32> = vec![];
for i in 8..count {
page_numbers.push(i as u32);
}
doc.delete_pages(&page_numbers);
doc.compress();
doc.save("PATH_TO_58_page_new1.pdf").unwrap();
doc.compress();
let mut doc2 = Document::load("PATH_TO_58_page.pdf").unwrap();
let pages = doc2.get_pages();
for (contents_index, page) in pages {
if !page_numbers.contains(&contents_index){
println!("{}", contents_index);
let content = doc2.get_page_content(page).unwrap();
let mut stream = Stream::new(dictionary! {}, content.clone());
stream.compress().unwrap();
let stream_obj = Object::Stream(stream);
doc2.objects.insert(page, stream_obj);
}
}
doc2.compress();
doc2.save("output.pdf").unwrap();
}
The text was updated successfully, but these errors were encountered:
lopdf is awesome, thank you for this crate.
Using this example [1] I can confirm that the split pages have the same size as the original file. Just take any word document, convert it to pdf and split with that code.
I had a PDF fie with 58 pages of size 4.5 MB , I removed 50 pages from the file the size was like 4.4 MB for 8 pages, I did use compress and other things but he size remained unchanged.
use lopdf::dictionary;
use std::collections::BTreeMap;
use std::vec;
use lopdf::content::{Content, Operation};
use lopdf::{Bookmark, Document, Object, ObjectId, Stream};
fn main() {
let mut doc = Document::load("PATH_TO_58_page.pdf").unwrap();
doc.compress();
let count = doc.get_pages().len();
}
The text was updated successfully, but these errors were encountered: