Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Merge upstream main 0dc77fe4 into pr/attachments

Osvaldo Duran4ebf287e61ca
diff --cc crates/automation/tests/it/attachment_key.rs
index 25b0d6c60,000000000..25b0d6c60
mode 100644,000000..100644
--- a/crates/automation/tests/it/attachment_key.rs
+++ b/crates/automation/tests/it/attachment_key.rs
diff --cc crates/automation/tests/it/main.rs
index 8bb3c7781,8bb3c7781..d94aa9f7c
--- a/crates/automation/tests/it/main.rs
+++ b/crates/automation/tests/it/main.rs
@@@ -1,5 -1,5 +1,6 @@@
//! The crate's integration tests, linked into one test binary.
++mod attachment_key;
mod automation;
mod automation_roots;
mod batch;
diff --cc crates/organize/src/attachments.rs
index 3705ad9ed,000000000..b0bf22299
mode 100644,000000..100644
--- a/crates/organize/src/attachments.rs
+++ b/crates/organize/src/attachments.rs
@@@ -1,166 -1,0 +1,166 @@@
+//! Document-level file attachments (Acrobat: Edit ▸ Attach file; the Attachments panel): the
+//! `/Names /EmbeddedFiles` name tree of file specifications, each with its embedded stream.
+//!
+//! A FileAttachment annotation (a paper clip on a page) is a comment and lives in the annot
+//! crate; this is the document level only.
+
+use pdfcraft_cos::{Dict, Document, Object, PdfString, Stream};
+
+use crate::OrganizeError;
+use crate::import::{name_tree_entries, set_attachments};
+
+/// The keys of the document-level attachments, in name-tree order.
+pub fn attachment_keys(doc: &Document) -> Vec<String> {
+ entries(doc).into_iter().map(|(k, _)| PdfString::literal(k).to_text()).collect()
+}
+
+/// Attach `bytes` to the document under `name` (a file name; any folder part is dropped). The
+/// name tree's key is the name, made unique with " (2)", " (3)"… when it is already taken, and
+/// is returned so the caller can find the attachment again. `date` is a PDF date string for the
+/// embedded file's `/ModDate`; `mime` its `/Subtype` when known.
+pub fn add_attachment(
+ doc: &mut Document,
+ name: &str,
+ bytes: &[u8],
+ description: Option<&str>,
+ mime: Option<&str>,
+ date: Option<&str>,
+) -> Result<String, OrganizeError> {
+ let name = file_name(name);
+ if name.is_empty() {
+ return Err(OrganizeError::Invalid("the attachment needs a file name".into()));
+ }
+ let mut all = entries(doc);
+ let taken: Vec<String> = all.iter().map(|(k, _)| PdfString::literal(k.clone()).to_text()).collect();
+ let mut key = name.clone();
+ let mut n = 2;
+ while taken.contains(&key) {
+ key = format!("{name} ({n})");
+ n += 1;
+ }
+
+ let mut params = Dict::new();
+ params.set(b"Size".to_vec(), Object::Int(bytes.len() as i64));
+ if let Some(d) = date {
+ params.set(b"ModDate".to_vec(), Object::String(PdfString::literal(d.as_bytes().to_vec())));
+ }
+ let mut ef = Dict::new();
+ ef.set(b"Type".to_vec(), Object::name("EmbeddedFile"));
+ if let Some(m) = mime.map(str::trim).filter(|m| !m.is_empty()) {
+ ef.set(b"Subtype".to_vec(), Object::Name(m.as_bytes().to_vec()));
+ }
+ ef.set(b"Params".to_vec(), Object::Dict(params));
+ let stream = doc.add(Object::Stream(Stream::flate(ef, bytes)));
+
+ let mut spec = Dict::new();
+ spec.set(b"Type".to_vec(), Object::name("Filespec"));
+ spec.set(b"F".to_vec(), Object::String(PdfString::text(&key)));
+ spec.set(b"UF".to_vec(), Object::String(PdfString::text(&key)));
+ if let Some(d) = description.map(str::trim).filter(|d| !d.is_empty()) {
+ spec.set(b"Desc".to_vec(), Object::String(PdfString::text(d)));
+ }
+ let mut files = Dict::new();
+ files.set(b"F".to_vec(), Object::Ref(stream));
+ files.set(b"UF".to_vec(), Object::Ref(stream));
+ spec.set(b"EF".to_vec(), Object::Dict(files));
+ let spec = doc.add(spec);
+
+ all.push((PdfString::text(&key).bytes, Object::Ref(spec)));
+ set_attachments(doc, all)?;
+ Ok(key)
+}
+
+/// Remove the document-level attachment whose name-tree key is `key`. The embedded stream is
+/// left for the writer's garbage collection, as with every other removal.
+pub fn delete_attachment(doc: &mut Document, key: &str) -> Result<(), OrganizeError> {
+ let mut all = entries(doc);
+ let before = all.len();
+ all.retain(|(k, _)| PdfString::literal(k.clone()).to_text() != key);
+ if all.len() == before {
+ return Err(OrganizeError::Invalid(format!("there is no attachment named {key}")));
+ }
+ if all.is_empty() { clear_embedded_files(doc) } else { set_attachments(doc, all) }
+}
+
+/// The `/EmbeddedFiles` entries (key, file specification), in tree order.
+fn entries(doc: &Document) -> Vec<(Vec<u8>, Object)> {
+ let Some(tree) = doc
+ .root()
+ .and_then(|r| doc.get(r).as_dict().cloned())
+ .and_then(|c| c.get(b"Names").map(|n| doc.resolve(n)))
+ .and_then(|n| n.as_dict().and_then(|d| d.get(b"EmbeddedFiles").cloned()))
+ else {
+ return Vec::new();
+ };
+ let mut out = Vec::new();
- name_tree_entries(doc, &tree, 0, &mut out);
++ name_tree_entries(doc, &tree, 0, &mut std::collections::HashSet::new(), &mut out);
+ out
+}
+
+/// Drop `/EmbeddedFiles` from the catalog's `/Names` (and `/Names` itself when nothing is left).
+fn clear_embedded_files(doc: &mut Document) -> Result<(), OrganizeError> {
+ let root = doc.root().ok_or(OrganizeError::NoPageTree)?;
+ let names = doc.get(root).as_dict().and_then(|c| c.get(b"Names").cloned());
+ match names {
+ Some(Object::Ref(r)) => {
+ let mut empty = false;
+ doc.update_dict(r, |n| {
+ n.remove(b"EmbeddedFiles");
+ empty = n.is_empty();
+ })?;
+ if empty {
+ doc.update_dict(root, |c| {
+ c.remove(b"Names");
+ })?;
+ }
+ }
+ Some(Object::Dict(mut n)) => {
+ n.remove(b"EmbeddedFiles");
+ doc.update_dict(root, |c| {
+ if n.is_empty() {
+ c.remove(b"Names");
+ } else {
+ c.set(b"Names".to_vec(), Object::Dict(n));
+ }
+ })?;
+ }
+ _ => {}
+ }
+ Ok(())
+}
+
+/// The last path component of `name`, trimmed: a dragged-in path or a folder part never becomes
+/// the attachment's name.
+fn file_name(name: &str) -> String {
+ name.rsplit(['/', '\\']).next().unwrap_or(name).trim().to_string()
+}
+
+/// A media type from the file's extension, for the embedded file's `/Subtype`; `None` when it
+/// is not one of the common kinds (the attachment is still fine without it).
+pub fn mime_for_name(name: &str) -> Option<&'static str> {
+ let ext = name.rsplit_once('.').map(|(_, e)| e.to_ascii_lowercase())?;
+ Some(match ext.as_str() {
+ "pdf" => "application/pdf",
+ "txt" | "log" | "md" => "text/plain",
+ "csv" => "text/csv",
+ "htm" | "html" => "text/html",
+ "xml" => "text/xml",
+ "json" => "application/json",
+ "zip" => "application/zip",
+ "png" => "image/png",
+ "jpg" | "jpeg" => "image/jpeg",
+ "gif" => "image/gif",
+ "tif" | "tiff" => "image/tiff",
+ "svg" => "image/svg+xml",
+ "doc" => "application/msword",
+ "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
+ "xls" => "application/vnd.ms-excel",
+ "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
+ "ppt" => "application/vnd.ms-powerpoint",
+ "pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
+ "rtf" => "application/rtf",
+ "eml" => "message/rfc822",
+ "msg" => "application/vnd.ms-outlook",
+ _ => return None,
+ })
+}
diff --cc crates/organize/src/import.rs
index ea7f78f97,1162a810c..76f5e3266
--- a/crates/organize/src/import.rs
+++ b/crates/organize/src/import.rs
@@@ -560,14 -566,16 +566,16 @@@ fn name_tree_lookup(src: &Document, nod
}
}
let kids = d.get(b"Kids").map(|k| src.resolve(k)).and_then(|k| k.as_array().cloned()).unwrap_or_default();
- kids.iter().find_map(|k| name_tree_lookup(src, k, key, depth + 1))
+ kids.iter().find_map(|k| name_tree_lookup(src, k, key, depth + 1, seen))
}
- /// All (key, value) pairs of a name tree, in order.
- pub(crate) fn name_tree_entries(src: &Document, node: &Object, depth: u8, out: &mut Vec<(Vec<u8>, Object)>) {
- if depth > 32 {
+ /// All (key, value) pairs of a name tree, in order, each node read once.
-fn name_tree_entries(src: &Document, node: &Object, depth: u8, seen: &mut HashSet<ObjRef>, out: &mut Vec<(Vec<u8>, Object)>) {
++pub(crate) fn name_tree_entries(src: &Document, node: &Object, depth: u8, seen: &mut HashSet<ObjRef>, out: &mut Vec<(Vec<u8>, Object)>) {
+ if depth > 32 || node.as_ref().is_some_and(|r| !seen.insert(r)) {
return;
}
+ #[cfg(test)]
+ crate::work(1);
let node = src.resolve(node);
let Some(d) = node.as_dict() else { return };
if let Some(names) = d.get(b"Names").map(|n| src.resolve(n)).and_then(|n| n.as_array().cloned()) {
diff --cc crates/ui-egui/tests/it/attach_picker_target.rs
index ac1aed736,000000000..ac1aed736
mode 100644,000000..100644
--- a/crates/ui-egui/tests/it/attach_picker_target.rs
+++ b/crates/ui-egui/tests/it/attach_picker_target.rs
diff --cc crates/ui-egui/tests/it/main.rs
index 7c04df20e,7c04df20e..93a9fcf89
--- a/crates/ui-egui/tests/it/main.rs
+++ b/crates/ui-egui/tests/it/main.rs
@@@ -20,6 -20,6 +20,7 @@@ fn gpu() -> std::sync::MutexGuard<'stat
mod accessibility;
mod actions;
mod animate;
++mod attach_picker_target;
mod bookmark_position;
mod bookmarks;
mod bulk_fields;