pdf-lite 1.3.3 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/dist/acroform/acroform.d.ts +7 -454
  2. package/dist/acroform/acroform.js +5 -1664
  3. package/dist/acroform/appearance/index.d.ts +4 -0
  4. package/dist/acroform/appearance/index.js +4 -0
  5. package/dist/acroform/appearance/pdf-appearance-stream.d.ts +21 -0
  6. package/dist/acroform/appearance/pdf-appearance-stream.js +41 -0
  7. package/dist/acroform/appearance/pdf-button-appearance-stream.d.ts +13 -0
  8. package/dist/acroform/appearance/pdf-button-appearance-stream.js +54 -0
  9. package/dist/acroform/appearance/pdf-choice-appearance-stream.d.ts +22 -0
  10. package/dist/acroform/appearance/pdf-choice-appearance-stream.js +75 -0
  11. package/dist/acroform/appearance/pdf-graphics.d.ts +51 -0
  12. package/dist/acroform/appearance/pdf-graphics.js +239 -0
  13. package/dist/acroform/appearance/pdf-text-appearance-stream.d.ts +22 -0
  14. package/dist/acroform/appearance/pdf-text-appearance-stream.js +104 -0
  15. package/dist/acroform/fields/index.d.ts +8 -0
  16. package/dist/acroform/fields/index.js +8 -0
  17. package/dist/acroform/fields/pdf-button-form-field.d.ts +23 -0
  18. package/dist/acroform/fields/pdf-button-form-field.js +102 -0
  19. package/dist/acroform/fields/pdf-choice-form-field.d.ts +18 -0
  20. package/dist/acroform/fields/pdf-choice-form-field.js +131 -0
  21. package/dist/acroform/fields/pdf-default-appearance.d.ts +23 -0
  22. package/dist/acroform/fields/pdf-default-appearance.js +68 -0
  23. package/dist/acroform/fields/pdf-form-field-flags.d.ts +45 -0
  24. package/dist/acroform/fields/pdf-form-field-flags.js +122 -0
  25. package/dist/acroform/fields/pdf-form-field.d.ts +123 -0
  26. package/dist/acroform/fields/pdf-form-field.js +433 -0
  27. package/dist/acroform/fields/pdf-signature-form-field.d.ts +7 -0
  28. package/dist/acroform/fields/pdf-signature-form-field.js +12 -0
  29. package/dist/acroform/fields/pdf-text-form-field.d.ts +10 -0
  30. package/dist/acroform/fields/pdf-text-form-field.js +77 -0
  31. package/dist/acroform/fields/types.d.ts +26 -0
  32. package/dist/acroform/fields/types.js +9 -0
  33. package/dist/acroform/index.d.ts +5 -1
  34. package/dist/acroform/index.js +5 -1
  35. package/dist/acroform/manager.d.ts +12 -1
  36. package/dist/acroform/manager.js +20 -2
  37. package/dist/acroform/pdf-acro-form.d.ts +69 -0
  38. package/dist/acroform/pdf-acro-form.js +293 -0
  39. package/dist/acroform/pdf-font-encoding-cache.d.ts +27 -0
  40. package/dist/acroform/pdf-font-encoding-cache.js +188 -0
  41. package/dist/acroform/xfa/index.d.ts +3 -0
  42. package/dist/acroform/xfa/index.js +2 -0
  43. package/dist/acroform/xfa/pdf-xfa-data.d.ts +20 -0
  44. package/dist/acroform/xfa/pdf-xfa-data.js +68 -0
  45. package/dist/acroform/xfa/pdf-xfa-form.d.ts +11 -0
  46. package/dist/acroform/xfa/pdf-xfa-form.js +56 -0
  47. package/dist/annotations/index.d.ts +4 -0
  48. package/dist/annotations/index.js +4 -0
  49. package/dist/annotations/pdf-annotation-flags.d.ts +24 -0
  50. package/dist/annotations/pdf-annotation-flags.js +93 -0
  51. package/dist/annotations/pdf-annotation-writer.d.ts +20 -0
  52. package/dist/annotations/pdf-annotation-writer.js +76 -0
  53. package/dist/annotations/pdf-annotation.d.ts +61 -0
  54. package/dist/annotations/pdf-annotation.js +106 -0
  55. package/dist/annotations/pdf-widget-annotation.d.ts +15 -0
  56. package/dist/annotations/pdf-widget-annotation.js +37 -0
  57. package/dist/core/objects/pdf-array.d.ts +1 -1
  58. package/dist/core/objects/pdf-array.js +3 -2
  59. package/dist/core/objects/pdf-boolean.d.ts +1 -1
  60. package/dist/core/objects/pdf-boolean.js +3 -2
  61. package/dist/core/objects/pdf-comment.d.ts +1 -1
  62. package/dist/core/objects/pdf-comment.js +1 -1
  63. package/dist/core/objects/pdf-dictionary.d.ts +1 -1
  64. package/dist/core/objects/pdf-dictionary.js +3 -2
  65. package/dist/core/objects/pdf-hexadecimal.d.ts +1 -1
  66. package/dist/core/objects/pdf-hexadecimal.js +3 -2
  67. package/dist/core/objects/pdf-indirect-object.d.ts +1 -1
  68. package/dist/core/objects/pdf-indirect-object.js +1 -1
  69. package/dist/core/objects/pdf-name.d.ts +1 -1
  70. package/dist/core/objects/pdf-name.js +3 -2
  71. package/dist/core/objects/pdf-null.d.ts +1 -1
  72. package/dist/core/objects/pdf-null.js +3 -2
  73. package/dist/core/objects/pdf-number.d.ts +1 -1
  74. package/dist/core/objects/pdf-number.js +3 -2
  75. package/dist/core/objects/pdf-object-reference.d.ts +1 -1
  76. package/dist/core/objects/pdf-object-reference.js +3 -2
  77. package/dist/core/objects/pdf-object.d.ts +3 -1
  78. package/dist/core/objects/pdf-object.js +6 -0
  79. package/dist/core/objects/pdf-start-xref.d.ts +1 -1
  80. package/dist/core/objects/pdf-start-xref.js +3 -2
  81. package/dist/core/objects/pdf-stream.d.ts +4 -3
  82. package/dist/core/objects/pdf-stream.js +45 -16
  83. package/dist/core/objects/pdf-string.d.ts +2 -1
  84. package/dist/core/objects/pdf-string.js +17 -2
  85. package/dist/core/objects/pdf-trailer.d.ts +1 -1
  86. package/dist/core/objects/pdf-trailer.js +3 -2
  87. package/dist/core/objects/pdf-xref-table.d.ts +3 -3
  88. package/dist/core/objects/pdf-xref-table.js +3 -3
  89. package/dist/core/parser/incremental-parser.d.ts +0 -13
  90. package/dist/core/parser/incremental-parser.js +1 -18
  91. package/dist/core/streams/object-stream.d.ts +1 -1
  92. package/dist/core/streams/object-stream.js +1 -1
  93. package/dist/errors.d.ts +22 -0
  94. package/dist/errors.js +24 -0
  95. package/dist/fonts/index.d.ts +1 -1
  96. package/dist/fonts/index.js +1 -1
  97. package/dist/fonts/pdf-font.d.ts +64 -7
  98. package/dist/fonts/pdf-font.js +188 -8
  99. package/dist/index.d.ts +2 -0
  100. package/dist/index.js +2 -0
  101. package/dist/pdf/index.d.ts +0 -1
  102. package/dist/pdf/index.js +0 -1
  103. package/dist/pdf/pdf-document.d.ts +16 -12
  104. package/dist/pdf/pdf-document.js +51 -37
  105. package/dist/pdf/pdf-revision.d.ts +1 -1
  106. package/dist/pdf/pdf-revision.js +1 -1
  107. package/dist/pdf/pdf-xref-lookup.d.ts +8 -0
  108. package/dist/pdf/pdf-xref-lookup.js +12 -0
  109. package/dist/security/handlers/base.js +3 -0
  110. package/dist/utils/encodePdfText.d.ts +17 -0
  111. package/dist/utils/encodePdfText.js +61 -0
  112. package/dist/utils/index.d.ts +1 -1
  113. package/dist/utils/index.js +1 -1
  114. package/package.json +1 -1
  115. package/dist/pdf/errors.d.ts +0 -6
  116. package/dist/pdf/errors.js +0 -6
  117. package/dist/xfa/index.d.ts +0 -1
  118. package/dist/xfa/index.js +0 -1
  119. package/dist/xfa/manager.d.ts +0 -44
  120. package/dist/xfa/manager.js +0 -136
  121. /package/dist/fonts/{font-manager.d.ts → manager.d.ts} +0 -0
  122. /package/dist/fonts/{font-manager.js → manager.js} +0 -0
  123. /package/dist/utils/{IterableReadableStream.d.ts → iterable-readable-stream.d.ts} +0 -0
  124. /package/dist/utils/{IterableReadableStream.js → iterable-readable-stream.js} +0 -0
@@ -0,0 +1,433 @@
1
+ import { PdfDictionary } from '../../core/objects/pdf-dictionary.js';
2
+ import { PdfArray } from '../../core/objects/pdf-array.js';
3
+ import { PdfString } from '../../core/objects/pdf-string.js';
4
+ import { PdfName } from '../../core/objects/pdf-name.js';
5
+ import { PdfNumber } from '../../core/objects/pdf-number.js';
6
+ import { decodeWithFontEncoding } from '../../utils/decodeWithFontEncoding.js';
7
+ import { PdfWidgetAnnotation } from '../../annotations/pdf-widget-annotation.js';
8
+ import { PdfDefaultAppearance } from './pdf-default-appearance.js';
9
+ import { PdfFieldType as PdfFieldTypeConst } from './types.js';
10
+ import { PdfFormFieldFlags } from './pdf-form-field-flags.js';
11
+ /**
12
+ * Abstract base form field class. Extends PdfWidgetAnnotation with form-specific properties:
13
+ * FT, V, DA, Ff, T (name), field hierarchy (parent/children/siblings).
14
+ * Subclasses must implement generateAppearance().
15
+ */
16
+ export class PdfFormField extends PdfWidgetAnnotation {
17
+ _parent;
18
+ defaultGenerateAppearance = true;
19
+ _appearanceStream;
20
+ _appearanceStreamYes;
21
+ form;
22
+ constructor(options) {
23
+ super({ other: options?.other });
24
+ this.form = options?.form;
25
+ if (options?.parent) {
26
+ this._parent = options.parent;
27
+ }
28
+ }
29
+ get parent() {
30
+ if (this._parent)
31
+ return this._parent;
32
+ if (!this.form)
33
+ return undefined;
34
+ return this.form.fields.find((f) => f !== this &&
35
+ f.kids.some((k) => k.objectNumber === this.objectNumber &&
36
+ k.generationNumber === this.generationNumber));
37
+ }
38
+ set parent(field) {
39
+ if (this._parent) {
40
+ this._parent.kids = this._parent.kids.filter((k) => k.objectNumber !== this.objectNumber ||
41
+ k.generationNumber !== this.generationNumber);
42
+ }
43
+ this._parent = field;
44
+ if (field) {
45
+ const alreadyInKids = field.kids.some((k) => k.objectNumber === this.objectNumber &&
46
+ k.generationNumber === this.generationNumber);
47
+ if (!alreadyInKids) {
48
+ field.kids = [...field.kids, this.reference];
49
+ }
50
+ }
51
+ }
52
+ get children() {
53
+ if (!this.form)
54
+ return [];
55
+ return this.form.fields.filter((f) => f.parent === this);
56
+ }
57
+ set children(fields) {
58
+ for (const child of this.children) {
59
+ child._parent = undefined;
60
+ }
61
+ this.kids = fields.map((f) => f.reference);
62
+ for (const child of fields) {
63
+ child._parent = this;
64
+ }
65
+ }
66
+ get siblings() {
67
+ return this.parent?.children ?? [];
68
+ }
69
+ get encodingMap() {
70
+ const fontName = this.fontName;
71
+ if (!fontName)
72
+ return undefined;
73
+ return this.form?.fontEncodingMaps?.get(fontName);
74
+ }
75
+ get fieldType() {
76
+ const ft = this.content.get('FT')?.as(PdfName)?.value ??
77
+ this.parent?.content.get('FT')?.as(PdfName)?.value;
78
+ switch (ft) {
79
+ case 'Tx':
80
+ return 'Text';
81
+ case 'Btn':
82
+ return 'Button';
83
+ case 'Ch':
84
+ return 'Choice';
85
+ case 'Sig':
86
+ return 'Signature';
87
+ default:
88
+ return null;
89
+ }
90
+ }
91
+ set fieldType(type) {
92
+ if (type === null) {
93
+ this.content.delete('FT');
94
+ }
95
+ else {
96
+ this.content.set('FT', new PdfName(PdfFieldTypeConst[type]));
97
+ }
98
+ }
99
+ get name() {
100
+ const parentName = this.parent?.name ?? '';
101
+ const ownName = this.content.get('T')?.as(PdfString)?.value ?? '';
102
+ if (parentName && ownName) {
103
+ return `${parentName}.${ownName}`;
104
+ }
105
+ return parentName || ownName;
106
+ }
107
+ set name(name) {
108
+ this.content.set('T', new PdfString(name));
109
+ }
110
+ get defaultValue() {
111
+ const dv = this.content.get('DV') ?? this.parent?.content.get('DV');
112
+ if (dv instanceof PdfString)
113
+ return dv.value;
114
+ if (dv instanceof PdfName)
115
+ return dv.value;
116
+ return '';
117
+ }
118
+ set defaultValue(val) {
119
+ this.content.set('DV', new PdfString(val));
120
+ }
121
+ get value() {
122
+ const v = this.content.get('V') ?? this.parent?.content.get('V');
123
+ if (v instanceof PdfString) {
124
+ if (v.isUTF16BE)
125
+ return v.value;
126
+ if (this.encodingMap) {
127
+ return decodeWithFontEncoding(v.raw, this.encodingMap);
128
+ }
129
+ return v.value;
130
+ }
131
+ else if (v instanceof PdfName) {
132
+ return v.value;
133
+ }
134
+ return '';
135
+ }
136
+ set value(val) {
137
+ if (this.value === val)
138
+ return;
139
+ const fieldParent = this.parent?.content.get('FT')
140
+ ? this.parent
141
+ : undefined;
142
+ const generateAppearance = this._storeValue(val, fieldParent);
143
+ if (generateAppearance && this.defaultGenerateAppearance) {
144
+ this.tryGenerateAppearance(this);
145
+ for (const sibling of this.siblings) {
146
+ if (sibling !== this &&
147
+ sibling.rect &&
148
+ sibling.defaultGenerateAppearance) {
149
+ this.tryGenerateAppearance(sibling);
150
+ }
151
+ }
152
+ }
153
+ }
154
+ /**
155
+ * Writes the value to the dictionary. Returns true if appearance generation
156
+ * should proceed, false to skip it (e.g. when value was cleared).
157
+ * Override in subclasses to change the stored representation.
158
+ */
159
+ _storeValue(val, fieldParent) {
160
+ const pdfVal = val instanceof PdfString ? val : new PdfString(val);
161
+ this.content.set('V', pdfVal);
162
+ fieldParent?.content.set('V', pdfVal);
163
+ return true;
164
+ }
165
+ tryGenerateAppearance(field) {
166
+ field.generateAppearance();
167
+ }
168
+ get checked() {
169
+ return false;
170
+ }
171
+ set checked(_isChecked) {
172
+ // no-op for non-button fields; overridden in PdfButtonFormField
173
+ }
174
+ get fontSize() {
175
+ const da = this.defaultAppearance || '';
176
+ const parsed = PdfDefaultAppearance.parse(da);
177
+ return parsed?.fontSize ?? null;
178
+ }
179
+ set fontSize(size) {
180
+ const da = this.defaultAppearance || '';
181
+ if (!da) {
182
+ this.content.set('DA', new PdfDefaultAppearance('F1', size, '0 g'));
183
+ return;
184
+ }
185
+ const parsed = PdfDefaultAppearance.parse(da);
186
+ if (parsed) {
187
+ parsed.fontSize = size;
188
+ this.content.set('DA', parsed);
189
+ }
190
+ }
191
+ get fontName() {
192
+ const da = this.defaultAppearance || '';
193
+ const parsed = PdfDefaultAppearance.parse(da);
194
+ return parsed?.fontName ?? null;
195
+ }
196
+ set fontName(fontName) {
197
+ const da = this.defaultAppearance || '';
198
+ if (!da) {
199
+ this.content.set('DA', new PdfDefaultAppearance(fontName, 12, '0 g'));
200
+ return;
201
+ }
202
+ const parsed = PdfDefaultAppearance.parse(da);
203
+ if (parsed) {
204
+ parsed.fontName = fontName;
205
+ this.content.set('DA', parsed);
206
+ }
207
+ }
208
+ set font(font) {
209
+ if (font === null) {
210
+ this.content.set('DA', new PdfString(''));
211
+ return;
212
+ }
213
+ const resourceName = font.resourceName;
214
+ const currentSize = this.fontSize ?? 12;
215
+ const da = this.defaultAppearance || '';
216
+ if (!da) {
217
+ this.content.set('DA', new PdfDefaultAppearance(resourceName, currentSize, '0 g'));
218
+ return;
219
+ }
220
+ const parsed = PdfDefaultAppearance.parse(da);
221
+ if (parsed) {
222
+ parsed.fontName = resourceName;
223
+ this.content.set('DA', parsed);
224
+ }
225
+ }
226
+ get flags() {
227
+ const flags = new PdfFormFieldFlags(this.content.get('Ff') ?? this.parent?.content.get('Ff') ?? 0);
228
+ flags.onChange(() => {
229
+ this.flags = flags;
230
+ });
231
+ return flags;
232
+ }
233
+ set flags(v) {
234
+ this.content.set('Ff', v);
235
+ }
236
+ get readOnly() {
237
+ return this.flags.readOnly;
238
+ }
239
+ set readOnly(v) {
240
+ this.flags.readOnly = v;
241
+ }
242
+ get required() {
243
+ return this.flags.required;
244
+ }
245
+ set required(v) {
246
+ this.flags.required = v;
247
+ }
248
+ get multiline() {
249
+ return this.flags.multiline;
250
+ }
251
+ set multiline(v) {
252
+ this.flags.multiline = v;
253
+ }
254
+ get password() {
255
+ return this.flags.password;
256
+ }
257
+ set password(v) {
258
+ this.flags.password = v;
259
+ }
260
+ get comb() {
261
+ return this.flags.comb;
262
+ }
263
+ get combField() {
264
+ return this.flags.comb;
265
+ }
266
+ set combField(v) {
267
+ this.flags.comb = v;
268
+ }
269
+ get combo() {
270
+ return this.flags.combo;
271
+ }
272
+ set combo(v) {
273
+ this.flags.combo = v;
274
+ }
275
+ get radio() {
276
+ return this.flags.radio;
277
+ }
278
+ set radio(v) {
279
+ this.flags.radio = v;
280
+ }
281
+ get noToggleToOff() {
282
+ return this.flags.noToggleToOff;
283
+ }
284
+ set noToggleToOff(v) {
285
+ this.flags.noToggleToOff = v;
286
+ }
287
+ get noExport() {
288
+ return this.flags.noExport;
289
+ }
290
+ set noExport(v) {
291
+ this.flags.noExport = v;
292
+ }
293
+ get pushButton() {
294
+ return this.flags.pushButton;
295
+ }
296
+ set pushButton(v) {
297
+ this.flags.pushButton = v;
298
+ }
299
+ get edit() {
300
+ return this.flags.edit;
301
+ }
302
+ set edit(v) {
303
+ this.flags.edit = v;
304
+ }
305
+ get sort() {
306
+ return this.flags.sort;
307
+ }
308
+ set sort(v) {
309
+ this.flags.sort = v;
310
+ }
311
+ get multiSelect() {
312
+ return this.flags.multiSelect;
313
+ }
314
+ set multiSelect(v) {
315
+ this.flags.multiSelect = v;
316
+ }
317
+ get doNotSpellCheck() {
318
+ return this.flags.doNotSpellCheck;
319
+ }
320
+ set doNotSpellCheck(v) {
321
+ this.flags.doNotSpellCheck = v;
322
+ }
323
+ get doNotScroll() {
324
+ return this.flags.doNotScroll;
325
+ }
326
+ set doNotScroll(v) {
327
+ this.flags.doNotScroll = v;
328
+ }
329
+ get commitOnSelChange() {
330
+ return this.flags.commitOnSelChange;
331
+ }
332
+ set commitOnSelChange(v) {
333
+ this.flags.commitOnSelChange = v;
334
+ }
335
+ get quadding() {
336
+ return (this.content.get('Q')?.as(PdfNumber)?.value ??
337
+ this.parent?.content.get('Q')?.as(PdfNumber)?.value ??
338
+ 0);
339
+ }
340
+ set quadding(q) {
341
+ this.content.set('Q', new PdfNumber(q));
342
+ }
343
+ get defaultAppearance() {
344
+ return (this.content.get('DA')?.as(PdfString)?.value ??
345
+ this.parent?.content.get('DA')?.as(PdfString)?.value ??
346
+ this.form?.defaultAppearance ??
347
+ null);
348
+ }
349
+ set defaultAppearance(da) {
350
+ this.content.set('DA', new PdfString(da));
351
+ }
352
+ get maxLen() {
353
+ return this.content.get('MaxLen')?.as(PdfNumber)?.value ?? null;
354
+ }
355
+ set maxLen(maxLen) {
356
+ if (maxLen === null) {
357
+ this.content.delete('MaxLen');
358
+ }
359
+ else {
360
+ this.content.set('MaxLen', new PdfNumber(maxLen));
361
+ }
362
+ }
363
+ get kids() {
364
+ const kidsArray = this.content
365
+ .get('Kids')
366
+ ?.as((PdfArray));
367
+ if (!kidsArray)
368
+ return [];
369
+ return kidsArray.items;
370
+ }
371
+ set kids(kids) {
372
+ if (kids.length === 0) {
373
+ this.content.delete('Kids');
374
+ return;
375
+ }
376
+ const kidsArray = new PdfArray(kids);
377
+ this.content.set('Kids', kidsArray);
378
+ }
379
+ getAppearanceStream() {
380
+ return this._appearanceStream?.content;
381
+ }
382
+ getAppearanceStreamsForWriting() {
383
+ if (!this._appearanceStream)
384
+ return undefined;
385
+ return { primary: this._appearanceStream };
386
+ }
387
+ setAppearanceReference(appearanceStreamRef, _appearanceStreamYesRef) {
388
+ let apDict = this.appearanceStreamDict;
389
+ if (!apDict) {
390
+ apDict = new PdfDictionary();
391
+ this.appearanceStreamDict = apDict;
392
+ }
393
+ apDict.set('N', appearanceStreamRef);
394
+ }
395
+ static _fallbackCtor;
396
+ static _registry = new Map();
397
+ static registerFieldType(ft, ctor, options) {
398
+ PdfFormField._registry.set(ft, ctor);
399
+ if (options?.fallback) {
400
+ PdfFormField._fallbackCtor = ctor;
401
+ }
402
+ }
403
+ static create(options) {
404
+ let ft;
405
+ try {
406
+ const dict = options.other.content.as(PdfDictionary);
407
+ ft = dict.get('FT')?.as(PdfName)?.value;
408
+ }
409
+ catch {
410
+ // content may not be a dictionary
411
+ }
412
+ if (!ft && options.parent) {
413
+ try {
414
+ ft = options.parent.content.get('FT')?.as(PdfName)?.value;
415
+ }
416
+ catch {
417
+ // ignore
418
+ }
419
+ }
420
+ const Ctor = ft ? PdfFormField._registry.get(ft) : undefined;
421
+ if (!Ctor) {
422
+ // Fields without FT are parent/container nodes — use the
423
+ // fallback type (typically Text) as a concrete stand-in.
424
+ if (!PdfFormField._fallbackCtor) {
425
+ throw new Error(`Unknown field type: ${ft ?? '(none)'}`);
426
+ }
427
+ return new PdfFormField._fallbackCtor(options);
428
+ }
429
+ return new Ctor(options);
430
+ }
431
+ }
432
+ /** Backward compatible alias */
433
+ export { PdfFormField as PdfAcroFormField };
@@ -0,0 +1,7 @@
1
+ import { PdfFormField } from './pdf-form-field.js';
2
+ /**
3
+ * Signature form field subtype.
4
+ */
5
+ export declare class PdfSignatureFormField extends PdfFormField {
6
+ generateAppearance(): boolean;
7
+ }
@@ -0,0 +1,12 @@
1
+ import { PdfFormField } from './pdf-form-field.js';
2
+ /**
3
+ * Signature form field subtype.
4
+ */
5
+ export class PdfSignatureFormField extends PdfFormField {
6
+ static {
7
+ PdfFormField.registerFieldType('Sig', PdfSignatureFormField);
8
+ }
9
+ generateAppearance() {
10
+ return false;
11
+ }
12
+ }
@@ -0,0 +1,10 @@
1
+ import { PdfFormField } from './pdf-form-field.js';
2
+ /**
3
+ * Text form field subtype.
4
+ */
5
+ export declare class PdfTextFormField extends PdfFormField {
6
+ generateAppearance(options?: {
7
+ makeReadOnly?: boolean;
8
+ textYOffset?: number;
9
+ }): boolean;
10
+ }
@@ -0,0 +1,77 @@
1
+ import { PdfFormField } from './pdf-form-field.js';
2
+ import { PdfDefaultAppearance } from './pdf-default-appearance.js';
3
+ import { PdfTextAppearanceStream } from '../appearance/pdf-text-appearance-stream.js';
4
+ import { PdfDictionary } from '../../core/objects/pdf-dictionary.js';
5
+ import { PdfObjectReference } from '../../core/objects/pdf-object-reference.js';
6
+ /**
7
+ * Text form field subtype.
8
+ */
9
+ export class PdfTextFormField extends PdfFormField {
10
+ static {
11
+ PdfFormField.registerFieldType('Tx', PdfTextFormField, {
12
+ fallback: true,
13
+ });
14
+ }
15
+ generateAppearance(options) {
16
+ const rect = this.rect;
17
+ if (!rect || rect.length !== 4)
18
+ return false;
19
+ const da = this.defaultAppearance;
20
+ if (!da)
21
+ return false;
22
+ const parsed = PdfDefaultAppearance.parse(da);
23
+ if (!parsed)
24
+ return false;
25
+ let fontResources;
26
+ const drFontValue = this.form?.defaultResources?.get('Font');
27
+ const drFonts = drFontValue instanceof PdfDictionary ? drFontValue : undefined;
28
+ const daFontRef = this.form?.fontRefs?.get(parsed.fontName);
29
+ if (drFonts || daFontRef) {
30
+ // Build a fresh font dict using clean PdfObjectReferences (no
31
+ // pre/postTokens inherited from the original parse context).
32
+ const fontDict = new PdfDictionary();
33
+ if (drFonts) {
34
+ for (const [key, val] of drFonts.entries()) {
35
+ if (val instanceof PdfObjectReference) {
36
+ fontDict.set(key, new PdfObjectReference(val.objectNumber, val.generationNumber));
37
+ }
38
+ else if (val != null) {
39
+ fontDict.set(key, val);
40
+ }
41
+ }
42
+ }
43
+ if (daFontRef && !fontDict.has(parsed.fontName)) {
44
+ fontDict.set(parsed.fontName, new PdfObjectReference(daFontRef.objectNumber, daFontRef.generationNumber));
45
+ }
46
+ fontResources = new PdfDictionary();
47
+ fontResources.set('Font', fontDict);
48
+ }
49
+ const isUnicode = this.form?.isFontUnicode(parsed.fontName) ?? false;
50
+ const encodingMap = this.form?.fontEncodingMaps?.get(parsed.fontName);
51
+ const reverseEncodingMap = encodingMap
52
+ ? new Map(Array.from(encodingMap, ([code, char]) => [char, code]))
53
+ : undefined;
54
+ this._appearanceStream = new PdfTextAppearanceStream({
55
+ rect: rect,
56
+ value: this.value,
57
+ da: parsed,
58
+ multiline: this.multiline,
59
+ comb: this.comb,
60
+ maxLen: this.maxLen,
61
+ fontResources,
62
+ isUnicode,
63
+ reverseEncodingMap,
64
+ });
65
+ if (options?.makeReadOnly) {
66
+ this.readOnly = true;
67
+ if (!this.print)
68
+ this.print = true;
69
+ this.noZoom = true;
70
+ }
71
+ else {
72
+ if (!this.print)
73
+ this.print = true;
74
+ }
75
+ return true;
76
+ }
77
+ }
@@ -0,0 +1,26 @@
1
+ import type { PdfDefaultResourcesDictionary } from '../acroform.js';
2
+ import type { PdfObjectReference } from '../../core/objects/pdf-object-reference.js';
3
+ /**
4
+ * Field types for AcroForm fields
5
+ */
6
+ export declare const PdfFieldType: {
7
+ readonly Text: "Tx";
8
+ readonly Button: "Btn";
9
+ readonly Choice: "Ch";
10
+ readonly Signature: "Sig";
11
+ };
12
+ export type PdfFieldType = keyof typeof PdfFieldType;
13
+ /**
14
+ * Interface that PdfAcroForm implements, used by PdfFormField to avoid circular imports.
15
+ * Uses generic field type to avoid circular dependency.
16
+ */
17
+ export interface FormContext<TField = any> {
18
+ defaultResources: PdfDefaultResourcesDictionary | null;
19
+ defaultAppearance: string | null;
20
+ fontEncodingMaps: Map<string, Map<number, string>>;
21
+ /** Object references for all resolved fonts, keyed by resource name. */
22
+ fontRefs: Map<string, PdfObjectReference>;
23
+ fields: TField[];
24
+ isFontUnicode: (fontName: string) => boolean;
25
+ needAppearances: boolean;
26
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Field types for AcroForm fields
3
+ */
4
+ export const PdfFieldType = {
5
+ Text: 'Tx',
6
+ Button: 'Btn',
7
+ Choice: 'Ch',
8
+ Signature: 'Sig',
9
+ };
@@ -1,2 +1,6 @@
1
1
  export * from './manager.js';
2
- export * from './acroform.js';
2
+ export * from './pdf-acro-form.js';
3
+ export * from './fields/index.js';
4
+ export * from './appearance/index.js';
5
+ export * from './xfa/index.js';
6
+ export { PdfFontEncodingCache } from './pdf-font-encoding-cache.js';
@@ -1,2 +1,6 @@
1
1
  export * from './manager.js';
2
- export * from './acroform.js';
2
+ export * from './pdf-acro-form.js';
3
+ export * from './fields/index.js';
4
+ export * from './appearance/index.js';
5
+ export * from './xfa/index.js';
6
+ export { PdfFontEncodingCache } from './pdf-font-encoding-cache.js';
@@ -1,12 +1,19 @@
1
1
  import { PdfDocument } from '../pdf/pdf-document.js';
2
2
  import { PdfAcroForm } from './acroform.js';
3
+ import { PdfXfaForm } from './xfa/pdf-xfa-form.js';
3
4
  /**
4
5
  * Manages AcroForm fields in PDF documents.
5
6
  * Provides methods to read and write form field values.
6
7
  */
7
8
  export declare class PdfAcroFormManager {
8
9
  private document;
10
+ private _acroform;
9
11
  constructor(document: PdfDocument);
12
+ /**
13
+ * Gets the XFA form wrapper, loading it lazily on first access.
14
+ * @returns The PdfXfaForm or null if no XFA forms exist
15
+ */
16
+ getXfa(): Promise<PdfXfaForm | null>;
10
17
  /**
11
18
  * Checks if the document contains AcroForm fields.
12
19
  * @returns True if the document has AcroForm fields, false otherwise
@@ -17,10 +24,14 @@ export declare class PdfAcroFormManager {
17
24
  * @returns The AcroForm object or null if not found
18
25
  */
19
26
  read(): Promise<PdfAcroForm | null>;
27
+ /**
28
+ * Explicitly sets the XFA form instance, bypassing the lazy load on next write.
29
+ */
30
+ setXfa(xfa: PdfXfaForm): Promise<void>;
20
31
  /**
21
32
  * Writes the provided AcroForm to the associated PDF document.
22
33
  * @param acroForm The AcroForm instance to serialize into the document.
23
34
  * @throws Error If writing the AcroForm to the document fails.
24
35
  */
25
- write(acroForm: PdfAcroForm): Promise<void>;
36
+ write(acroForm?: PdfAcroForm): Promise<void>;
26
37
  }
@@ -5,9 +5,17 @@ import { PdfAcroForm } from './acroform.js';
5
5
  */
6
6
  export class PdfAcroFormManager {
7
7
  document;
8
+ _acroform = null;
8
9
  constructor(document) {
9
10
  this.document = document;
10
11
  }
12
+ /**
13
+ * Gets the XFA form wrapper, loading it lazily on first access.
14
+ * @returns The PdfXfaForm or null if no XFA forms exist
15
+ */
16
+ async getXfa() {
17
+ return (await (await this.read())?.getXfa()) || null;
18
+ }
11
19
  /**
12
20
  * Checks if the document contains AcroForm fields.
13
21
  * @returns True if the document has AcroForm fields, false otherwise
@@ -26,7 +34,17 @@ export class PdfAcroFormManager {
26
34
  * @returns The AcroForm object or null if not found
27
35
  */
28
36
  async read() {
29
- return await PdfAcroForm.fromDocument(this.document);
37
+ if (this._acroform)
38
+ return this._acroform;
39
+ this._acroform = await PdfAcroForm.fromDocument(this.document);
40
+ return this._acroform;
41
+ }
42
+ /**
43
+ * Explicitly sets the XFA form instance, bypassing the lazy load on next write.
44
+ */
45
+ async setXfa(xfa) {
46
+ ;
47
+ (await this.read())?.setXfa(xfa);
30
48
  }
31
49
  /**
32
50
  * Writes the provided AcroForm to the associated PDF document.
@@ -34,6 +52,6 @@ export class PdfAcroFormManager {
34
52
  * @throws Error If writing the AcroForm to the document fails.
35
53
  */
36
54
  async write(acroForm) {
37
- await acroForm.write(this.document);
55
+ await (acroForm ?? (await this.read()))?.write(this.document);
38
56
  }
39
57
  }