Expand description
Editing an opened document (§7.5.6 Incremental Updates).
An edit is a copy-on-write overlay: MutableDocument holds the changed and
deleted objects, the base Document keeps answering with the file as it was
read, and a save appends to the base’s bytes instead of rewriting them. The
model is decided in docs/adr/0006-editing-mutation-model.md.
let bytes = std::fs::read("tests/fixtures/text.pdf")?;
let base = pdf2::Document::open(bytes.clone())?;
let before = base.page_count()?;
let mut edit = pdf2::MutableDocument::new(base);
edit.add_page(b"BT /F1 12 Tf 72 720 Td (a page added by an edit) Tj ET")?;
assert_eq!(edit.page_count()?, before + 1);
assert_eq!(edit.base().page_count()?, before);
let saved = edit.save_incremental()?;
assert!(saved.starts_with(&bytes), "an update appends, it never rewrites");
let reopened = pdf2::Document::open(&saved[..])?;
assert_eq!(reopened.page_count()?, before + 1);Reading goes through the overlay first and the base second, so every reader
that takes a Document keeps working on the unedited file — the base is
never modified.
Structs§
- Mutable
Document - A document being edited, overlaid on the one that was opened.