diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cf2b3cd..bbf18afa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Unreleased +- Write numeric form values and defaults as PDF text strings so viewers can display and reset them - Add an `onPageAdded` document option, a function called with the document each time a page is added, including the automatic first page that a `pageAdded` listener attached after construction would miss. Fixes #1529 - Fix smooth curves (`S`, `T`) in SVG paths reflecting the control point of an arc or of a curve of the other kind. As the SVG spec says, they now start from the current point there. An arc with a zero radius is now drawn as a straight line instead of nothing diff --git a/docs/forms.md b/docs/forms.md index e48b965e..b1acace6 100644 --- a/docs/forms.md +++ b/docs/forms.md @@ -203,6 +203,9 @@ options: - `borderColor` - Field border color. - `fontSize` [_number_] - Sets the font size used in the field appearance string. +Numeric values and defaults in text and choice fields are stored as text. For example, +`value: 0` and `value: '0'` set the same field value. + ### Text Field Formatting When needing to format the text value of a Form Annotation, the following diff --git a/lib/mixins/acroform.js b/lib/mixins/acroform.js index 64949283..a1ceb483 100644 --- a/lib/mixins/acroform.js +++ b/lib/mixins/acroform.js @@ -105,8 +105,9 @@ function mapStrings(options, pdfObject) { Object.keys(VALUE_MAP).forEach((key) => { if (key in options) { const value = options[key] ?? ''; - pdfObject[VALUE_MAP[key]] = - typeof value === 'string' ? new String(value) : value; + const isTextValue = + typeof value === 'string' || typeof value === 'number'; + pdfObject[VALUE_MAP[key]] = isTextValue ? new String(value) : value; } }); diff --git a/tests/unit/acroform.spec.js b/tests/unit/acroform.spec.js index 8fd184f3..6d57ea7c 100644 --- a/tests/unit/acroform.spec.js +++ b/tests/unit/acroform.spec.js @@ -418,11 +418,23 @@ describe('acroform', () => { expect(docData[4]).not.toContain('/Combo'); }); + test('writes numeric choice values and defaults as PDF text strings', () => { + doc.initForm(); + const docData = logData(doc); + doc.formCombo('quantity', 20, 20, 50, 20, { + select: ['0', '1'], + value: 0, + defaultValue: 1, + }); + expect(docData[1]).toContain('/V (0)'); + expect(docData[1]).toContain('/DV (1)'); + }); + describe('text format', () => { test('number', () => { const expected = [ '10 0 obj', - '<<\n/FT /Tx\n/V 32.98\n/AA <<\n/K <<\n/S /JavaScript\n' + + '<<\n/FT /Tx\n/V (32.98)\n/AA <<\n/K <<\n/S /JavaScript\n' + '/JS (AFNumber_Keystroke\\(2,1,"MinusBlack",null,"$",true\\);)\n>>\n' + '/F <<\n/S /JavaScript\n/JS (AFNumber_Format\\(2,1,"MinusBlack",null,"$",true\\);)\n>>\n>>\n' + '/T (dollars)\n/Subtype /Widget\n/F 4\n/Type /Annot\n/Rect [20 752 70 772]\n/Border [0 0 0]\n/C [0 0 0]\n>>',