pub struct DocumentBuilder { /* private fields */ }Expand description
Builds a new PDF 2.0 file.
Obtained from Document::new or DocumentBuilder::new; the catalog
(object 1) and the page tree root (object 2) exist from the start.
Implementations§
Source§impl DocumentBuilder
impl DocumentBuilder
Sourcepub fn add_object(&mut self, object: Object) -> u32
pub fn add_object(&mut self, object: Object) -> u32
Add an object and return the number it was assigned.
Sourcepub fn add_page(&mut self, media_box: Rect, content: impl Into<Vec<u8>>) -> u32
pub fn add_page(&mut self, media_box: Rect, content: impl Into<Vec<u8>>) -> u32
Append a page with content as its content stream.
Every page gets /Resources << /Font << /F1 … >> >> pointing at one
shared Helvetica font, so written content streams that use /F1 are
immediately extractable.
Examples found in repository?
examples/write.rs (lines 10-13)
8fn main() -> Result<(), pdf2::Error> {
9 let mut document = Document::new();
10 document.add_page(
11 Rect::new(0.0, 0.0, 612.0, 792.0),
12 "BT /F1 24 Tf 72 700 Td (Hello from pdf2) Tj ET",
13 );
14 let bytes = document.save()?;
15 std::fs::write("hello.pdf", &bytes)?;
16
17 let reopened = Document::open(bytes)?;
18 let page = reopened.page(0)?;
19 println!(
20 "hello.pdf: PDF {}, {} page(s), {}",
21 reopened.version(),
22 reopened.page_count()?,
23 reopened.extract_text(&page)?
24 );
25 Ok(())
26}Sourcepub fn page_count(&self) -> usize
pub fn page_count(&self) -> usize
Number of pages appended so far.
Sourcepub fn set_metadata(&mut self, xmp: impl Into<Vec<u8>>)
pub fn set_metadata(&mut self, xmp: impl Into<Vec<u8>>)
Replace the XMP metadata packet written into the catalog.
Sourcepub fn objects(&self) -> impl Iterator<Item = (u32, &Object)>
pub fn objects(&self) -> impl Iterator<Item = (u32, &Object)>
Iterate over the objects collected so far, in object-number order.
Sourcepub fn save(&self) -> Result<Vec<u8>>
pub fn save(&self) -> Result<Vec<u8>>
Serialize the document as a PDF 2.0 file.
Examples found in repository?
examples/write.rs (line 14)
8fn main() -> Result<(), pdf2::Error> {
9 let mut document = Document::new();
10 document.add_page(
11 Rect::new(0.0, 0.0, 612.0, 792.0),
12 "BT /F1 24 Tf 72 700 Td (Hello from pdf2) Tj ET",
13 );
14 let bytes = document.save()?;
15 std::fs::write("hello.pdf", &bytes)?;
16
17 let reopened = Document::open(bytes)?;
18 let page = reopened.page(0)?;
19 println!(
20 "hello.pdf: PDF {}, {} page(s), {}",
21 reopened.version(),
22 reopened.page_count()?,
23 reopened.extract_text(&page)?
24 );
25 Ok(())
26}Trait Implementations§
Source§impl Clone for DocumentBuilder
impl Clone for DocumentBuilder
Source§impl Debug for DocumentBuilder
impl Debug for DocumentBuilder
Auto Trait Implementations§
impl Forget for DocumentBuilder
impl Freeze for DocumentBuilder
impl Move for DocumentBuilder
impl RefUnwindSafe for DocumentBuilder
impl Send for DocumentBuilder
impl Sync for DocumentBuilder
impl Unpin for DocumentBuilder
impl UnsafeUnpin for DocumentBuilder
impl UnwindSafe for DocumentBuilder
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