-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
782 additions
and
89 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "pdf-examples" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
pdf = { path = "../pdf" } | ||
datasize = "0.2.13" | ||
clap = { version = "*", features = ["derive"] } | ||
image = "*" | ||
|
||
[[bin]] | ||
name = "extract_page" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::{path::PathBuf, io::BufReader, fs::File, error::Error}; | ||
|
||
use pdf::{ | ||
error::PdfError, | ||
file::FileOptions, | ||
object::*, | ||
build::*, | ||
primitive::{PdfString, Name}, enc::{StreamFilter, DCTDecodeParams}, content::{Op, Matrix, Content}, | ||
}; | ||
|
||
use clap::Parser; | ||
use std::io::Cursor; | ||
use image::io::Reader as ImageReader; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(author, version, about, long_about = None)] | ||
struct Args { | ||
/// Input file | ||
#[arg(short, long)] | ||
input: PathBuf, | ||
|
||
/// Page number | ||
#[arg(long)] | ||
image: PathBuf, | ||
|
||
/// Page number to add the image to | ||
#[arg(short, long, default_value_t = 0)] | ||
page: u32, | ||
|
||
/// Output file | ||
#[arg(short, long)] | ||
output: PathBuf, | ||
} | ||
|
||
struct Point { | ||
x: f32, | ||
y: f32 | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn Error>> { | ||
let args = Args::parse(); | ||
|
||
let img_data = std::fs::read(&args.image)?; | ||
let img = ImageReader::new(Cursor::new(&img_data)).with_guessed_format()?.decode()?; | ||
let image_dict = ImageDict { | ||
width: img.width(), | ||
height: img.height(), | ||
color_space: Some(ColorSpace::DeviceRGB), | ||
bits_per_component: Some(8), | ||
.. Default::default() | ||
}; | ||
let image = Stream::new_with_filters(image_dict, img_data, vec![StreamFilter::DCTDecode(DCTDecodeParams { color_transform: None})]); | ||
|
||
let mut file = FileOptions::cached().open(&args.input).unwrap(); | ||
let page = file.get_page(args.page).expect("no such page"); | ||
|
||
let resources = page.resources()?; | ||
let mut resources2: Resources = (**resources).clone(); | ||
|
||
let image_obj = XObject::Image(ImageXObject { inner: image }); | ||
let image_ref = file.create(image_obj)?; | ||
|
||
// assume that name did not exist | ||
let image_name = Name::from("MyImage"); | ||
resources2.xobjects.insert(image_name.clone(), image_ref.get_ref()); | ||
|
||
|
||
let mut ops = page.contents.as_ref().unwrap().operations(&file.resolver())?; | ||
|
||
let scale = Point { x: img.width() as f32, y: img.height() as f32 }; | ||
let skew = Point { x: 0.0, y: 0.0 }; | ||
let position = Point { x: 100., y: 100. }; | ||
|
||
ops.append(&mut vec![ | ||
Op::Save, // ADD IMAGE START | ||
Op::Transform { matrix: Matrix{ // IMAGE MANIPULATION | ||
a: scale.x * 0.1, d: scale.y * 0.1, | ||
b: skew.x, c: skew.y, | ||
e: position.x, f: position.y, | ||
} }, | ||
Op::XObject {name: image_name}, // IMAGE | ||
Op::Restore, // ADD IMAGE STOP | ||
]); | ||
|
||
let mut page2: Page = (*page).clone(); | ||
page2.contents = Some(Content::from_ops(ops)); | ||
page2.resources = Some(file.create(resources2)?.into()); | ||
|
||
file.update(page.get_ref().get_inner(), page2)?; | ||
|
||
file.save_to(&args.output)?; | ||
|
||
Ok(()) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::path::PathBuf; | ||
|
||
use pdf::{ | ||
error::PdfError, | ||
file::FileOptions, | ||
object::*, | ||
build::*, | ||
primitive::PdfString, | ||
}; | ||
|
||
use clap::Parser; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(author, version, about, long_about = None)] | ||
struct Args { | ||
/// Input file | ||
#[arg(short, long)] | ||
input: PathBuf, | ||
|
||
/// Page number | ||
#[arg(short, long, default_value_t = 0)] | ||
page: u32, | ||
|
||
/// Output file | ||
#[arg(short, long)] | ||
output: PathBuf, | ||
} | ||
|
||
fn main() -> Result<(), PdfError> { | ||
let args = Args::parse(); | ||
|
||
let old_file = FileOptions::cached().open(&args.input).unwrap(); | ||
let old_page = old_file.get_page(args.page).expect("no such page"); | ||
|
||
let mut builder = PdfBuilder::new(FileOptions::cached()); | ||
|
||
let mut importer = Importer::new(old_file.resolver(), &mut builder.storage); | ||
let mut pages = Vec::new(); | ||
|
||
let new_page = PageBuilder::clone_page(&old_page, &mut importer)?; | ||
|
||
pages.push(new_page); | ||
|
||
let catalog = CatalogBuilder::from_pages(pages); | ||
|
||
let mut info = InfoDict::default(); | ||
info.title = Some(PdfString::from("test")); | ||
|
||
let data = builder.info(info).build(catalog)?; | ||
|
||
std::fs::write(&args.output, data)?; | ||
|
||
Ok(()) | ||
} |
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
Oops, something went wrong.