pdf: non-ASCII form and annotation text is no longer written as UTF-8 mojibake
- Commit
91012fe4397e6b3508381c37c9140cc5b3c4be8d- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 03:53 UTC (5 days ago)
- Parents
9a84c47
set_text_field/set_choice_field wrote /V as raw UTF-8 bytes,
which a PDF text string may never be (it must be
PDFDocEncoding or UTF-16BE): 'café — €' went in as C3 A9 /
E2 80 94 / E2 82 AC and read back — including by this
crate's own Latin-1-fallback decoder — as mojibake. The
appearance stream had the same defect, pushing Rust chars
verbatim into a WinAnsiEncoding simple font (one byte per
glyph). The Kotlin/JNI bridge is a faithful Unicode
pass-through and needed no change.
The Rust layer now owns encoding: encode_pdf_text emits
PDFDocEncoding (Annex D.3 table, validated against lopdf's
own decoder) when every character is representable, else
UTF-16BE with BOM — never UTF-8. decode_pdf_text decodes
non-BOM bytes as true PDFDocEncoding instead of the Latin-1
approximation, and escape_pdf_literal emits WinAnsi bytes as
octal escapes ('?' for glyphs the font cannot represent; the
stored /V stays lossless and /NeedAppearances is already
set). The same helper now serves the annotation Contents
writers, which had the identical bug; the README's known
limitation is narrowed accordingly. Four new tests check
the exact bytes, the UTF-16BE path, choice fields and a
cross-check against lopdf's spec decoder (all failed
pre-fix). Verified: cargo test 671 passed; :pdf:testDevUnitTest
305 tests pass.Diff
pdf/src/main/rust/README.md
diff --git a/pdf/src/main/rust/README.md b/pdf/src/main/rust/README.mdindex c13e4ee3c8..794a9a8a04 100644--- a/pdf/src/main/rust/README.md+++ b/pdf/src/main/rust/README.md@@ -463,11 +463,12 @@ slight shift in the darkest tones rather than missing content. the viewer to build its own appearance (`/NeedAppearances` set, no `/AP`) renders as nothing at all rather than as a box showing its value, because appearance generation runs only when the user edits a field, not on load.-- **Non-ASCII text typed into a form field or text annotation is mangled in the- saved file**: it displays correctly inside this app, but the generated- appearance stream writes UTF-8 bytes into a Latin-1 encoded font, so every other- viewer shows `é` where `é` was typed. The text is not lost, but the file is- wrong everywhere except here.+- **Text beyond WinAnsi typed into a form field or text annotation shows as+ `?` in the generated appearance**: field values and annotation contents are+ stored losslessly (PDFDocEncoding, or UTF-16BE where PDFDoc cannot hold+ them), but the generated appearance paints with a WinAnsi simple font, so a+ character it has no glyph for (CJK, emoji) is substituted in the baked+ appearance stream. - **Overlapping strokes in a translucent annotation darken where they cross**: a semi-transparent Ink or Highlight annotation whose own strokes overlap is composited per-stroke rather than as a single group, so intersections come out
pdf/src/main/rust/src/annotations_part3.rs
diff --git a/pdf/src/main/rust/src/annotations_part3.rs b/pdf/src/main/rust/src/annotations_part3.rsindex 712c6db33c..030ce3c41e 100644--- a/pdf/src/main/rust/src/annotations_part3.rs+++ b/pdf/src/main/rust/src/annotations_part3.rs@@ -233,9 +233,16 @@ pub(crate) fn normalize_rect(r: [f64; 4]) -> [f64; 4] { ] } -/// Escape a string for PDF literal — full escapes per spec §7.3.4.2: \n \r \t \b \f ( ) \-/// Previous only escaped ( ) \ causing invalid streams for FreeText with newlines.+/// Escape a string for a PDF literal in a generated appearance stream.+/// The bytes inside `(...) Tj` are font codes, not a text string: with the+/// WinAnsi simple font they must be single WinAnsi bytes, so a non-ASCII+/// character goes out as an octal escape for its WinAnsi byte (é -> `\351`,+/// € -> `\200`) and one the font cannot represent as `?`. Writing the Rust+/// string verbatim put UTF-8 bytes in the stream, which the font then+/// painted as two or three unrelated glyphs (`café`). pub(crate) fn escape_pdf_literal(s: &str) -> String {+ // Reverse WinAnsi map (char -> byte), built at most once per call.+ let mut win_rev: Option<HashMap<char, u8>> = None; let mut out = String::with_capacity(s.len()); for c in s.chars() { match c {@@ -247,14 +254,25 @@ pub(crate) fn escape_pdf_literal(s: &str) -> String { '\t' => out.push_str("\\t"), '\u{0008}' => out.push_str("\\b"), '\u{000C}' => out.push_str("\\f"),- _ => out.push(c),+ c if c.is_ascii() && !c.is_control() => out.push(c),+ c => {+ let rev = win_rev.get_or_insert_with(|| {+ encoding::win_ansi().iter().map(|(&code, &ch)| (ch, code as u8)).collect()+ });+ match rev.get(&c) {+ Some(b) => out.push_str(&format!("\\{b:03o}")),+ None => out.push('?'),+ }+ } } } out } -/// Decode PDF text string: handles BE BOM FE FF and LE BOM FF FE, else Latin-1/PDFDoc approximation.-/// Fix: previously only BE, not LE.++/// Decode PDF text string (§7.9.2): UTF-16BE with FE FF BOM (FF FE LE is+/// also accepted, as before), otherwise PDFDocEncoding. Bytes PDFDoc leaves+/// undefined keep their Latin-1 value, preserving prior behaviour for them. pub(crate) fn decode_pdf_text(bytes: &[u8]) -> String { if bytes.len() >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF { let units: Vec<u16> = bytes[2..].chunks(2).map(|c| ((c[0] as u16) << 8) | *c.get(1).unwrap_or(&0) as u16).collect();@@ -263,11 +281,7 @@ pub(crate) fn decode_pdf_text(bytes: &[u8]) -> String { let units: Vec<u16> = bytes[2..].chunks(2).map(|c| (c[0] as u16) | ((*c.get(1).unwrap_or(&0) as u16) << 8)).collect(); String::from_utf16_lossy(&units) } else {- // Not a UTF-16 string, so treat the bytes as Latin-1. PDFDocEncoding- // (§7.9.2.2) differs from Latin-1 only in 0x18-0x1F and 0x80-0x9F, so- // this is an approximation: a true PDFDocEncoding table would map those- // ranges, and WinAnsiEncoding would map 0x80-0x9F differently again.- bytes.iter().map(|&b| b as char).collect()+ bytes.iter().map(|&b| pdfdoc_char(b).unwrap_or(b as char)).collect() } } @@ -422,7 +436,7 @@ pub(crate) fn add_free_text( let mut annot = Dictionary::new(); annot.set("Type", name_obj("Annot")); annot.set("Subtype", name_obj("FreeText"));- annot.set("Contents", Object::string_literal(text));+ annot.set("Contents", encode_pdf_text(text)); annot.set( "DA", Object::string_literal(format!("{cr:.3} {cg:.3} {cb:.3} rg /F1 {size} Tf")),
pdf/src/main/rust/src/annotations_part4.rs
diff --git a/pdf/src/main/rust/src/annotations_part4.rs b/pdf/src/main/rust/src/annotations_part4.rsindex ed316558a8..8b7f3b91ec 100644--- a/pdf/src/main/rust/src/annotations_part4.rs+++ b/pdf/src/main/rust/src/annotations_part4.rs@@ -121,7 +121,7 @@ pub(crate) fn add_note(handle: i64, page_index: i32, x: f64, y: f64, argb: u32, annot.set("Type", name_obj("Annot")); annot.set("Subtype", name_obj("Text")); annot.set("Name", name_obj("Note"));- annot.set("Contents", Object::string_literal(text));+ annot.set("Contents", encode_pdf_text(text)); annot.set("C", Object::Array(vec![cr.into(), cg.into(), cb.into()])); set_appearance(&mut annot, r, ap_id); add_annotation_object(doc, page_index, annot)@@ -195,7 +195,7 @@ pub(crate) fn add_callout( annot.set("Type", name_obj("Annot")); annot.set("Subtype", name_obj("FreeText")); annot.set("IT", name_obj("FreeTextCallout"));- annot.set("Contents", Object::string_literal(text));+ annot.set("Contents", encode_pdf_text(text)); annot.set( "DA", Object::string_literal(format!("{cr:.3} {cg:.3} {cb:.3} rg /F1 {size} Tf")),
pdf/src/main/rust/src/annotations_part5.rs
diff --git a/pdf/src/main/rust/src/annotations_part5.rs b/pdf/src/main/rust/src/annotations_part5.rsindex 3d77490c05..36ee9a61af 100644--- a/pdf/src/main/rust/src/annotations_part5.rs+++ b/pdf/src/main/rust/src/annotations_part5.rs@@ -203,7 +203,7 @@ pub(crate) fn update_free_text(handle: i64, annot_id: i64, text: &str) -> bool { let content = free_text_content(w, h, text, argb, size); let ap_id = make_appearance_oriented(doc, w, h, content, helvetica_resources(), apm); if let Ok(dict) = doc.get_dictionary_mut(id) {- dict.set("Contents", Object::string_literal(text));+ dict.set("Contents", encode_pdf_text(text)); let mut ap = Dictionary::new(); ap.set("N", Object::Reference(ap_id)); dict.set("AP", Object::Dictionary(ap));
pdf/src/main/rust/src/annotations_part6.rs
diff --git a/pdf/src/main/rust/src/annotations_part6.rs b/pdf/src/main/rust/src/annotations_part6.rsindex 83aabcf4c4..e87bc4b1a7 100644--- a/pdf/src/main/rust/src/annotations_part6.rs+++ b/pdf/src/main/rust/src/annotations_part6.rs@@ -68,3 +68,83 @@ pub(crate) fn list_annotations(handle: i64, page_index: i32) -> Option<Vec<u8>> } Some(buf) }++/// Encode a PDF text string (§7.9.2): PDFDocEncoding bytes when every+/// character is representable there, otherwise UTF-16BE with its FE FF BOM.+/// Never the Rust string's UTF-8 bytes, which no conforming reader may+/// interpret as text-string characters.+pub(crate) fn encode_pdf_text(s: &str) -> Object {+ Object::String(encode_pdf_text_bytes(s), lopdf::StringFormat::Literal)+}++pub(crate) fn encode_pdf_text_bytes(s: &str) -> Vec<u8> {+ if let Some(bytes) = pdfdoc_bytes(s) {+ return bytes;+ }+ let mut out = vec![0xFE, 0xFF];+ for unit in s.encode_utf16() {+ out.extend_from_slice(&unit.to_be_bytes());+ }+ out+}++fn pdfdoc_bytes(s: &str) -> Option<Vec<u8>> {+ let mut out = Vec::with_capacity(s.len());+ for c in s.chars() {+ out.push(pdfdoc_byte(c)?);+ }+ Some(out)+}++/// PDFDocEncoding (ISO 32000 Annex D.3) byte for `c`, where defined.+fn pdfdoc_byte(c: char) -> Option<u8> {+ Some(match c {+ '\u{02D8}' => 0x18, '\u{02C7}' => 0x19, '\u{02C6}' => 0x1A,+ '\u{02D9}' => 0x1B, '\u{02DD}' => 0x1C, '\u{02DB}' => 0x1D,+ '\u{02DA}' => 0x1E, '\u{02DC}' => 0x1F,+ '\u{2022}' => 0x80, '\u{2020}' => 0x81, '\u{2021}' => 0x82,+ '\u{2026}' => 0x83, '\u{2014}' => 0x84, '\u{2013}' => 0x85,+ '\u{0192}' => 0x86, '\u{2044}' => 0x87, '\u{2039}' => 0x88,+ '\u{203A}' => 0x89, '\u{2212}' => 0x8A, '\u{2030}' => 0x8B,+ '\u{201E}' => 0x8C, '\u{201C}' => 0x8D, '\u{201D}' => 0x8E,+ '\u{2018}' => 0x8F, '\u{2019}' => 0x90, '\u{201A}' => 0x91,+ '\u{2122}' => 0x92, '\u{FB01}' => 0x93, '\u{FB02}' => 0x94,+ '\u{0141}' => 0x95, '\u{0152}' => 0x96, '\u{0160}' => 0x97,+ '\u{0178}' => 0x98, '\u{017D}' => 0x99, '\u{0131}' => 0x9A,+ '\u{0142}' => 0x9B, '\u{0153}' => 0x9C, '\u{0161}' => 0x9D,+ '\u{017E}' => 0x9E, '\u{20AC}' => 0xA0,+ // 0xA1..=0xFF coincide with Latin-1, except 0xAD (undefined).+ c if ('\u{20}'..='\u{7E}').contains(&c) => c as u8,+ c if ('\u{A1}'..='\u{FF}').contains(&c) && c != '\u{AD}' => c as u8,+ _ => return None,+ })+}++fn pdfdoc_char(b: u8) -> Option<char> {+ // Inverse of `pdfdoc_byte`; scanning a fixed 32-entry special table plus+ // the two identity ranges beats carrying a second copy of the table.+ const SPECIAL: [(char, u8); 40] = [+ ('\u{02D8}', 0x18), ('\u{02C7}', 0x19), ('\u{02C6}', 0x1A),+ ('\u{02D9}', 0x1B), ('\u{02DD}', 0x1C), ('\u{02DB}', 0x1D),+ ('\u{02DA}', 0x1E), ('\u{02DC}', 0x1F), ('\u{2022}', 0x80),+ ('\u{2020}', 0x81), ('\u{2021}', 0x82), ('\u{2026}', 0x83),+ ('\u{2014}', 0x84), ('\u{2013}', 0x85), ('\u{0192}', 0x86),+ ('\u{2044}', 0x87), ('\u{2039}', 0x88), ('\u{203A}', 0x89),+ ('\u{2212}', 0x8A), ('\u{2030}', 0x8B), ('\u{201E}', 0x8C),+ ('\u{201C}', 0x8D), ('\u{201D}', 0x8E), ('\u{2018}', 0x8F),+ ('\u{2019}', 0x90), ('\u{201A}', 0x91), ('\u{2122}', 0x92),+ ('\u{FB01}', 0x93), ('\u{FB02}', 0x94), ('\u{0141}', 0x95),+ ('\u{0152}', 0x96), ('\u{0160}', 0x97), ('\u{0178}', 0x98),+ ('\u{017D}', 0x99), ('\u{0131}', 0x9A), ('\u{0142}', 0x9B),+ ('\u{0153}', 0x9C), ('\u{0161}', 0x9D), ('\u{017E}', 0x9E),+ ('\u{20AC}', 0xA0),+ ];+ if let Some((c, _)) = SPECIAL.iter().find(|(_, code)| *code == b) {+ return Some(*c);+ }+ match b {+ 0x20..=0x7E => char::from_u32(b as u32),+ 0xA1..=0xFF if b != 0xAD => char::from_u32(b as u32),+ _ => None,+ }+}
pdf/src/main/rust/src/cov_annotations_tests.rs
diff --git a/pdf/src/main/rust/src/cov_annotations_tests.rs b/pdf/src/main/rust/src/cov_annotations_tests.rsindex f673ee2160..90241395a0 100644--- a/pdf/src/main/rust/src/cov_annotations_tests.rs+++ b/pdf/src/main/rust/src/cov_annotations_tests.rs@@ -95,6 +95,26 @@ fn escape_and_decode_pdf_text() { assert_eq!(decode_pdf_text(&[0xFE, 0xFF, 0x00]), "\u{0}"); } +#[test]+fn encode_pdf_text_uses_pdfdoc_or_utf16be_never_utf8() {+ assert_eq!(encode_pdf_text_bytes("plain"), b"plain");+ // PDFDocEncoding: é=0xE9, em dash=0x84, Euro=0xA0 (Annex D.3) — and+ // lopdf's own spec decoder must read those bytes back as the same text.+ let bytes = encode_pdf_text_bytes("café — €");+ assert_eq!(bytes, vec![b'c', b'a', b'f', 0xE9, b' ', 0x84, b' ', 0xA0]);+ let obj = Object::String(bytes.clone(), lopdf::StringFormat::Literal);+ assert_eq!(lopdf::decode_text_string(&obj).unwrap(), "café — €");+ assert_eq!(decode_pdf_text(&bytes), "café — €");+ // Appearance literals are WinAnsi font codes: é \351, em dash \227, € \200.+ assert_eq!(escape_pdf_literal("café — €"), "caf\\351 \\227 \\200");+ assert_eq!(escape_pdf_literal("漢"), "?", "unrepresentable in WinAnsi");+ // Beyond PDFDocEncoding: UTF-16BE with BOM, round-tripping both ways.+ let bytes = encode_pdf_text_bytes("a漢\nb");+ assert_eq!(&bytes[..2], &[0xFE, 0xFF]);+ assert_eq!(decode_pdf_text(&bytes), "a漢\nb");+ assert_eq!(lopdf::decode_text_string(&encode_pdf_text("a漢")).unwrap(), "a漢");+}+ #[test] fn offset_flat_shifts_alternating_axes() { let mut arr = ints(&[0, 0, 10, 20]);
pdf/src/main/rust/src/cov_forms_tests.rs
diff --git a/pdf/src/main/rust/src/cov_forms_tests.rs b/pdf/src/main/rust/src/cov_forms_tests.rsindex f88782dfb4..75b01b781d 100644--- a/pdf/src/main/rust/src/cov_forms_tests.rs+++ b/pdf/src/main/rust/src/cov_forms_tests.rs@@ -43,6 +43,135 @@ fn dest_array_unwraps_dict_d() { assert_eq!(named_dest(&doc, b"nope"), None); } +/// One-page form with a single widget that is also its terminal field.+fn single_field_handle(ft: &str, opt: Option<Object>) -> (i64, ObjectId) {+ let mut doc = Document::with_version("1.7");+ let pages_id = doc.new_object_id();+ let widget = doc.add_object(dictionary! {+ "Type" => name_obj("Annot"), "Subtype" => name_obj("Widget"),+ "Rect" => rect_obj([0.0, 0.0, 200.0, 24.0]),+ "FT" => name_obj(ft), "T" => Object::string_literal("field"),+ "DA" => Object::string_literal("/Helv 10 Tf 0 g"),+ });+ if let Some(opt) = opt {+ doc.get_dictionary_mut(widget).unwrap().set("Opt", opt);+ }+ let page = doc.add_object(dictionary! {+ "Type" => name_obj("Page"), "Parent" => Object::Reference(pages_id),+ "MediaBox" => rect_obj([0.0, 0.0, 612.0, 792.0]),+ "Annots" => Object::Array(vec![Object::Reference(widget)]),+ });+ doc.objects.insert(pages_id, Object::Dictionary(dictionary! {+ "Type" => name_obj("Pages"),+ "Kids" => Object::Array(vec![Object::Reference(page)]), "Count" => 1 }));+ let acro = doc.add_object(dictionary! {+ "Fields" => Object::Array(vec![Object::Reference(widget)]) });+ let cat = doc.add_object(dictionary! {+ "Type" => name_obj("Catalog"), "Pages" => Object::Reference(pages_id),+ "AcroForm" => Object::Reference(acro) });+ doc.trailer.set("Root", cat);+ let handle = next_handle();+ registry().lock().unwrap_or_else(|e| e.into_inner()).insert(handle, doc);+ (handle, widget)+}++fn raw_value_and_appearance(handle: i64, widget: ObjectId) -> (Vec<u8>, Vec<u8>) {+ let reg = registry().lock().unwrap_or_else(|e| e.into_inner());+ let doc = reg.get(&handle).expect("doc");+ let dict = doc.get_dictionary(widget).expect("widget");+ let v = match dict.get(b"V").expect("/V") {+ Object::String(s, _) => s.clone(),+ other => panic!("/V is not a string: {other:?}"),+ };+ let content = dict+ .get(b"AP")+ .ok()+ .and_then(|o| deref(doc, o))+ .and_then(|o| o.as_dict().ok())+ .and_then(|ap| ap.get(b"N").ok())+ .and_then(|o| deref(doc, o))+ .and_then(|o| o.as_stream().ok())+ .map(|s| s.content.clone())+ .unwrap_or_default();+ (v, content)+}++/// Decode the `Tj` operands of an appearance stream as WinAnsi bytes.+fn appearance_text(content: &[u8]) -> String {+ let win = encoding::win_ansi();+ let mut out = String::new();+ for op in crate::content::parse_operations_lenient(content) {+ if op.operator == "Tj" {+ if let Some(Object::String(bytes, _)) = op.operands.first() {+ out.extend(bytes.iter().map(|b| win.get(&(*b as u32)).copied().unwrap_or('?')));+ }+ }+ }+ out+}++fn listed_value(handle: i64) -> String {+ let buf = list_form_fields(handle, 0).expect("fields");+ let count = u32::from_le_bytes(buf[0..4].try_into().unwrap());+ assert_eq!(count, 1);+ // id(8) type(1) rect(16) then name len+bytes, value len+bytes+ let mut p = 4 + 8 + 1 + 16;+ let nlen = u16::from_le_bytes(buf[p..p + 2].try_into().unwrap()) as usize;+ p += 2 + nlen;+ let vlen = u16::from_le_bytes(buf[p..p + 2].try_into().unwrap()) as usize;+ p += 2;+ String::from_utf8(buf[p..p + vlen].to_vec()).expect("wire value is UTF-8")+}++#[test]+fn text_field_non_ascii_value_round_trips_without_mojibake() {+ // 'café — €' is representable in PDFDocEncoding and in WinAnsi, but its+ // UTF-8 bytes are neither: writing them verbatim makes §7.9.2 readers+ // (including decode_pdf_text) show 'café' as 'café' etc.+ let (handle, widget) = single_field_handle("Tx", None);+ assert!(set_text_field(handle, encode_id(widget), "café — €"));+ let (raw, content) = raw_value_and_appearance(handle, widget);+ assert_ne!(raw, "café — €".as_bytes(), "/V must not hold raw UTF-8: {raw:?}");+ assert_eq!(decode_pdf_text(&raw), "café — €", "/V bytes {raw:?}");+ assert_eq!(+ appearance_text(&content),+ "café — €",+ "appearance: {}",+ String::from_utf8_lossy(&content)+ );+ assert_eq!(listed_value(handle), "café — €");+ // The serialised file must carry the same bytes, not a UTF-8 re-encode.+ let saved = save_document(handle).expect("save");+ assert!(!saved.windows(3).any(|w| w == "é".as_bytes()), "saved /V or appearance leaks UTF-8 é");+ close_document(handle);+}++#[test]+fn text_field_beyond_pdfdoc_uses_utf16be_with_bom() {+ let (handle, widget) = single_field_handle("Tx", None);+ let value = "café 漢字 €";+ assert!(set_text_field(handle, encode_id(widget), value));+ let (raw, _) = raw_value_and_appearance(handle, widget);+ assert_eq!(&raw[..2], &[0xFE, 0xFF], "non-PDFDoc value needs UTF-16BE BOM: {raw:?}");+ assert_eq!(decode_pdf_text(&raw), value);+ assert_eq!(listed_value(handle), value);+ close_document(handle);+}++#[test]+fn choice_field_non_ascii_value_round_trips_without_mojibake() {+ let opt =+ Object::Array(vec![Object::string_literal("café — €"), Object::string_literal("plain")]);+ let (handle, widget) = single_field_handle("Ch", Some(opt));+ assert!(set_choice_field(handle, encode_id(widget), "café — €"));+ let (raw, content) = raw_value_and_appearance(handle, widget);+ assert_ne!(raw, "café — €".as_bytes(), "/V must not hold raw UTF-8");+ assert_eq!(decode_pdf_text(&raw), "café — €");+ assert_eq!(appearance_text(&content), "café — €");+ assert_eq!(listed_value(handle), "café — €");+ close_document(handle);+}+ #[test] fn comps_to_rgb_all_arities_and_short_input() { assert_eq!(comps_to_rgb(&[10, 20, 30], 3), (10, 20, 30));
pdf/src/main/rust/src/forms_part2.rs
diff --git a/pdf/src/main/rust/src/forms_part2.rs b/pdf/src/main/rust/src/forms_part2.rsindex 9ae6693349..7558344c2b 100644--- a/pdf/src/main/rust/src/forms_part2.rs+++ b/pdf/src/main/rust/src/forms_part2.rs@@ -70,7 +70,8 @@ pub(crate) fn set_text_field(handle: i64, widget_id: i64, value: &str) -> bool { // /V is a field attribute, so it belongs on the root; the widgets carry only // the regenerated appearance. let set_root = if let Ok(dict) = doc.get_dictionary_mut(root_id) {- dict.set("V", Object::string_literal(value));+ // /V is a text string (§7.9.2), not UTF-8: PDFDoc or UTF-16BE.+ dict.set("V", encode_pdf_text(value)); true } else { false }; let mut set_widget = false;@@ -235,7 +236,7 @@ pub(crate) fn set_choice_field(handle: i64, widget_id: i64, value: &str) -> bool // /V and /I are field keys (§12.7.3.1) and belong on the root; only /AP is // per-widget. if let Ok(dict) = doc.get_dictionary_mut(root_id) {- dict.set("V", Object::string_literal(export));+ dict.set("V", encode_pdf_text(&export)); match opt_index { Some(i) => { dict.set("I", Object::Array(vec![Object::Integer(i as i64)])); } None => { dict.remove(b"I"); }