Repository navigation
Expand file tree
/
Copy pathFormField.ts
More file actions
397 lines (346 loc) · 10.8 KB
/
Copy pathFormField.ts
File metadata and controls
397 lines (346 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/**
* @typedef FormField
* @memberof FormField
* @property {FormField.Type} [type] The type of the form field.
* @property {string} [name] The name of the form field.
* @property {string} [fullyQualifiedName] The fully qualified name of the form field.
* @property {string} [mappingName] The mapping name used when exporting form field data.
* @property {string} [alternateFieldName] An alternate name used in the UI.
* @property {boolean} [isEditable] Whether the form field is editable.
* @property {boolean} [isReadOnly] Whether the form field is read-only.
* @property {boolean} [isRequired] Whether the form field is required.
* @property {boolean} [isNoExport] Whether the form field should not be exported.
* @property {any} [defaultValue] The default value of the form field.
* @property {any} [exportValue] The value exported when the form is submitted.
* @property {any} [value] The current value of the form field.
* @property {number} [calculationOrderIndex] The calculation order index.
* @property {boolean} [dirty] Whether the form field has been modified.
*/
/**
* @typedef ButtonFormField
* @memberof FormField
* @property {boolean} isPushButton Whether this is a push button.
* @property {boolean} isCheckBox Whether this is a check box.
* @property {boolean} isRadioButton Whether this is a radio button.
* @property {number[]} selectedAnnotationObjectNumbers The selected annotation object numbers.
* @property {Array<{value: string, label: string}>} options The available options for the button.
*/
/**
* @typedef ChoiceFormField
* @memberof FormField
* @property {Array<{value: string, label: string}>} options The available options.
* @property {number[]} selectedIndices The indices of selected options.
* @property {boolean} isMultiSelect Whether multiple selections are allowed.
* @property {boolean} isCombo Whether this is a combo box.
* @property {boolean} isEditable Whether the field is editable.
* @property {number} [topIndex] The top index of visible options.
*/
/**
* @typedef SignatureFormField
* @memberof FormField
* @property {string} [signatureContents] The contents of the signature.
* @property {{name: string, date: Date, reason: string, location: string}} [signatureInfo] Information about the signature.
* @property {boolean} isSigned Whether the field is signed.
* @property {'text' | 'image' | 'drawing'} [signatureType] The type of signature.
*/
/**
* @typedef TextFormField
* @memberof FormField
* @property {string} value The current value of the text field.
* @property {string} [placeholder] The placeholder text.
* @property {boolean} isPassword Whether this is a password field.
* @property {boolean} isRichText Whether rich text is enabled.
* @property {'left' | 'center' | 'right'} alignment The text alignment.
* @property {number} fontSize The font size.
* @property {string} [fontName] The font name.
* @property {string} [textColor] The text color.
* @property {number} [maxLength] The maximum length of the text.
* @property {boolean} isMultiline Whether the field is multiline.
* @property {boolean} doNotScroll Whether scrolling is disabled.
*/
/**
* @interface FormField
*/
export class FormField {
/**
* The type of the form field.
*/
type?: FormField.Type;
/**
* The name of the form field.
*/
name?: string;
/**
* The fully qualified name of the form field. Form fields can form a hierarchy in the PDF form.
* This combines all the parents names and separates them by a single dot to create a string that
* can uniquely identify a form field across one PDF file.
*/
fullyQualifiedName?: string;
/**
* (Optional; PDF 1.3) The mapping name that shall be used when exporting interactive form field data from the document.
*/
mappingName?: string;
/**
* (Optional; PDF 1.3) An alternate field name that shall be used in place of the actual field name
* wherever the field shall be identified in the user interface.
*/
alternateFieldName?: string;
/**
* Specifies if the linked form elements are editable in the UI. Defaults to true.
*/
isEditable?: boolean;
/**
* If set, the user may not change the value of the field. This flag is useful for fields whose
* values are computed or imported from a database.
*/
isReadOnly?: boolean;
/**
* If set, the field shall have a value at the time it is exported by a submit-form action.
*/
isRequired?: boolean;
/**
* If set, the field shall not be exported by a submit-form action.
*/
isNoExport?: boolean;
/**
* (Optional; inheritable) The default value to which the field reverts when a reset-form action is executed.
*/
defaultValue?: any;
/**
* The value which the field is to export when submitted. Can return either a string or an array
* of strings in the case of multiple selection.
*/
exportValue?: any;
/**
* The value of the field. Can either be a string or an array of strings.
*/
value?: any;
/**
* Returns the calculation order index.
*/
calculationOrderIndex?: number;
/**
* Checks if the form field is dirty.
*/
dirty?: boolean;
constructor(data: Partial<FormField> = {}) {
this.type = data.type;
this.name = data.name;
this.fullyQualifiedName = data.fullyQualifiedName;
this.mappingName = data.mappingName;
this.alternateFieldName = data.alternateFieldName;
this.isEditable = data.isEditable;
this.isReadOnly = data.isReadOnly;
this.isRequired = data.isRequired;
this.isNoExport = data.isNoExport;
this.defaultValue = data.defaultValue;
this.exportValue = data.exportValue;
this.value = data.value;
this.calculationOrderIndex = data.calculationOrderIndex;
this.dirty = data.dirty;
}
}
export namespace FormField {
/**
* The different types of form fields.
* @readonly
* @enum {string} Type
*/
export const Type = {
/**
* No form type known.
*/
UNKNOWN: 'unknown',
/**
* Push button form field.
*/
PUSH_BUTTON: 'pushButton',
/**
* Radio button form field.
*/
RADIO_BUTTON: 'radioButton',
/**
* Check box form field.
*/
CHECK_BOX: 'checkBox',
/**
* Text form field.
*/
TEXT: 'text',
/**
* List box form field.
*/
LIST_BOX: 'listBox',
/**
* Combo box form field.
*/
COMBO_BOX: 'comboBox',
/**
* Signature form field.
*/
SIGNATURE: 'signature',
} as const;
export type Type = ValueOf<typeof Type>;
type ValueOf<T> = T[keyof T];
}
export class ButtonFormField extends FormField {
/**
* Whether this is a push button.
*/
isPushButton: boolean;
/**
* Whether this is a check box.
*/
isCheckBox: boolean;
/**
* Whether this is a radio button.
*/
isRadioButton: boolean;
/**
* The selected annotation object numbers.
*/
selectedAnnotationObjectNumbers: number[];
/**
* The available options for the button.
*/
options: Array<{
value: string;
label: string;
}>;
constructor(data: Partial<ButtonFormField> = {}) {
super(data);
this.isPushButton = data.isPushButton || false;
this.isCheckBox = data.isCheckBox || false;
this.isRadioButton = data.isRadioButton || false;
this.selectedAnnotationObjectNumbers = data.selectedAnnotationObjectNumbers || [];
this.options = data.options || [];
}
}
export class ChoiceFormField extends FormField {
/**
* The available options.
*/
options: Array<{
value: string;
label: string;
}>;
/**
* The indices of selected options.
*/
selectedIndices: number[];
/**
* Whether multiple selections are allowed.
*/
isMultiSelect: boolean;
/**
* Whether this is a combo box.
*/
isCombo: boolean;
/**
* Whether the field is editable.
*/
override isEditable: boolean;
/**
* The top index of visible options.
*/
topIndex?: number;
constructor(data: Partial<ChoiceFormField> = {}) {
super(data);
this.options = data.options || [];
this.selectedIndices = data.selectedIndices || [];
this.isMultiSelect = data.isMultiSelect || false;
this.isCombo = data.isCombo || false;
this.isEditable = data.isEditable || false;
this.topIndex = data.topIndex;
}
}
export class SignatureFormField extends FormField {
/**
* The contents of the signature.
*/
signatureContents?: string;
/**
* Information about the signature.
*/
signatureInfo?: {
name: string;
date: Date;
reason: string;
location: string;
};
/**
* Whether the field is signed.
*/
isSigned: boolean;
/**
* The type of signature.
*/
signatureType?: 'text' | 'image' | 'drawing';
constructor(data: Partial<SignatureFormField> = {}) {
super(data);
this.signatureContents = data.signatureContents;
this.signatureInfo = data.signatureInfo;
this.isSigned = data.isSigned || false;
this.signatureType = data.signatureType;
}
}
export class TextFormField extends FormField {
/**
* The current value of the text field.
*/
override value: string;
/**
* The placeholder text.
*/
placeholder?: string;
/**
* Whether this is a password field.
*/
isPassword: boolean;
/**
* Whether rich text is enabled.
*/
isRichText: boolean;
/**
* The text alignment.
*/
alignment: 'left' | 'center' | 'right';
/**
* The font size.
*/
fontSize: number;
/**
* The font name.
*/
fontName?: string;
/**
* The text color.
*/
textColor?: string;
/**
* The maximum length of the text.
*/
maxLength?: number;
/**
* Whether the field is multiline.
*/
isMultiline: boolean;
/**
* Whether scrolling is disabled.
*/
doNotScroll: boolean;
constructor(data: Partial<TextFormField> = {}) {
super(data);
this.value = data.value || '';
this.placeholder = data.placeholder;
this.isPassword = data.isPassword || false;
this.isRichText = data.isRichText || false;
this.alignment = data.alignment || 'left';
this.fontSize = data.fontSize || 12;
this.fontName = data.fontName;
this.textColor = data.textColor;
this.maxLength = data.maxLength;
this.isMultiline = data.isMultiline || false;
this.doNotScroll = data.doNotScroll || false;
}
}