Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions docs/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/mixins/acroform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});

Expand Down
14 changes: 13 additions & 1 deletion tests/unit/acroform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>>',
Expand Down
Loading