JFLib

pdf/src/edit/mod.rs#

Source: lib/pdf/src/edit/mod.rs

pdf::edit -- changing a document that pdf::read opened, saved as an incremental update (ISO
32000-1, 7.5.6): the bytes of the file stay as they are, the objects that change and the new
ones follow with a cross-reference section and a trailer of their own.  Or written again in
full (full.rs: save_full, with its encryption, decrypted, or encrypted anew).  An incremental
update (save) keeps the bytes a signature covers as they are, so it still verifies over them
(whether a certification's DocMDP permissions allow the change is the verifier's judgement:
/P 1 allows none), and an encrypted file stays encrypted with its key; save_full writes new
bytes, so signatures break (it asks for Full::break_signatures) and the encryption is what
Full::security says.

  let doc = read::Document::open(bytes)?;
  let mut up = edit::Update::new(&doc);
  up.set_field("person.name", FieldValue::Text("Jakob"))?;   // form.rs: fill in a form
  up.flatten(None)?;                                        // the fields into the pages
  let r = up.add(Object::Dict(d))?;                         // any object -> its Ref
  up.set(page_ref, Object::Dict(changed))?;                 // replace one
  let s = up.add_stream(dict, &data)?;                      // FlateDecode
  let out: Vec<u8> = up.save()?;                            // the file and the update
  let all: Vec<u8> = up.save_full(&Full::default())?;       // or a new file of it all

set_field and flatten change all they would or, when they fail, nothing.

  Update       new, document, get / resolve (as the update will have it), add / add_stream /
               add_stream_raw / set / set_stream / delete, save; set_field / flatten /
               set_fonts / set_lang (form.rs); save_full (full.rs)
  FieldValue   Text, Check, Choice
  Full         security (Security::Keep / None / Encrypt(&Encryption)), break_signatures

The section: a cross-reference stream when the newest section of the file is one, else a
table; /Prev is that section.  A file that was repaired (its cross-reference rebuilt from its
objects) gets a section that lists every object, and no /Prev: it is whole again (a stream
when objects lie in object streams).  The trailer keeps /Root, /Info and /Encrypt; /ID keeps
its first string, the second is a hash of what was added (the same update gives the same
bytes).  In an encrypted file the strings and streams of what is written are encrypted with
the document's key (crypt.rs: RC4, AES-128, AES-256); cross-reference streams, a signature's
/Contents and metadata a file keeps clear (EncryptMetadata false) are not.

An existing stream is written again only as set_stream / set gives it: its data as it is in
the file when it stays the same object (its key is its number), else decrypted first.
  • struct Update<'a> the changes to one document, written by save() as an incremental update

impl<'a> Update<'a>

  • fn new(doc: &'a Document) -> Update<'a> no changes yet
  • fn document(&self) -> &'a Document the document the update changes
  • fn get(&self, r: Ref) -> Object an object as the update will have it: changed, added, or as in the file (Null when deleted or missing; a stream added here gives its dictionary)
  • fn resolve(&self, o: &Object) -> Object follow references until an object that is none (Null after too many)
  • fn add(&mut self, o: Object) -> Result<Ref, Error> a new object (not a stream: add_stream), and its reference
  • fn add_stream(&mut self, dict: Dict, data: &[u8]) -> Result<Ref, Error> a new stream of data, compressed with FlateDecode (dict: its other keys)
  • fn add_stream_raw(&mut self, dict: Dict, encoded: Vec<u8>) -> Result<Ref, Error> a new stream whose data is encoded already as the filters of dict say
  • fn set(&mut self, r: Ref, o: Object) -> Result<(), Error> object r becomes o. A stream from the document keeps its data (decrypted first when it was another object's)
  • fn set_stream(&mut self, r: Ref, dict: Dict, data: &[u8]) -> Result<(), Error> stream r becomes data, compressed with FlateDecode (dict: its other keys)
  • fn delete(&mut self, r: Ref) object r is gone (a free entry with the next generation)
  • fn is_empty(&self) -> bool nothing changed
  • fn save(&self) -> Result<Vec<u8>, Error> the file with the update after it (the file as it was when nothing changed)

Source: docs/API.md in the JFLib repository.