Struct printpdf::PdfPageReference
source · pub struct PdfPageReference {
pub document: Weak<RefCell<PdfDocument>>,
pub page: PdfPageIndex,
}
Expand description
A “reference” to the current page, allows for inner mutability but only inside this library
Fields§
§document: Weak<RefCell<PdfDocument>>
A weak reference to the document, for inner mutability
page: PdfPageIndex
The index of the page this layer is on
Implementations§
source§impl PdfPageReference
impl PdfPageReference
sourcepub fn add_layer<S>(&self, layer_name: S) -> PdfLayerReference
pub fn add_layer<S>(&self, layer_name: S) -> PdfLayerReference
Adds a page and returns the index of the currently added page
Examples found in repository?
examples/bookmark.rs (line 12)
7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let (doc, page1, _) = PdfDocument::new("printpdf page test", Mm(210.0), Mm(297.0), "Layer 1");
doc.add_bookmark("This is a bookmark", page1);
let (page2, _) = doc.add_page(Mm(297.0), Mm(210.0), "Page 2, Layer 1");
let _ = doc.get_page(page2).add_layer("Layer 3");
doc.add_bookmark("This is another bookmark", page2);
// If this is successful, you should see a PDF with two blank A4 pages and 2 bookmarks
doc.save(&mut BufWriter::new(
File::create("test_bookmark.pdf").unwrap(),
))
.unwrap();
}
More examples
examples/page.rs (line 16)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
// To prevent empty documents, you must specify at least one page with one layer
// You can later on add more pages with the add_page() function
// You also have to specify the title of the PDF and the document creator
let (doc, _, _) = PdfDocument::new("printpdf page test", Mm(210.0), Mm(297.0), "Layer 1");
// You can add more pages and layers to the PDF.
// Just make sure you don't lose the references, otherwise, you can't add things to the layer anymore
let (page2, _) = doc.add_page(Mm(297.0), Mm(210.0), "Page 2, Layer 1");
let _ = doc.get_page(page2).add_layer("Layer 3");
// If this is successful, you should see a PDF with two blank A4 pages
doc.save(&mut BufWriter::new(File::create("test_pages.pdf").unwrap()))
.unwrap();
}
sourcepub fn get_layer(&self, layer: PdfLayerIndex) -> PdfLayerReference
pub fn get_layer(&self, layer: PdfLayerIndex) -> PdfLayerReference
Validates that a layer is present and returns a reference to it
Examples found in repository?
examples/rect.rs (line 11)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let (doc, page1, layer1) =
PdfDocument::new("printpdf rect test", Mm(210.0), Mm(297.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);
let rect = Rect::new(Mm(30.), Mm(250.), Mm(200.), Mm(290.));
current_layer.add_rect(rect);
let rect = Rect::new(Mm(50.), Mm(180.), Mm(120.), Mm(290.))
.with_mode(PaintMode::Clip)
.with_winding(WindingOrder::EvenOdd);
current_layer.add_rect(rect);
let mut font_reader =
std::io::Cursor::new(include_bytes!("../assets/fonts/RobotoMedium.ttf").as_ref());
let font = doc.add_external_font(&mut font_reader).unwrap();
current_layer.use_text("hello world", 100.0, Mm(10.0), Mm(200.0), &font);
doc.save(&mut BufWriter::new(File::create("test_rect.pdf").unwrap()))
.unwrap();
}
More examples
examples/hyperlink.rs (line 10)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
fn main() {
let (doc, page1, layer1) =
PdfDocument::new("printpdf graphics test", Mm(595.276), Mm(841.89), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);
let text = "Lorem ipsum";
let font = doc.add_builtin_font(BuiltinFont::TimesBoldItalic).unwrap();
current_layer.use_text(text, 48.0, Mm(10.0), Mm(200.0), &font);
current_layer.add_link_annotation(LinkAnnotation::new(
printpdf::Rect::new(Mm(10.0), Mm(200.0), Mm(100.0), Mm(212.0)),
Some(printpdf::BorderArray::default()),
Some(printpdf::ColorArray::default()),
printpdf::Actions::uri("https://www.google.com/".to_string()),
Some(printpdf::HighlightingMode::Invert),
));
doc.save(&mut BufWriter::new(
File::create("test_hyperlink.pdf").unwrap(),
))
.unwrap();
}
examples/svg.rs (line 11)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
fn main() {
let (doc, page1, layer1) =
PdfDocument::new("printpdf graphics test", Mm(210.0), Mm(297.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);
let svg = Svg::parse(SVG).unwrap();
let rotation_center_x = Px((svg.width.0 as f32 / 2.0) as usize);
let rotation_center_y = Px((svg.height.0 as f32 / 2.0) as usize);
let reference = svg.into_xobject(¤t_layer);
for i in 0..10 {
reference.clone().add_to_layer(
¤t_layer,
SvgTransform {
rotate: Some(SvgRotation {
angle_ccw_degrees: i as f32 * 36.0,
rotation_center_x: rotation_center_x.into_pt(300.0),
rotation_center_y: rotation_center_y.into_pt(300.0),
}),
translate_x: Some(Mm(i as f32 * 20.0 % 50.0).into()),
translate_y: Some(Mm(i as f32 * 30.0).into()),
..Default::default()
},
);
}
let pdf_bytes = doc.save_to_bytes().unwrap();
std::fs::write("test_svg.pdf", &pdf_bytes).unwrap();
}
examples/circle.rs (line 11)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
fn main() {
let (doc, page1, layer1) =
PdfDocument::new("printpdf circle test", Mm(210.0), Mm(297.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);
let radius_1 = Pt(40.0);
let radius_2 = Pt(30.0);
let offset_x = Pt(10.0);
let offset_y = Pt(50.0);
let line = Polygon {
rings: vec![
calculate_points_for_circle(radius_1, offset_x, offset_y),
calculate_points_for_circle(radius_2, offset_x, offset_y), // hole
],
mode: PaintMode::FillStroke,
winding_order: WindingOrder::EvenOdd,
};
current_layer.add_polygon(line);
let scale_x_rect = Pt(40.0);
let scale_y_rect = Pt(10.0);
let offset_x_rect = Pt(20.0);
let offset_y_rect = Pt(5.0);
let line = Polygon {
rings: vec![calculate_points_for_rect(
scale_x_rect,
scale_y_rect,
offset_x_rect,
offset_y_rect,
)],
mode: PaintMode::FillStroke,
winding_order: WindingOrder::NonZero,
};
current_layer.add_polygon(line);
doc.save(&mut BufWriter::new(
File::create("test_circle.pdf").unwrap(),
))
.unwrap();
}
examples/image.rs (line 12)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn main() {
let (doc, page1, layer1) =
PdfDocument::new("printpdf graphics test", Mm(210.0), Mm(297.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);
// currently, the only reliable file formats are bmp/jpeg/png
// this is an issue of the image library, not a fault of printpdf
let image_bytes = include_bytes!("../assets/img/BMP_test.bmp");
let mut reader = Cursor::new(image_bytes.as_ref());
let decoder = BmpDecoder::new(&mut reader).unwrap();
let image = Image::try_from(decoder).unwrap();
let rotation_center_x = Px((image.image.width.0 as f32 / 2.0) as usize);
let rotation_center_y = Px((image.image.height.0 as f32 / 2.0) as usize);
// layer,
image.add_to_layer(
current_layer.clone(),
ImageTransform {
rotate: Some(ImageRotation {
angle_ccw_degrees: 45.0,
rotation_center_x,
rotation_center_y,
}),
translate_x: Some(Mm(10.0)),
translate_y: Some(Mm(10.0)),
..Default::default()
},
);
doc.save(&mut BufWriter::new(File::create("test_image.pdf").unwrap()))
.unwrap();
}
examples/image_alpha.rs (line 12)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn main() {
let (doc, page1, layer1) =
PdfDocument::new("printpdf graphics test", Mm(210.0), Mm(297.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);
// currently, the only reliable file formats are bmp/jpeg/png
// this is an issue of the image library, not a fault of printpdf
let image_bytes = include_bytes!("../assets/img/dog_alpha.png");
let mut reader = Cursor::new(image_bytes.as_ref());
let decoder = PngDecoder::new(&mut reader).unwrap();
let image = Image::try_from(decoder).unwrap();
let rotation_center_x = Px((image.image.width.0 as f32 / 2.0) as usize);
let rotation_center_y = Px((image.image.height.0 as f32 / 2.0) as usize);
// layer,
image.add_to_layer(
current_layer.clone(),
ImageTransform {
rotate: Some(ImageRotation {
angle_ccw_degrees: 0.0,
rotation_center_x,
rotation_center_y,
}),
translate_x: Some(Mm(50.0)),
translate_y: Some(Mm(50.0)),
..Default::default()
},
);
doc.save(&mut BufWriter::new(File::create("test_image.pdf").unwrap()))
.unwrap();
}
Additional examples can be found in:
sourcepub fn extend_with(&self, dict: Dictionary)
pub fn extend_with(&self, dict: Dictionary)
Examples found in repository?
examples/annotations.rs (line 38)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
let (doc, page1, layer1) =
PdfDocument::new("printpdf graphics test", Mm(210.0), Mm(297.0), "Layer 1");
let page = doc.get_page(page1);
let current_layer = page.get_layer(layer1);
let action = Dictionary::from_iter(vec![
("Type", "Action".into()),
("S", Object::Name(b"URI".to_vec())),
("URI", Object::String(b"https://github.com/fschutt/printpdf".to_vec(), Literal)),
]);
let annotation = Dictionary::from_iter(vec![
("Type", "Annot".into()),
("Subtype", Object::Name(b"Link".to_vec())),
("Rect", vec![20.into(), 580.into(), 300.into(), 560.into()].into()),
("C", vec![].into()),
("Contents", Object::String("Hello World".into(), Literal)),
("A", action.into()),
]);
let annotations = Dictionary::from_iter(vec![
("Annots", Object::Array(vec![annotation.into()]))
]);
page.extend_with(annotations);
let text = "There's an invisible annotation with a link covering this text.";
let font = doc.add_builtin_font(BuiltinFont::Helvetica).unwrap();
current_layer.use_text(text, 10.0, Mm(10.0), Mm(200.0), &font);
doc.save(&mut BufWriter::new(File::create("test_annotations.pdf").unwrap()))
.unwrap();
}
Auto Trait Implementations§
impl !RefUnwindSafe for PdfPageReference
impl !Send for PdfPageReference
impl !Sync for PdfPageReference
impl Unpin for PdfPageReference
impl !UnwindSafe for PdfPageReference
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more