Skip to content

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

Keep zero annotation flags through XFDF export/import

NetterSilver12c66f7113a3
diff --git a/crates/xfdf/src/lib.rs b/crates/xfdf/src/lib.rs
index 35bc78154..ca196fcb8 100644
--- a/crates/xfdf/src/lib.rs
+++ b/crates/xfdf/src/lib.rs
@@ -312,9 +312,7 @@ fn xfdf_annots(doc: &Document, out: &mut String) {
}
let flags = d.get(b"F").and_then(|f| doc.resolve(f).as_int()).unwrap_or(0);
let fl: Vec<&str> = FLAGS.iter().filter(|(b, _)| flags & b != 0).map(|(_, s)| *s).collect();
- if !fl.is_empty() {
- let _ = write!(attrs, " flags=\"{}\"", fl.join(","));
- }
+ let _ = write!(attrs, " flags=\"{}\"", fl.join(","));
if let Some(c) = hex(&nums_of(doc, d.get(b"C"))) {
let _ = write!(attrs, " color=\"{c}\"");
}
diff --git a/crates/xfdf/src/tests.rs b/crates/xfdf/src/tests.rs
index c33c115e7..93f23ca8c 100644
--- a/crates/xfdf/src/tests.rs
+++ b/crates/xfdf/src/tests.rs
@@ -308,6 +308,37 @@ fn xfdf_round_trips_comments_and_values() {
assert!(!only.contains("<fields>"));
}
+#[test]
+fn xfdf_round_trip_keeps_zero_flags() {
+ let square = |doc: &Document| {
+ let page = pdfcraft_model::pages(doc)[0].obj;
+ let annots = doc.get(page).as_dict().unwrap().get(b"Annots").map(|a| doc.resolve(a)).unwrap();
+ annots
+ .as_array()
+ .unwrap()
+ .iter()
+ .filter_map(Object::as_ref)
+ .find(|r| doc.get(*r).as_dict().unwrap().name(b"Subtype") == Some(b"Square"))
+ .unwrap()
+ };
+ let mut src = blank();
+ let shape = Shape::Rectangle { rect: [10.0, 10.0, 100.0, 60.0] };
+ add_annotation(
+ &mut src,
+ &NewAnnotation { page: 0, style: Style::default_for(&shape), shape, contents: String::new(), author: "A".into() },
+ &Meta { id: "s1".into(), date: None },
+ )
+ .unwrap();
+ let annot = square(&src);
+ src.update_dict(annot, |d| d.set(b"F".to_vec(), Object::Int(0))).unwrap();
+ let xfdf = export_xfdf(&src, true, false, "a.pdf");
+ let mut dst = blank();
+ import(&mut dst, xfdf.as_bytes()).unwrap();
+ let imported = square(&dst);
+ let flags = dst.get(imported).as_dict().unwrap().get(b"F").and_then(|f| dst.resolve(f).as_int());
+ assert_eq!(flags, Some(0), "flags after round trip: {xfdf}");
+}
+
#[test]
fn fdf_round_trips_values_and_comments() {
let src = filled();