pdf-lite 1.3.0 → 1.3.2

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 (38) hide show
  1. package/README.md +1 -1
  2. package/dist/acroform/acroform.d.ts +258 -23
  3. package/dist/acroform/acroform.js +1036 -174
  4. package/dist/core/decoder.d.ts +1 -1
  5. package/dist/core/decoder.js +1 -1
  6. package/dist/core/index.d.ts +2 -2
  7. package/dist/core/index.js +2 -2
  8. package/dist/core/objects/pdf-array.d.ts +1 -0
  9. package/dist/core/objects/pdf-array.js +4 -0
  10. package/dist/core/objects/pdf-dictionary.d.ts +1 -0
  11. package/dist/core/objects/pdf-dictionary.js +12 -0
  12. package/dist/core/objects/pdf-hexadecimal.d.ts +3 -1
  13. package/dist/core/objects/pdf-hexadecimal.js +14 -2
  14. package/dist/core/objects/pdf-indirect-object.d.ts +5 -3
  15. package/dist/core/objects/pdf-indirect-object.js +23 -5
  16. package/dist/core/objects/pdf-number.js +3 -0
  17. package/dist/core/objects/pdf-object.d.ts +6 -0
  18. package/dist/core/objects/pdf-object.js +10 -0
  19. package/dist/core/objects/pdf-stream.js +3 -0
  20. package/dist/core/objects/pdf-string.js +3 -0
  21. package/dist/core/ref.d.ts +5 -0
  22. package/dist/core/ref.js +14 -0
  23. package/dist/core/serializer.d.ts +1 -1
  24. package/dist/core/serializer.js +1 -1
  25. package/dist/core/tokeniser.d.ts +2 -2
  26. package/dist/core/tokeniser.js +2 -2
  27. package/dist/fonts/font-manager.js +6 -8
  28. package/dist/pdf/pdf-document.d.ts +6 -5
  29. package/dist/pdf/pdf-document.js +29 -21
  30. package/dist/pdf/pdf-revision.d.ts +33 -4
  31. package/dist/pdf/pdf-revision.js +100 -26
  32. package/dist/pdf/pdf-xref-lookup.js +3 -2
  33. package/dist/xfa/manager.js +2 -4
  34. package/package.json +1 -1
  35. /package/dist/core/{incremental-parser.d.ts → parser/incremental-parser.d.ts} +0 -0
  36. /package/dist/core/{incremental-parser.js → parser/incremental-parser.js} +0 -0
  37. /package/dist/core/{parser.d.ts → parser/parser.d.ts} +0 -0
  38. /package/dist/core/{parser.js → parser/parser.js} +0 -0
package/README.md CHANGED
@@ -13,10 +13,10 @@ PRs and issues are welcome!
13
13
 
14
14
  ## Features
15
15
 
16
- - **Zero dependencies**: No external libraries are required, making it lightweight and easy to integrate.
17
16
  - **Type-safe**: Built with TypeScript, ensuring type safety and reducing runtime errors.
18
17
  - **Browser and Node.js support**: Works seamlessly in both environments, allowing for versatile usage.
19
18
  - **Low-level API**: Provides a low-level API for advanced users who want to manipulate PDF files directly, as well as a higher-level API for easier usage.
19
+ - **Minimal dependencies**: A small number external libraries are required, making it lightweight and easy to integrate.
20
20
 
21
21
  ## Installation
22
22
 
@@ -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,14 +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;
40
- form?: PdfAcroForm;
66
+ defaultGenerateAppearance: boolean;
67
+ private _appearanceStream?;
68
+ private _appearanceStreamYes?;
69
+ private form?;
41
70
  constructor(options?: {
42
- container?: PdfIndirectObject;
71
+ other?: PdfIndirectObject;
43
72
  form?: PdfAcroForm;
44
73
  });
74
+ get encodingMap(): Map<number, string> | undefined;
75
+ /**
76
+ * Convenience method to check if field dictionary is modified
77
+ */
78
+ isModified(): boolean;
45
79
  /**
46
80
  * Gets the field type
47
81
  */
@@ -70,12 +104,7 @@ export declare class PdfAcroFormField extends PdfDictionary<{
70
104
  */
71
105
  set defaultValue(val: string);
72
106
  get value(): string;
73
- /**
74
- * Gets the cached encoding map for this field's font, if available.
75
- * Returns undefined if no encoding has been cached yet.
76
- */
77
- private getCachedEncodingMap;
78
- set value(val: string);
107
+ set value(val: string | PdfString);
79
108
  get checked(): boolean;
80
109
  set checked(isChecked: boolean);
81
110
  get fontSize(): number | null;
@@ -96,11 +125,19 @@ export declare class PdfAcroFormField extends PdfDictionary<{
96
125
  */
97
126
  set flags(flags: number);
98
127
  /**
99
- * 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)
100
137
  */
101
138
  get readOnly(): boolean;
102
139
  /**
103
- * Sets the field as read-only or editable
140
+ * Sets the field as read-only or editable (Ff bit 1)
104
141
  */
105
142
  set readOnly(isReadOnly: boolean);
106
143
  /**
@@ -127,26 +164,216 @@ export declare class PdfAcroFormField extends PdfDictionary<{
127
164
  * Sets the field as a password field (for text fields)
128
165
  */
129
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;
130
343
  }
131
- 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<{
132
345
  Fields: PdfArray<PdfObjectReference>;
133
346
  NeedAppearances?: PdfBoolean;
134
347
  SigFlags?: PdfNumber;
135
348
  CO?: PdfArray<PdfObjectReference>;
136
- DR?: PdfDictionary;
349
+ DR?: PdfDefaultResourcesDictionary;
137
350
  DA?: PdfString;
138
351
  Q?: PdfNumber;
139
- }> {
352
+ }>> {
140
353
  fields: PdfAcroFormField[];
141
- readonly container?: PdfIndirectObject;
142
- readonly fontEncodingMaps: Map<string, Map<number, string> | null>;
354
+ readonly fontEncodingMaps: Map<string, Map<number, string>>;
143
355
  private document?;
144
- constructor(options: {
145
- dict: PdfDictionary;
356
+ constructor(options?: {
357
+ other?: PdfIndirectObject;
146
358
  fields?: PdfAcroFormField[];
147
- container?: PdfIndirectObject;
148
359
  document?: PdfDocument;
149
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;
150
377
  /**
151
378
  * Gets the NeedAppearances flag
152
379
  */
@@ -180,6 +407,14 @@ export declare class PdfAcroForm<T extends Record<string, string> = Record<strin
180
407
  * Sets the default quadding (alignment) for the form
181
408
  */
182
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);
183
418
  /**
184
419
  * Sets multiple field values by field name.
185
420
  * @param values Object with field names as keys and values to set