pdf-lite 1.4.0 → 1.6.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 (144) hide show
  1. package/EXAMPLES.md +51 -70
  2. package/README.md +1 -1
  3. package/dist/acroform/appearance/index.d.ts +4 -4
  4. package/dist/acroform/appearance/index.js +4 -4
  5. package/dist/acroform/appearance/{PdfAppearanceStream.d.ts → pdf-appearance-stream.d.ts} +9 -3
  6. package/dist/acroform/appearance/{PdfAppearanceStream.js → pdf-appearance-stream.js} +14 -5
  7. package/dist/acroform/appearance/{PdfButtonAppearanceStream.d.ts → pdf-button-appearance-stream.d.ts} +3 -2
  8. package/dist/acroform/appearance/pdf-button-appearance-stream.js +58 -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/{PdfTextAppearanceStream.d.ts → pdf-text-appearance-stream.d.ts} +7 -2
  14. package/dist/acroform/appearance/pdf-text-appearance-stream.js +104 -0
  15. package/dist/acroform/fields/index.d.ts +7 -7
  16. package/dist/acroform/fields/index.js +7 -7
  17. package/dist/acroform/fields/pdf-button-form-field.d.ts +14 -0
  18. package/dist/acroform/fields/pdf-button-form-field.js +70 -0
  19. package/dist/acroform/fields/pdf-choice-form-field.d.ts +19 -0
  20. package/dist/acroform/fields/pdf-choice-form-field.js +112 -0
  21. package/dist/acroform/fields/{PdfFormFieldFlags.d.ts → pdf-form-field-flags.d.ts} +5 -6
  22. package/dist/acroform/fields/{PdfFormFieldFlags.js → pdf-form-field-flags.js} +12 -18
  23. package/dist/acroform/fields/{PdfFormField.d.ts → pdf-form-field.d.ts} +37 -38
  24. package/dist/acroform/fields/pdf-form-field.js +519 -0
  25. package/dist/acroform/fields/{PdfSignatureFormField.d.ts → pdf-signature-form-field.d.ts} +1 -1
  26. package/dist/acroform/fields/{PdfSignatureFormField.js → pdf-signature-form-field.js} +1 -1
  27. package/dist/acroform/fields/{PdfTextFormField.d.ts → pdf-text-form-field.d.ts} +1 -1
  28. package/dist/acroform/fields/{PdfTextFormField.js → pdf-text-form-field.js} +11 -13
  29. package/dist/acroform/fields/types.d.ts +6 -1
  30. package/dist/acroform/index.d.ts +1 -3
  31. package/dist/acroform/index.js +1 -3
  32. package/dist/acroform/pdf-acro-form.d.ts +45 -0
  33. package/dist/acroform/pdf-acro-form.js +203 -0
  34. package/dist/acroform/xfa/index.d.ts +3 -3
  35. package/dist/acroform/xfa/index.js +2 -2
  36. package/dist/acroform/xfa/{PdfXfaData.d.ts → pdf-xfa-data.d.ts} +4 -3
  37. package/dist/acroform/xfa/{PdfXfaData.js → pdf-xfa-data.js} +16 -12
  38. package/dist/acroform/xfa/pdf-xfa-form.d.ts +16 -0
  39. package/dist/acroform/xfa/pdf-xfa-form.js +34 -0
  40. package/dist/annotations/index.d.ts +3 -4
  41. package/dist/annotations/index.js +3 -4
  42. package/dist/annotations/{PdfAnnotationFlags.d.ts → pdf-annotation-flags.d.ts} +3 -4
  43. package/dist/annotations/{PdfAnnotationFlags.js → pdf-annotation-flags.js} +5 -6
  44. package/dist/annotations/{PdfAnnotation.d.ts → pdf-annotation.d.ts} +31 -5
  45. package/dist/annotations/{PdfAnnotation.js → pdf-annotation.js} +31 -19
  46. package/dist/annotations/pdf-default-resources.d.ts +11 -0
  47. package/dist/annotations/pdf-default-resources.js +3 -0
  48. package/dist/annotations/{PdfWidgetAnnotation.d.ts → pdf-widget-annotation.d.ts} +1 -1
  49. package/dist/annotations/{PdfWidgetAnnotation.js → pdf-widget-annotation.js} +1 -1
  50. package/dist/core/decoder.js +1 -1
  51. package/dist/core/objects/pdf-array.d.ts +8 -1
  52. package/dist/core/objects/pdf-array.js +31 -0
  53. package/dist/core/objects/pdf-dictionary.d.ts +2 -0
  54. package/dist/core/objects/pdf-dictionary.js +14 -7
  55. package/dist/core/objects/pdf-hexadecimal.d.ts +1 -0
  56. package/dist/core/objects/pdf-hexadecimal.js +3 -3
  57. package/dist/core/objects/pdf-indirect-object.d.ts +18 -9
  58. package/dist/core/objects/pdf-indirect-object.js +75 -16
  59. package/dist/core/objects/pdf-number.d.ts +1 -0
  60. package/dist/core/objects/pdf-number.js +5 -4
  61. package/dist/core/objects/pdf-object-reference.d.ts +8 -1
  62. package/dist/core/objects/pdf-object-reference.js +14 -0
  63. package/dist/core/objects/pdf-object.d.ts +14 -0
  64. package/dist/core/objects/pdf-object.js +36 -0
  65. package/dist/core/objects/pdf-start-xref.d.ts +1 -0
  66. package/dist/core/objects/pdf-start-xref.js +4 -0
  67. package/dist/core/objects/pdf-stream.d.ts +44 -7
  68. package/dist/core/objects/pdf-stream.js +284 -26
  69. package/dist/core/objects/pdf-string.d.ts +1 -0
  70. package/dist/core/objects/pdf-string.js +3 -6
  71. package/dist/core/objects/pdf-trailer.d.ts +1 -0
  72. package/dist/core/objects/pdf-trailer.js +6 -3
  73. package/dist/core/objects/pdf-xref-table.js +1 -1
  74. package/dist/core/parser/incremental-parser.d.ts +0 -13
  75. package/dist/core/parser/incremental-parser.js +1 -18
  76. package/dist/core/ref.d.ts +3 -1
  77. package/dist/core/ref.js +8 -5
  78. package/dist/core/streams/object-stream.d.ts +1 -1
  79. package/dist/core/streams/object-stream.js +1 -1
  80. package/dist/core/tokens/token.d.ts +2 -1
  81. package/dist/core/tokens/token.js +3 -0
  82. package/dist/errors.d.ts +22 -0
  83. package/dist/errors.js +24 -0
  84. package/dist/fonts/index.d.ts +0 -1
  85. package/dist/fonts/index.js +0 -1
  86. package/dist/fonts/pdf-font.d.ts +94 -32
  87. package/dist/fonts/pdf-font.js +301 -83
  88. package/dist/index.d.ts +1 -0
  89. package/dist/index.js +1 -0
  90. package/dist/pdf/index.d.ts +2 -1
  91. package/dist/pdf/index.js +2 -1
  92. package/dist/pdf/pdf-document.d.ts +61 -36
  93. package/dist/pdf/pdf-document.js +315 -117
  94. package/dist/pdf/pdf-page.d.ts +50 -0
  95. package/dist/pdf/pdf-page.js +144 -0
  96. package/dist/pdf/pdf-pages.d.ts +28 -0
  97. package/dist/pdf/pdf-pages.js +94 -0
  98. package/dist/pdf/pdf-reader.d.ts +5 -1
  99. package/dist/pdf/pdf-reader.js +36 -2
  100. package/dist/pdf/pdf-revision.d.ts +3 -3
  101. package/dist/pdf/pdf-revision.js +7 -7
  102. package/dist/pdf/pdf-xref-lookup.js +34 -14
  103. package/dist/signing/document-security-store.d.ts +14 -17
  104. package/dist/signing/document-security-store.js +19 -34
  105. package/dist/signing/signer.d.ts +23 -8
  106. package/dist/signing/signer.js +51 -17
  107. package/dist/utils/encodePdfText.d.ts +17 -0
  108. package/dist/utils/encodePdfText.js +61 -0
  109. package/dist/utils/index.d.ts +1 -2
  110. package/dist/utils/index.js +1 -2
  111. package/dist/utils/needsCentralWhitespace.d.ts +10 -0
  112. package/dist/utils/needsCentralWhitespace.js +34 -0
  113. package/package.json +3 -3
  114. package/dist/acroform/PdfAcroForm.d.ts +0 -63
  115. package/dist/acroform/PdfAcroForm.js +0 -279
  116. package/dist/acroform/PdfFontEncodingCache.d.ts +0 -16
  117. package/dist/acroform/PdfFontEncodingCache.js +0 -75
  118. package/dist/acroform/acroform.d.ts +0 -9
  119. package/dist/acroform/acroform.js +0 -7
  120. package/dist/acroform/appearance/PdfButtonAppearanceStream.js +0 -54
  121. package/dist/acroform/appearance/PdfChoiceAppearanceStream.d.ts +0 -15
  122. package/dist/acroform/appearance/PdfChoiceAppearanceStream.js +0 -48
  123. package/dist/acroform/appearance/PdfTextAppearanceStream.js +0 -75
  124. package/dist/acroform/fields/PdfButtonFormField.d.ts +0 -9
  125. package/dist/acroform/fields/PdfButtonFormField.js +0 -35
  126. package/dist/acroform/fields/PdfChoiceFormField.d.ts +0 -9
  127. package/dist/acroform/fields/PdfChoiceFormField.js +0 -46
  128. package/dist/acroform/fields/PdfFormField.js +0 -499
  129. package/dist/acroform/manager.d.ts +0 -33
  130. package/dist/acroform/manager.js +0 -51
  131. package/dist/acroform/xfa/PdfXfaForm.d.ts +0 -12
  132. package/dist/acroform/xfa/PdfXfaForm.js +0 -64
  133. package/dist/annotations/PdfAnnotationWriter.d.ts +0 -20
  134. package/dist/annotations/PdfAnnotationWriter.js +0 -76
  135. package/dist/fonts/font-manager.d.ts +0 -127
  136. package/dist/fonts/font-manager.js +0 -378
  137. package/dist/pdf/errors.d.ts +0 -6
  138. package/dist/pdf/errors.js +0 -6
  139. package/dist/utils/predictors.d.ts +0 -113
  140. package/dist/utils/predictors.js +0 -279
  141. /package/dist/acroform/fields/{PdfDefaultAppearance.d.ts → pdf-default-appearance.d.ts} +0 -0
  142. /package/dist/acroform/fields/{PdfDefaultAppearance.js → pdf-default-appearance.js} +0 -0
  143. /package/dist/utils/{IterableReadableStream.d.ts → iterable-readable-stream.d.ts} +0 -0
  144. /package/dist/utils/{IterableReadableStream.js → iterable-readable-stream.js} +0 -0
@@ -1,46 +0,0 @@
1
- import { PdfFormField } from './PdfFormField.js';
2
- import { PdfDefaultAppearance } from './PdfDefaultAppearance.js';
3
- import { PdfChoiceAppearanceStream } from '../appearance/PdfChoiceAppearanceStream.js';
4
- import { PdfDictionary } from '../../core/objects/pdf-dictionary.js';
5
- /**
6
- * Choice form field subtype (dropdowns, list boxes).
7
- */
8
- export class PdfChoiceFormField extends PdfFormField {
9
- static {
10
- PdfFormField.registerFieldType('Ch', PdfChoiceFormField);
11
- }
12
- generateAppearance(options) {
13
- const rect = this.rect;
14
- if (!rect || rect.length !== 4)
15
- return false;
16
- const da = this.defaultAppearance;
17
- if (!da)
18
- return false;
19
- const value = this.value;
20
- if (!value)
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
- if (drFonts) {
29
- fontResources = new PdfDictionary();
30
- fontResources.set('Font', drFonts);
31
- }
32
- this._appearanceStream = new PdfChoiceAppearanceStream({
33
- rect: rect,
34
- value,
35
- da: parsed,
36
- flags: this.flags,
37
- fontResources,
38
- });
39
- if (options?.makeReadOnly) {
40
- this.readOnly = true;
41
- this.print = true;
42
- this.noZoom = true;
43
- }
44
- return true;
45
- }
46
- }
@@ -1,499 +0,0 @@
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/PdfWidgetAnnotation.js';
8
- import { PdfDefaultAppearance } from './PdfDefaultAppearance.js';
9
- import { PdfFieldType as PdfFieldTypeConst } from './types.js';
10
- /**
11
- * Abstract base form field class. Extends PdfWidgetAnnotation with form-specific properties:
12
- * FT, V, DA, Ff, T (name), field hierarchy (parent/children/siblings).
13
- * Subclasses must implement generateAppearance().
14
- */
15
- export class PdfFormField extends PdfWidgetAnnotation {
16
- _parent;
17
- defaultGenerateAppearance = true;
18
- _appearanceStream;
19
- _appearanceStreamYes;
20
- form;
21
- constructor(options) {
22
- super({ other: options?.other });
23
- this.form = options?.form;
24
- if (options?.parent) {
25
- this._parent = options.parent;
26
- }
27
- }
28
- get parent() {
29
- if (this._parent)
30
- return this._parent;
31
- if (!this.form)
32
- return undefined;
33
- return this.form.fields.find((f) => f !== this &&
34
- f.kids.some((k) => k.objectNumber === this.objectNumber &&
35
- k.generationNumber === this.generationNumber));
36
- }
37
- set parent(field) {
38
- if (this._parent) {
39
- this._parent.kids = this._parent.kids.filter((k) => k.objectNumber !== this.objectNumber ||
40
- k.generationNumber !== this.generationNumber);
41
- }
42
- this._parent = field;
43
- if (field) {
44
- const alreadyInKids = field.kids.some((k) => k.objectNumber === this.objectNumber &&
45
- k.generationNumber === this.generationNumber);
46
- if (!alreadyInKids) {
47
- field.kids = [...field.kids, this.reference];
48
- }
49
- }
50
- }
51
- get children() {
52
- if (!this.form)
53
- return [];
54
- return this.form.fields.filter((f) => f.parent === this);
55
- }
56
- set children(fields) {
57
- for (const child of this.children) {
58
- child._parent = undefined;
59
- }
60
- this.kids = fields.map((f) => f.reference);
61
- for (const child of fields) {
62
- child._parent = this;
63
- }
64
- }
65
- get siblings() {
66
- return this.parent?.children ?? [];
67
- }
68
- get encodingMap() {
69
- const fontName = this.fontName;
70
- if (!fontName)
71
- return undefined;
72
- return this.form?.fontEncodingMaps?.get(fontName);
73
- }
74
- get fieldType() {
75
- const ft = this.content.get('FT')?.as(PdfName)?.value ??
76
- this.parent?.content.get('FT')?.as(PdfName)?.value;
77
- switch (ft) {
78
- case 'Tx':
79
- return 'Text';
80
- case 'Btn':
81
- return 'Button';
82
- case 'Ch':
83
- return 'Choice';
84
- case 'Sig':
85
- return 'Signature';
86
- default:
87
- return null;
88
- }
89
- }
90
- set fieldType(type) {
91
- if (type === null) {
92
- this.content.delete('FT');
93
- }
94
- else {
95
- this.content.set('FT', new PdfName(PdfFieldTypeConst[type]));
96
- }
97
- }
98
- get name() {
99
- const parentName = this.parent?.name ?? '';
100
- const ownName = this.content.get('T')?.as(PdfString)?.value ?? '';
101
- if (parentName && ownName) {
102
- return `${parentName}.${ownName}`;
103
- }
104
- return parentName || ownName;
105
- }
106
- set name(name) {
107
- this.content.set('T', new PdfString(name));
108
- }
109
- get defaultValue() {
110
- const dv = this.content.get('DV') ?? this.parent?.content.get('DV');
111
- if (dv instanceof PdfString)
112
- return dv.value;
113
- if (dv instanceof PdfName)
114
- return dv.value;
115
- return '';
116
- }
117
- set defaultValue(val) {
118
- if (this.fieldType === 'Button') {
119
- this.content.set('DV', new PdfName(val));
120
- }
121
- else {
122
- this.content.set('DV', new PdfString(val));
123
- }
124
- }
125
- get value() {
126
- const v = this.content.get('V') ?? this.parent?.content.get('V');
127
- if (v instanceof PdfString) {
128
- if (v.isUTF16BE)
129
- return v.value;
130
- if (this.encodingMap) {
131
- return decodeWithFontEncoding(v.raw, this.encodingMap);
132
- }
133
- return v.value;
134
- }
135
- else if (v instanceof PdfName) {
136
- return v.value;
137
- }
138
- return '';
139
- }
140
- set value(val) {
141
- if (this.value === val)
142
- return;
143
- const fieldParent = this.parent?.content.get('FT')
144
- ? this.parent
145
- : undefined;
146
- const fieldType = this.fieldType;
147
- if (fieldType === 'Button') {
148
- val = val instanceof PdfString ? val.value : val;
149
- if (val.trim() === '') {
150
- this.content.delete('V');
151
- fieldParent?.content.delete('V');
152
- this.content.delete('AS');
153
- return;
154
- }
155
- this.content.set('V', new PdfName(val));
156
- fieldParent?.content.set('V', new PdfName(val));
157
- this.content.set('AS', new PdfName(val));
158
- }
159
- else {
160
- const pdfVal = val instanceof PdfString ? val : new PdfString(val);
161
- this.content.set('V', pdfVal);
162
- fieldParent?.content.set('V', pdfVal);
163
- }
164
- if (this.defaultGenerateAppearance) {
165
- this.generateAppearance();
166
- for (const sibling of this.siblings) {
167
- if (sibling !== this &&
168
- sibling.rect &&
169
- sibling.defaultGenerateAppearance) {
170
- sibling.generateAppearance();
171
- }
172
- }
173
- }
174
- }
175
- get checked() {
176
- if (this.fieldType === 'Button') {
177
- const v = this.content.get('V') ?? this.parent?.content.get('V');
178
- return v instanceof PdfName && v.value === 'Yes';
179
- }
180
- return false;
181
- }
182
- set checked(isChecked) {
183
- if (this.fieldType === 'Button') {
184
- const target = this.parent ?? this;
185
- if (isChecked) {
186
- target.content.set('V', new PdfName('Yes'));
187
- this.content.set('AS', new PdfName('Yes'));
188
- }
189
- else {
190
- target.content.set('V', new PdfName('Off'));
191
- this.content.set('AS', new PdfName('Off'));
192
- }
193
- }
194
- }
195
- get fontSize() {
196
- const da = this.defaultAppearance || '';
197
- const parsed = PdfDefaultAppearance.parse(da);
198
- return parsed?.fontSize ?? null;
199
- }
200
- set fontSize(size) {
201
- const da = this.defaultAppearance || '';
202
- if (!da) {
203
- this.content.set('DA', new PdfDefaultAppearance('F1', size, '0 g'));
204
- return;
205
- }
206
- const parsed = PdfDefaultAppearance.parse(da);
207
- if (parsed) {
208
- parsed.fontSize = size;
209
- this.content.set('DA', parsed);
210
- }
211
- }
212
- get fontName() {
213
- const da = this.defaultAppearance || '';
214
- const parsed = PdfDefaultAppearance.parse(da);
215
- return parsed?.fontName ?? null;
216
- }
217
- set fontName(fontName) {
218
- const da = this.defaultAppearance || '';
219
- if (!da) {
220
- this.content.set('DA', new PdfDefaultAppearance(fontName, 12, '0 g'));
221
- return;
222
- }
223
- const parsed = PdfDefaultAppearance.parse(da);
224
- if (parsed) {
225
- parsed.fontName = fontName;
226
- this.content.set('DA', parsed);
227
- }
228
- }
229
- set font(font) {
230
- if (font === null) {
231
- this.content.set('DA', new PdfString(''));
232
- return;
233
- }
234
- const resourceName = font.resourceName;
235
- const currentSize = this.fontSize ?? 12;
236
- const da = this.defaultAppearance || '';
237
- if (!da) {
238
- this.content.set('DA', new PdfDefaultAppearance(resourceName, currentSize, '0 g'));
239
- return;
240
- }
241
- const parsed = PdfDefaultAppearance.parse(da);
242
- if (parsed) {
243
- parsed.fontName = resourceName;
244
- this.content.set('DA', parsed);
245
- }
246
- }
247
- // Field flags (Ff) - with parent inheritance
248
- get flags() {
249
- const own = this.content.get('Ff')?.as(PdfNumber)?.value;
250
- if (own !== undefined)
251
- return own;
252
- return this.parent?.flags ?? 0;
253
- }
254
- set flags(flags) {
255
- this.content.set('Ff', new PdfNumber(flags));
256
- }
257
- getFlag(bit) {
258
- return (this.flags & bit) !== 0;
259
- }
260
- setFlag(bit, value) {
261
- if (value) {
262
- this.flags = this.flags | bit;
263
- }
264
- else {
265
- this.flags = this.flags & ~bit;
266
- }
267
- }
268
- get readOnly() {
269
- return this.getFlag(1);
270
- }
271
- set readOnly(v) {
272
- this.setFlag(1, v);
273
- }
274
- get required() {
275
- return this.getFlag(2);
276
- }
277
- set required(v) {
278
- this.setFlag(2, v);
279
- }
280
- get multiline() {
281
- return this.getFlag(4096);
282
- }
283
- set multiline(v) {
284
- this.setFlag(4096, v);
285
- }
286
- get password() {
287
- return this.getFlag(8192);
288
- }
289
- set password(v) {
290
- this.setFlag(8192, v);
291
- }
292
- get comb() {
293
- return this.getFlag(16777216);
294
- }
295
- get combField() {
296
- return this.getFlag(16777216);
297
- }
298
- set combField(v) {
299
- this.setFlag(16777216, v);
300
- }
301
- get combo() {
302
- return this.getFlag(131072);
303
- }
304
- set combo(v) {
305
- this.setFlag(131072, v);
306
- }
307
- get radio() {
308
- return this.getFlag(32768);
309
- }
310
- set radio(v) {
311
- this.setFlag(32768, v);
312
- }
313
- get noToggleToOff() {
314
- return this.getFlag(16384);
315
- }
316
- set noToggleToOff(v) {
317
- this.setFlag(16384, v);
318
- }
319
- get noExport() {
320
- return this.getFlag(4);
321
- }
322
- set noExport(v) {
323
- this.setFlag(4, v);
324
- }
325
- get pushButton() {
326
- return this.getFlag(65536);
327
- }
328
- set pushButton(v) {
329
- this.setFlag(65536, v);
330
- }
331
- get edit() {
332
- return this.getFlag(262144);
333
- }
334
- set edit(v) {
335
- this.setFlag(262144, v);
336
- }
337
- get sort() {
338
- return this.getFlag(524288);
339
- }
340
- set sort(v) {
341
- this.setFlag(524288, v);
342
- }
343
- get multiSelect() {
344
- return this.getFlag(2097152);
345
- }
346
- set multiSelect(v) {
347
- this.setFlag(2097152, v);
348
- }
349
- get doNotSpellCheck() {
350
- return this.getFlag(4194304);
351
- }
352
- set doNotSpellCheck(v) {
353
- this.setFlag(4194304, v);
354
- }
355
- get doNotScroll() {
356
- return this.getFlag(8388608);
357
- }
358
- set doNotScroll(v) {
359
- this.setFlag(8388608, v);
360
- }
361
- get commitOnSelChange() {
362
- return this.getFlag(67108864);
363
- }
364
- set commitOnSelChange(v) {
365
- this.setFlag(67108864, v);
366
- }
367
- get quadding() {
368
- return (this.content.get('Q')?.as(PdfNumber)?.value ??
369
- this.parent?.content.get('Q')?.as(PdfNumber)?.value ??
370
- 0);
371
- }
372
- set quadding(q) {
373
- this.content.set('Q', new PdfNumber(q));
374
- }
375
- get options() {
376
- const opt = this.content.get('Opt')?.as((PdfArray)) ??
377
- this.parent?.content.get('Opt')?.as((PdfArray));
378
- if (!opt)
379
- return [];
380
- return opt.items.map((item) => item.value);
381
- }
382
- set options(options) {
383
- if (options.length === 0) {
384
- this.content.delete('Opt');
385
- return;
386
- }
387
- const optArray = new PdfArray(options.map((opt) => new PdfString(opt)));
388
- this.content.set('Opt', optArray);
389
- }
390
- get defaultAppearance() {
391
- return (this.content.get('DA')?.as(PdfString)?.value ??
392
- this.parent?.content.get('DA')?.as(PdfString)?.value ??
393
- this.form?.defaultAppearance ??
394
- null);
395
- }
396
- set defaultAppearance(da) {
397
- this.content.set('DA', new PdfString(da));
398
- }
399
- get maxLen() {
400
- return this.content.get('MaxLen')?.as(PdfNumber)?.value ?? null;
401
- }
402
- set maxLen(maxLen) {
403
- if (maxLen === null) {
404
- this.content.delete('MaxLen');
405
- }
406
- else {
407
- this.content.set('MaxLen', new PdfNumber(maxLen));
408
- }
409
- }
410
- get kids() {
411
- const kidsArray = this.content
412
- .get('Kids')
413
- ?.as((PdfArray));
414
- if (!kidsArray)
415
- return [];
416
- return kidsArray.items;
417
- }
418
- set kids(kids) {
419
- if (kids.length === 0) {
420
- this.content.delete('Kids');
421
- return;
422
- }
423
- const kidsArray = new PdfArray(kids);
424
- this.content.set('Kids', kidsArray);
425
- }
426
- getAppearanceStream() {
427
- if (this.fieldType === 'Button') {
428
- if (this.checked && this._appearanceStreamYes) {
429
- return this._appearanceStreamYes?.content;
430
- }
431
- return this._appearanceStream?.content;
432
- }
433
- return this._appearanceStream?.content;
434
- }
435
- getAppearanceStreamsForWriting() {
436
- if (!this._appearanceStream)
437
- return undefined;
438
- return {
439
- primary: this._appearanceStream,
440
- secondary: this.fieldType === 'Button'
441
- ? this._appearanceStreamYes
442
- : undefined,
443
- };
444
- }
445
- setAppearanceReference(appearanceStreamRef, appearanceStreamYesRef) {
446
- let apDict = this.appearanceStreamDict;
447
- if (!apDict) {
448
- apDict = new PdfDictionary();
449
- this.appearanceStreamDict = apDict;
450
- }
451
- if (appearanceStreamYesRef && this.fieldType === 'Button') {
452
- const stateDict = new PdfDictionary();
453
- stateDict.set('Off', appearanceStreamRef);
454
- stateDict.set('Yes', appearanceStreamYesRef);
455
- apDict.set('N', stateDict);
456
- }
457
- else {
458
- apDict.set('N', appearanceStreamRef);
459
- }
460
- }
461
- static _fallbackCtor;
462
- static _registry = new Map();
463
- static registerFieldType(ft, ctor, options) {
464
- PdfFormField._registry.set(ft, ctor);
465
- if (options?.fallback) {
466
- PdfFormField._fallbackCtor = ctor;
467
- }
468
- }
469
- static create(options) {
470
- let ft;
471
- try {
472
- const dict = options.other.content.as(PdfDictionary);
473
- ft = dict.get('FT')?.as(PdfName)?.value;
474
- }
475
- catch {
476
- // content may not be a dictionary
477
- }
478
- if (!ft && options.parent) {
479
- try {
480
- ft = options.parent.content.get('FT')?.as(PdfName)?.value;
481
- }
482
- catch {
483
- // ignore
484
- }
485
- }
486
- const Ctor = ft ? PdfFormField._registry.get(ft) : undefined;
487
- if (!Ctor) {
488
- // Fields without FT are parent/container nodes — use the
489
- // fallback type (typically Text) as a concrete stand-in.
490
- if (!PdfFormField._fallbackCtor) {
491
- throw new Error(`Unknown field type: ${ft ?? '(none)'}`);
492
- }
493
- return new PdfFormField._fallbackCtor(options);
494
- }
495
- return new Ctor(options);
496
- }
497
- }
498
- /** Backward compatible alias */
499
- export { PdfFormField as PdfAcroFormField };
@@ -1,33 +0,0 @@
1
- import { PdfDocument } from '../pdf/pdf-document.js';
2
- import { PdfAcroForm } from './acroform.js';
3
- import { PdfXfaForm } from './xfa/PdfXfaForm.js';
4
- /**
5
- * Manages AcroForm fields in PDF documents.
6
- * Provides methods to read and write form field values.
7
- */
8
- export declare class PdfAcroFormManager {
9
- private document;
10
- private _xfa;
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>;
17
- /**
18
- * Checks if the document contains AcroForm fields.
19
- * @returns True if the document has AcroForm fields, false otherwise
20
- */
21
- exists(): Promise<boolean>;
22
- /**
23
- * Gets the AcroForm object from the document catalog.
24
- * @returns The AcroForm object or null if not found
25
- */
26
- read(): Promise<PdfAcroForm | null>;
27
- /**
28
- * Writes the provided AcroForm to the associated PDF document.
29
- * @param acroForm The AcroForm instance to serialize into the document.
30
- * @throws Error If writing the AcroForm to the document fails.
31
- */
32
- write(acroForm: PdfAcroForm): Promise<void>;
33
- }
@@ -1,51 +0,0 @@
1
- import { PdfAcroForm } from './acroform.js';
2
- import { PdfXfaForm } from './xfa/PdfXfaForm.js';
3
- /**
4
- * Manages AcroForm fields in PDF documents.
5
- * Provides methods to read and write form field values.
6
- */
7
- export class PdfAcroFormManager {
8
- document;
9
- _xfa = undefined;
10
- constructor(document) {
11
- this.document = document;
12
- }
13
- /**
14
- * Gets the XFA form wrapper, loading it lazily on first access.
15
- * @returns The PdfXfaForm or null if no XFA forms exist
16
- */
17
- async getXfa() {
18
- if (this._xfa === undefined) {
19
- this._xfa = await PdfXfaForm.fromDocument(this.document);
20
- }
21
- return this._xfa;
22
- }
23
- /**
24
- * Checks if the document contains AcroForm fields.
25
- * @returns True if the document has AcroForm fields, false otherwise
26
- */
27
- async exists() {
28
- try {
29
- const acroForm = await this.read();
30
- return acroForm !== null;
31
- }
32
- catch {
33
- return false;
34
- }
35
- }
36
- /**
37
- * Gets the AcroForm object from the document catalog.
38
- * @returns The AcroForm object or null if not found
39
- */
40
- async read() {
41
- return await PdfAcroForm.fromDocument(this.document);
42
- }
43
- /**
44
- * Writes the provided AcroForm to the associated PDF document.
45
- * @param acroForm The AcroForm instance to serialize into the document.
46
- * @throws Error If writing the AcroForm to the document fails.
47
- */
48
- async write(acroForm) {
49
- await acroForm.write(this.document);
50
- }
51
- }
@@ -1,12 +0,0 @@
1
- import { PdfDocument } from '../../pdf/pdf-document.js';
2
- import { PdfArray } from '../../core/objects/pdf-array.js';
3
- import { PdfXfaData } from './PdfXfaData.js';
4
- /**
5
- * Typed wrapper around the XFA name/stream-ref array.
6
- * Holds eagerly-loaded references to component streams like datasets.
7
- */
8
- export declare class PdfXfaForm extends PdfArray {
9
- datasets: PdfXfaData | null;
10
- static fromDocument(document: PdfDocument): Promise<PdfXfaForm | null>;
11
- write(document: PdfDocument): void;
12
- }
@@ -1,64 +0,0 @@
1
- import { PdfArray } from '../../core/objects/pdf-array.js';
2
- import { PdfDictionary } from '../../core/objects/pdf-dictionary.js';
3
- import { PdfString } from '../../core/objects/pdf-string.js';
4
- import { PdfObjectReference } from '../../core/objects/pdf-object-reference.js';
5
- import { PdfIndirectObject } from '../../core/objects/pdf-indirect-object.js';
6
- import { PdfXfaData } from './PdfXfaData.js';
7
- /**
8
- * Typed wrapper around the XFA name/stream-ref array.
9
- * Holds eagerly-loaded references to component streams like datasets.
10
- */
11
- export class PdfXfaForm extends PdfArray {
12
- datasets = null;
13
- static async fromDocument(document) {
14
- const catalog = document.root;
15
- const acroFormRef = catalog.content.get('AcroForm');
16
- if (!acroFormRef)
17
- return null;
18
- let acroFormDict;
19
- if (acroFormRef instanceof PdfObjectReference) {
20
- const acroFormObject = await document.readObject({
21
- objectNumber: acroFormRef.objectNumber,
22
- generationNumber: acroFormRef.generationNumber,
23
- });
24
- if (!acroFormObject)
25
- return null;
26
- acroFormDict = acroFormObject.content.as(PdfDictionary);
27
- }
28
- else if (acroFormRef instanceof PdfDictionary) {
29
- acroFormDict = acroFormRef;
30
- }
31
- else {
32
- return null;
33
- }
34
- const xfaArray = acroFormDict.get('XFA');
35
- if (!(xfaArray instanceof PdfArray))
36
- return null;
37
- const form = new PdfXfaForm(xfaArray.items.slice());
38
- // Find the datasets reference in the name/ref pairs
39
- const items = xfaArray.items;
40
- for (let i = 0; i < items.length - 1; i += 2) {
41
- const name = items[i];
42
- const ref = items[i + 1];
43
- if (name instanceof PdfString &&
44
- name.value === 'datasets' &&
45
- ref instanceof PdfObjectReference) {
46
- const datasetObject = (await document.readObject({
47
- objectNumber: ref.objectNumber,
48
- generationNumber: ref.generationNumber,
49
- allowUnindexed: true,
50
- }))?.as((PdfIndirectObject));
51
- if (datasetObject) {
52
- form.datasets = new PdfXfaData(datasetObject);
53
- }
54
- break;
55
- }
56
- }
57
- return form;
58
- }
59
- write(document) {
60
- if (this.datasets?.isModified()) {
61
- document.add(this.datasets);
62
- }
63
- }
64
- }