Skip to main content

set_info_entry

Function set_info_entry 

Source
pub fn set_info_entry(dest: &mut EditDoc<'_>, key: &Name, text: Option<&str>)
Expand description

Set or remove one text entry of the document’s /Info dictionary.

Some(text) writes key as a text string — PDFDocEncoding when every character has a byte there, otherwise UTF-16BE behind a byte-order mark — and None or an empty string removes the key. A document without an /Info gains one, as a new indirect object the saved trailer names; an /Info that is not a dictionary is replaced by one.

use std::sync::Arc;
use pdfrum_edit::{EditDoc, SaveOptions, save, set_info_entry};
use pdfrum_object::names;
use pdfrum_parser::{LoadOptions, load};

let bytes: Arc<[u8]> = Arc::from(&include_bytes!("../tests/files/hello.pdf")[..]);
let doc = load(bytes, &LoadOptions::default())?;
let mut edit = EditDoc::new(&doc);
set_info_entry(&mut edit, names::TITLE, Some("Hello"));

let mut out = Vec::new();
save(&edit, &SaveOptions::default(), &mut out)?;
let reloaded = load(Arc::from(&out[..]), &LoadOptions::default())?;
let info = reloaded.trailer().dict(names::INFO, &reloaded).expect("an /Info");
assert_eq!(info.text(names::TITLE, &reloaded).as_deref(), Some("Hello"));