pdf-lite 1.2.1 → 1.3.1

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 (52) hide show
  1. package/EXAMPLES.md +1 -1
  2. package/dist/acroform/acroform.d.ts +272 -16
  3. package/dist/acroform/acroform.js +1084 -144
  4. package/dist/acroform/manager.d.ts +2 -2
  5. package/dist/acroform/manager.js +3 -3
  6. package/dist/core/decoder.d.ts +1 -1
  7. package/dist/core/decoder.js +3 -3
  8. package/dist/core/index.d.ts +2 -2
  9. package/dist/core/index.js +2 -2
  10. package/dist/core/objects/pdf-array.d.ts +1 -0
  11. package/dist/core/objects/pdf-array.js +4 -0
  12. package/dist/core/objects/pdf-dictionary.d.ts +1 -0
  13. package/dist/core/objects/pdf-dictionary.js +12 -0
  14. package/dist/core/objects/pdf-hexadecimal.d.ts +9 -2
  15. package/dist/core/objects/pdf-hexadecimal.js +25 -5
  16. package/dist/core/objects/pdf-indirect-object.d.ts +5 -3
  17. package/dist/core/objects/pdf-indirect-object.js +23 -5
  18. package/dist/core/objects/pdf-number.js +3 -0
  19. package/dist/core/objects/pdf-object.d.ts +6 -0
  20. package/dist/core/objects/pdf-object.js +10 -0
  21. package/dist/core/objects/pdf-stream.js +3 -0
  22. package/dist/core/objects/pdf-string.d.ts +11 -1
  23. package/dist/core/objects/pdf-string.js +24 -6
  24. package/dist/core/ref.d.ts +5 -0
  25. package/dist/core/ref.js +14 -0
  26. package/dist/core/serializer.d.ts +1 -1
  27. package/dist/core/serializer.js +1 -1
  28. package/dist/core/tokeniser.d.ts +2 -2
  29. package/dist/core/tokeniser.js +37 -75
  30. package/dist/core/tokens/hexadecimal-token.d.ts +8 -1
  31. package/dist/core/tokens/hexadecimal-token.js +20 -2
  32. package/dist/core/tokens/name-token.js +0 -3
  33. package/dist/core/tokens/string-token.d.ts +8 -1
  34. package/dist/core/tokens/string-token.js +20 -2
  35. package/dist/fonts/font-manager.js +6 -8
  36. package/dist/pdf/pdf-document.d.ts +12 -11
  37. package/dist/pdf/pdf-document.js +50 -42
  38. package/dist/pdf/pdf-revision.d.ts +33 -4
  39. package/dist/pdf/pdf-revision.js +100 -26
  40. package/dist/pdf/pdf-xref-lookup.js +3 -2
  41. package/dist/utils/decodeWithFontEncoding.d.ts +20 -0
  42. package/dist/utils/decodeWithFontEncoding.js +67 -0
  43. package/dist/utils/escapeString.d.ts +1 -1
  44. package/dist/utils/escapeString.js +12 -3
  45. package/dist/utils/glyphNameToUnicode.d.ts +10 -0
  46. package/dist/utils/glyphNameToUnicode.js +4292 -0
  47. package/dist/xfa/manager.js +2 -4
  48. package/package.json +1 -1
  49. /package/dist/core/{incremental-parser.d.ts → parser/incremental-parser.d.ts} +0 -0
  50. /package/dist/core/{incremental-parser.js → parser/incremental-parser.js} +0 -0
  51. /package/dist/core/{parser.d.ts → parser/parser.d.ts} +0 -0
  52. /package/dist/core/{parser.js → parser/parser.js} +0 -0
package/EXAMPLES.md CHANGED
@@ -1109,7 +1109,7 @@ console.log('Created form-empty.pdf with empty form fields')
1109
1109
  const emptyFormBytes = await fs.readFile(`${tmpFolder}/form-empty.pdf`)
1110
1110
  const filledDocument = await PdfDocument.fromBytes([emptyFormBytes])
1111
1111
 
1112
- const acroform = await filledDocument.acroForm.getAcroForm()
1112
+ const acroform = await filledDocument.acroForm.read()
1113
1113
  if (!acroform) {
1114
1114
  throw new Error('No AcroForm found in the document')
1115
1115
  }
@@ -8,6 +8,7 @@ import { PdfName } from '../core/objects/pdf-name.js';
8
8
  import { PdfBoolean } from '../core/objects/pdf-boolean.js';
9
9
  import { PdfNumber } from '../core/objects/pdf-number.js';
10
10
  import { PdfFont } from '../fonts/pdf-font.js';
11
+ import { PdfStream } from '../core/objects/pdf-stream.js';
11
12
  /**
12
13
  * Field types for AcroForm fields
13
14
  */
@@ -17,9 +18,31 @@ export declare const PdfFieldType: {
17
18
  readonly Choice: "Ch";
18
19
  readonly Signature: "Sig";
19
20
  };
20
- export type PdfFieldType = (typeof PdfFieldType)[keyof typeof PdfFieldType];
21
- export declare class PdfAcroFormField extends PdfDictionary<{
22
- FT: PdfName<PdfFieldType>;
21
+ export type PdfFieldType = keyof typeof PdfFieldType;
22
+ export type PdfAppearanceStreamDictionary = PdfDictionary<{
23
+ /** Appearance streams for different states */
24
+ N: PdfObjectReference | PdfDictionary;
25
+ R?: PdfObjectReference | PdfDictionary;
26
+ D?: PdfObjectReference | PdfDictionary;
27
+ }>;
28
+ export type PdfDefaultResourcesDictionary = PdfDictionary<{
29
+ /** Font resources used in the form */
30
+ Font?: PdfDictionary;
31
+ /** Procedure sets */
32
+ ProcSet?: PdfArray;
33
+ /** Extended graphics states */
34
+ ExtGState?: PdfDictionary;
35
+ /** Color spaces */
36
+ ColorSpace?: PdfDictionary;
37
+ /** Patterns */
38
+ Pattern?: PdfDictionary;
39
+ /** Shading dictionaries */
40
+ Shading?: PdfDictionary;
41
+ /** External objects */
42
+ XObject?: PdfDictionary;
43
+ }>;
44
+ export declare class PdfAcroFormField extends PdfIndirectObject<PdfDictionary<{
45
+ FT: PdfName<(typeof PdfFieldType)[keyof typeof PdfFieldType]>;
23
46
  T?: PdfString;
24
47
  V?: PdfString | PdfName;
25
48
  DV?: PdfString | PdfName;
@@ -34,12 +57,25 @@ export declare class PdfAcroFormField extends PdfDictionary<{
34
57
  MK?: PdfDictionary;
35
58
  Type?: PdfName<'Annot'>;
36
59
  Subtype?: PdfName<'Widget'>;
37
- }> {
60
+ AP?: PdfAppearanceStreamDictionary;
61
+ Q?: PdfNumber;
62
+ MaxLen?: PdfNumber;
63
+ Opt?: PdfArray<PdfString>;
64
+ }>> {
38
65
  parent?: PdfAcroFormField;
39
- readonly container?: PdfIndirectObject;
66
+ defaultGenerateAppearance: boolean;
67
+ private _appearanceStream?;
68
+ private _appearanceStreamYes?;
69
+ private form?;
40
70
  constructor(options?: {
41
- container?: PdfIndirectObject;
71
+ other?: PdfIndirectObject;
72
+ form?: PdfAcroForm;
42
73
  });
74
+ get encodingMap(): Map<number, string> | undefined;
75
+ /**
76
+ * Convenience method to check if field dictionary is modified
77
+ */
78
+ isModified(): boolean;
43
79
  /**
44
80
  * Gets the field type
45
81
  */
@@ -68,7 +104,7 @@ export declare class PdfAcroFormField extends PdfDictionary<{
68
104
  */
69
105
  set defaultValue(val: string);
70
106
  get value(): string;
71
- set value(val: string);
107
+ set value(val: string | PdfString);
72
108
  get checked(): boolean;
73
109
  set checked(isChecked: boolean);
74
110
  get fontSize(): number | null;
@@ -89,11 +125,19 @@ export declare class PdfAcroFormField extends PdfDictionary<{
89
125
  */
90
126
  set flags(flags: number);
91
127
  /**
92
- * Checks if the field is read-only
128
+ * Gets annotation flags (for visual appearance and behavior)
129
+ */
130
+ get annotationFlags(): number;
131
+ /**
132
+ * Sets annotation flags
133
+ */
134
+ set annotationFlags(flags: number);
135
+ /**
136
+ * Checks if the field is read-only (Ff bit 1)
93
137
  */
94
138
  get readOnly(): boolean;
95
139
  /**
96
- * Sets the field as read-only or editable
140
+ * Sets the field as read-only or editable (Ff bit 1)
97
141
  */
98
142
  set readOnly(isReadOnly: boolean);
99
143
  /**
@@ -120,23 +164,216 @@ export declare class PdfAcroFormField extends PdfDictionary<{
120
164
  * Sets the field as a password field (for text fields)
121
165
  */
122
166
  set password(isPassword: boolean);
167
+ /**
168
+ * Checks if the field is a comb field (characters distributed evenly across cells)
169
+ */
170
+ get comb(): boolean;
171
+ /**
172
+ * Gets the quadding (text alignment) for this field.
173
+ * 0 = left-justified, 1 = centered, 2 = right-justified
174
+ */
175
+ get quadding(): number;
176
+ /**
177
+ * Sets the quadding (text alignment) for this field.
178
+ * 0 = left-justified, 1 = centered, 2 = right-justified
179
+ */
180
+ set quadding(q: number);
181
+ /**
182
+ * Gets the options for choice fields (dropdowns, list boxes).
183
+ * Returns an array of option strings.
184
+ */
185
+ get options(): string[];
186
+ /**
187
+ * Sets the options for choice fields (dropdowns, list boxes).
188
+ * Pass an array of strings.
189
+ */
190
+ set options(options: string[]);
191
+ get defaultAppearance(): string | null;
192
+ set defaultAppearance(da: string);
193
+ set combo(isCombo: boolean);
194
+ get combo(): boolean;
195
+ get radio(): boolean;
196
+ set radio(isRadio: boolean);
197
+ get noToggleToOff(): boolean;
198
+ set noToggleToOff(noToggle: boolean);
199
+ get combField(): boolean;
200
+ set combField(isComb: boolean);
201
+ get maxLen(): number | null;
202
+ set maxLen(maxLen: number | null);
203
+ /**
204
+ * If true, the annotation is invisible (F bit 1)
205
+ */
206
+ get invisible(): boolean;
207
+ set invisible(value: boolean);
208
+ /**
209
+ * If true, the annotation is hidden (F bit 2)
210
+ */
211
+ get hidden(): boolean;
212
+ set hidden(value: boolean);
213
+ /**
214
+ * If true, print the annotation when printing (F bit 3)
215
+ */
216
+ get print(): boolean;
217
+ set print(value: boolean);
218
+ /**
219
+ * If true, do not zoom annotation when zooming (F bit 4)
220
+ */
221
+ get noZoom(): boolean;
222
+ set noZoom(value: boolean);
223
+ /**
224
+ * If true, do not rotate annotation when rotating (F bit 5)
225
+ */
226
+ get noRotate(): boolean;
227
+ set noRotate(value: boolean);
228
+ /**
229
+ * If true, do not display annotation on screen (F bit 6)
230
+ */
231
+ get noView(): boolean;
232
+ set noView(value: boolean);
233
+ /**
234
+ * If true, annotation is locked (F bit 8)
235
+ */
236
+ get locked(): boolean;
237
+ set locked(value: boolean);
238
+ /**
239
+ * If true, field value should not be exported (Ff bit 3)
240
+ */
241
+ get noExport(): boolean;
242
+ set noExport(value: boolean);
243
+ /**
244
+ * If true, field is a pushbutton (Ff bit 17)
245
+ */
246
+ get pushButton(): boolean;
247
+ set pushButton(value: boolean);
248
+ /**
249
+ * If true, text field allows editing (Ff bit 19)
250
+ */
251
+ get edit(): boolean;
252
+ set edit(value: boolean);
253
+ /**
254
+ * If true, choice options should be sorted alphabetically (Ff bit 20)
255
+ */
256
+ get sort(): boolean;
257
+ set sort(value: boolean);
258
+ /**
259
+ * If true, allows multiple selections in choice field (Ff bit 22)
260
+ */
261
+ get multiSelect(): boolean;
262
+ set multiSelect(value: boolean);
263
+ /**
264
+ * If true, do not spell check this field (Ff bit 23)
265
+ */
266
+ get doNotSpellCheck(): boolean;
267
+ set doNotSpellCheck(value: boolean);
268
+ /**
269
+ * If true, do not scroll text field (Ff bit 24)
270
+ */
271
+ get doNotScroll(): boolean;
272
+ set doNotScroll(value: boolean);
273
+ /**
274
+ * If true, commit field value immediately on selection change (Ff bit 27)
275
+ */
276
+ get commitOnSelChange(): boolean;
277
+ set commitOnSelChange(value: boolean);
278
+ get kids(): PdfObjectReference[];
279
+ set kids(kids: PdfObjectReference[]);
280
+ get appearanceStreamDict(): PdfAppearanceStreamDictionary | null;
281
+ set appearanceStreamDict(dict: PdfAppearanceStreamDictionary | null);
282
+ /**
283
+ * Generates an appearance stream for a text field using iText's approach.
284
+ *
285
+ * This generates an appearance with text using the same positioning formula as iText:
286
+ * - textY = (height - fontSize) / 2 + fontSize * 0.2
287
+ * - Wrapped in marked content blocks (/Tx BMC ... EMC)
288
+ * - Field remains editable unless makeReadOnly is set
289
+ *
290
+ * For editable fields (default, no options):
291
+ * - Text visible immediately
292
+ * - Field remains fully editable
293
+ * - No save dialog (needAppearances = false)
294
+ * - Text positioning matches iText
295
+ *
296
+ * For read-only fields (makeReadOnly: true):
297
+ * - Same appearance generation
298
+ * - Field is set as read-only
299
+ *
300
+ * @param options.makeReadOnly - If true, sets field as read-only
301
+ * @returns true if appearance was generated successfully
302
+ */
303
+ generateAppearance(options?: {
304
+ makeReadOnly?: boolean;
305
+ textYOffset?: number;
306
+ }): boolean;
307
+ /**
308
+ * Generates appearance for text fields
309
+ * @internal
310
+ */
311
+ private generateTextAppearance;
312
+ /**
313
+ * Generates appearance for button fields (checkboxes, radio buttons)
314
+ * @internal
315
+ */
316
+ private generateButtonAppearance;
317
+ /**
318
+ * Generates appearance for choice fields (dropdowns, list boxes)
319
+ * @internal
320
+ */
321
+ private generateChoiceAppearance;
322
+ /**
323
+ * Gets the stored appearance stream if one has been generated.
324
+ * For button fields, returns the appropriate stream based on the current state.
325
+ * @internal
326
+ */
327
+ getAppearanceStream(): PdfStream | undefined;
328
+ /**
329
+ * Gets all appearance streams for writing to PDF.
330
+ * For button fields, returns both Off and Yes states.
331
+ * For other fields, returns just the primary appearance.
332
+ * @internal
333
+ */
334
+ getAppearanceStreamsForWriting(): {
335
+ primary: PdfStream;
336
+ secondary?: PdfStream;
337
+ } | undefined;
338
+ /**
339
+ * Sets the appearance dictionary reference for this field.
340
+ * @internal - This is called automatically by PdfAcroForm.write()
341
+ */
342
+ setAppearanceReference(appearanceStreamRef: PdfObjectReference, appearanceStreamYesRef?: PdfObjectReference): void;
123
343
  }
124
- export declare class PdfAcroForm<T extends Record<string, string> = Record<string, string>> extends PdfDictionary<{
344
+ export declare class PdfAcroForm<T extends Record<string, string> = Record<string, string>> extends PdfIndirectObject<PdfDictionary<{
125
345
  Fields: PdfArray<PdfObjectReference>;
126
346
  NeedAppearances?: PdfBoolean;
127
347
  SigFlags?: PdfNumber;
128
348
  CO?: PdfArray<PdfObjectReference>;
129
- DR?: PdfDictionary;
349
+ DR?: PdfDefaultResourcesDictionary;
130
350
  DA?: PdfString;
131
351
  Q?: PdfNumber;
132
- }> {
352
+ }>> {
133
353
  fields: PdfAcroFormField[];
134
- readonly container?: PdfIndirectObject;
135
- constructor(options: {
136
- dict: PdfDictionary;
354
+ readonly fontEncodingMaps: Map<string, Map<number, string>>;
355
+ private document?;
356
+ constructor(options?: {
357
+ other?: PdfIndirectObject;
137
358
  fields?: PdfAcroFormField[];
138
- container?: PdfIndirectObject;
359
+ document?: PdfDocument;
139
360
  });
361
+ /**
362
+ * Convenience method to get a value from the form dictionary
363
+ */
364
+ get(key: string): any;
365
+ /**
366
+ * Convenience method to set a value in the form dictionary
367
+ */
368
+ set(key: string, value: any): void;
369
+ /**
370
+ * Convenience method to delete a key from the form dictionary
371
+ */
372
+ delete(key: string): void;
373
+ /**
374
+ * Convenience method to check if form dictionary is modified
375
+ */
376
+ isModified(): boolean;
140
377
  /**
141
378
  * Gets the NeedAppearances flag
142
379
  */
@@ -170,6 +407,14 @@ export declare class PdfAcroForm<T extends Record<string, string> = Record<strin
170
407
  * Sets the default quadding (alignment) for the form
171
408
  */
172
409
  set defaultQuadding(q: number);
410
+ /**
411
+ * Gets the default resources dictionary for the form
412
+ */
413
+ get defaultResources(): PdfDefaultResourcesDictionary | null;
414
+ /**
415
+ * Sets the default resources dictionary for the form
416
+ */
417
+ set defaultResources(resources: PdfDefaultResourcesDictionary | null);
173
418
  /**
174
419
  * Sets multiple field values by field name.
175
420
  * @param values Object with field names as keys and values to set
@@ -177,7 +422,18 @@ export declare class PdfAcroForm<T extends Record<string, string> = Record<strin
177
422
  setValues(values: Partial<T>): void;
178
423
  importData(fields: T): void;
179
424
  exportData(): Partial<T>;
425
+ /**
426
+ * Gets the encoding map for a specific font in the form's resources.
427
+ * Returns null if no custom encoding is found.
428
+ * Results are cached for performance.
429
+ */
430
+ getFontEncodingMap(fontName: string): Promise<Map<number, string> | null>;
180
431
  static fromDocument(document: PdfDocument): Promise<PdfAcroForm | null>;
432
+ /**
433
+ * Pre-caches encoding maps for all fonts used in the form fields.
434
+ * This makes subsequent field value access faster and synchronous.
435
+ */
436
+ private cacheAllFontEncodings;
181
437
  /**
182
438
  * Gets or creates the Annots array for a page.
183
439
  * Returns the array and metadata about whether it's an indirect object.