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,33 +1,43 @@
1
+ import { PdfDictionary } from '../../core/objects/pdf-dictionary.js';
2
+ import { PdfArray } from '../../core/objects/pdf-array.js';
1
3
  import { PdfString } from '../../core/objects/pdf-string.js';
2
4
  import { PdfObjectReference } from '../../core/objects/pdf-object-reference.js';
3
5
  import { PdfIndirectObject } from '../../core/objects/pdf-indirect-object.js';
4
6
  import { PdfFont } from '../../fonts/pdf-font.js';
5
7
  import { PdfStream } from '../../core/objects/pdf-stream.js';
6
- import { PdfWidgetAnnotation } from '../../annotations/PdfWidgetAnnotation.js';
7
- import type { PdfAppearanceStream } from '../appearance/PdfAppearanceStream.js';
8
- import type { FormContext, PdfFieldType } from './types.js';
8
+ import { PdfWidgetAnnotation } from '../../annotations/pdf-widget-annotation.js';
9
+ import type { PdfFieldType } from './types.js';
10
+ import { PdfFormFieldFlags } from './pdf-form-field-flags.js';
11
+ import { PdfDefaultResourcesDictionary } from '../../annotations/pdf-default-resources.js';
12
+ import type { PdfAcroForm } from '../pdf-acro-form.js';
9
13
  /**
10
14
  * Abstract base form field class. Extends PdfWidgetAnnotation with form-specific properties:
11
15
  * FT, V, DA, Ff, T (name), field hierarchy (parent/children/siblings).
12
16
  * Subclasses must implement generateAppearance().
13
17
  */
14
18
  export declare abstract class PdfFormField extends PdfWidgetAnnotation {
15
- private _parent?;
16
19
  defaultGenerateAppearance: boolean;
17
- protected _appearanceStream?: PdfAppearanceStream;
18
- protected _appearanceStreamYes?: PdfAppearanceStream;
19
- form?: FormContext<PdfFormField>;
20
- constructor(options?: {
21
- other?: PdfIndirectObject;
22
- form?: FormContext<PdfFormField>;
23
- parent?: PdfFormField;
20
+ /** @internal */
21
+ _form?: PdfAcroForm;
22
+ constructor(other?: PdfIndirectObject | {
23
+ form?: PdfAcroForm;
24
24
  });
25
+ set form(f: PdfAcroForm);
26
+ static create(other?: PdfIndirectObject): PdfFormField;
25
27
  get parent(): PdfFormField | undefined;
26
- set parent(field: PdfFormField | undefined);
28
+ set parent(field: PdfFormField | PdfIndirectObject | undefined);
27
29
  get children(): PdfFormField[];
28
30
  set children(fields: PdfFormField[]);
29
31
  get siblings(): PdfFormField[];
30
- get encodingMap(): Map<number, string> | undefined;
32
+ get font(): PdfFont | null;
33
+ get defaultResources(): PdfDefaultResourcesDictionary | null;
34
+ set defaultResources(resources: PdfDefaultResourcesDictionary | null);
35
+ /**
36
+ * Builds a Resources dictionary containing the font entry for `fontName`,
37
+ * resolved from DR (handling indirect references) or from a loaded font.
38
+ * Returns undefined if neither source provides the font.
39
+ */
40
+ buildFontResources(fontName: string): PdfDictionary | undefined;
31
41
  get fieldType(): PdfFieldType | null;
32
42
  set fieldType(type: PdfFieldType | null);
33
43
  get name(): string;
@@ -36,17 +46,19 @@ export declare abstract class PdfFormField extends PdfWidgetAnnotation {
36
46
  set defaultValue(val: string);
37
47
  get value(): string;
38
48
  set value(val: string | PdfString);
39
- get checked(): boolean;
40
- set checked(isChecked: boolean);
49
+ /**
50
+ * Writes the value to the dictionary. Returns true if appearance generation
51
+ * should proceed, false to skip it (e.g. when value was cleared).
52
+ * Override in subclasses to change the stored representation.
53
+ */
54
+ protected _storeValue(val: string | PdfString, fieldParent: PdfFormField | undefined): boolean;
41
55
  get fontSize(): number | null;
42
56
  set fontSize(size: number);
43
57
  get fontName(): string | null;
44
58
  set fontName(fontName: string);
45
59
  set font(font: PdfFont | null);
46
- get flags(): number;
47
- set flags(flags: number);
48
- private getFlag;
49
- private setFlag;
60
+ get flags(): PdfFormFieldFlags;
61
+ set flags(v: PdfFormFieldFlags);
50
62
  get readOnly(): boolean;
51
63
  set readOnly(v: boolean);
52
64
  get required(): boolean;
@@ -82,38 +94,25 @@ export declare abstract class PdfFormField extends PdfWidgetAnnotation {
82
94
  set commitOnSelChange(v: boolean);
83
95
  get quadding(): number;
84
96
  set quadding(q: number);
85
- get options(): string[];
86
- set options(options: string[]);
87
97
  get defaultAppearance(): string | null;
88
98
  set defaultAppearance(da: string);
89
99
  get maxLen(): number | null;
90
100
  set maxLen(maxLen: number | null);
91
- get kids(): PdfObjectReference[];
101
+ get kids(): PdfArray<PdfObjectReference> | undefined;
92
102
  set kids(kids: PdfObjectReference[]);
93
103
  abstract generateAppearance(options?: {
94
104
  makeReadOnly?: boolean;
95
105
  textYOffset?: number;
96
106
  }): boolean;
97
- getAppearanceStream(): PdfStream | undefined;
98
- getAppearanceStreamsForWriting(): {
99
- primary: PdfAppearanceStream;
100
- secondary?: PdfAppearanceStream;
101
- } | undefined;
102
- setAppearanceReference(appearanceStreamRef: PdfObjectReference, appearanceStreamYesRef?: PdfObjectReference): void;
107
+ setAppearanceStream(stream: PdfIndirectObject | {
108
+ [key: string]: PdfIndirectObject;
109
+ }): void;
110
+ getAppearanceStream(setting?: string): PdfIndirectObject<PdfStream> | null;
103
111
  private static _fallbackCtor?;
104
112
  private static _registry;
105
- static registerFieldType(ft: string, ctor: new (options?: {
106
- other?: PdfIndirectObject;
107
- form?: FormContext<PdfFormField>;
108
- parent?: PdfFormField;
109
- }) => PdfFormField, options?: {
113
+ static registerFieldType(ft: 'Sig' | 'Btn' | 'Tx' | 'Ch', ctor: new (other?: PdfIndirectObject) => PdfFormField, options?: {
110
114
  fallback?: boolean;
111
115
  }): void;
112
- static create(options: {
113
- other: PdfIndirectObject;
114
- form: FormContext<PdfFormField>;
115
- parent?: PdfFormField;
116
- }): PdfFormField;
117
116
  }
118
117
  /** Backward compatible alias */
119
118
  export { PdfFormField as PdfAcroFormField };
@@ -0,0 +1,519 @@
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 { PdfObjectReference } from '../../core/objects/pdf-object-reference.js';
5
+ import { PdfIndirectObject } from '../../core/objects/pdf-indirect-object.js';
6
+ import { PdfName } from '../../core/objects/pdf-name.js';
7
+ import { PdfNumber } from '../../core/objects/pdf-number.js';
8
+ import { PdfFont } from '../../fonts/pdf-font.js';
9
+ import { PdfStream } from '../../core/objects/pdf-stream.js';
10
+ import { decodeWithFontEncoding } from '../../utils/decodeWithFontEncoding.js';
11
+ import { PdfWidgetAnnotation } from '../../annotations/pdf-widget-annotation.js';
12
+ import { PdfDefaultAppearance } from './pdf-default-appearance.js';
13
+ import { PdfFieldType as PdfFieldTypeConst } from './types.js';
14
+ import { PdfFormFieldFlags } from './pdf-form-field-flags.js';
15
+ /**
16
+ * Abstract base form field class. Extends PdfWidgetAnnotation with form-specific properties:
17
+ * FT, V, DA, Ff, T (name), field hierarchy (parent/children/siblings).
18
+ * Subclasses must implement generateAppearance().
19
+ */
20
+ export class PdfFormField extends PdfWidgetAnnotation {
21
+ defaultGenerateAppearance = true;
22
+ /** @internal */
23
+ _form;
24
+ constructor(other) {
25
+ super();
26
+ if (other && !(other instanceof PdfIndirectObject)) {
27
+ this._form = other.form;
28
+ }
29
+ }
30
+ set form(f) {
31
+ this._form = f;
32
+ }
33
+ static create(other) {
34
+ if (!(other?.content instanceof PdfDictionary))
35
+ throw new Error('Invalid form field object');
36
+ const ft = other?.content.get('FT')?.as(PdfName)?.value;
37
+ const cls = ft ? PdfFormField._registry.get(ft) : undefined;
38
+ if (!cls) {
39
+ if (PdfFormField._fallbackCtor) {
40
+ return other.becomes(PdfFormField._fallbackCtor);
41
+ }
42
+ throw new Error(`Unsupported form field type: ${ft ?? 'unknown'}`);
43
+ }
44
+ return other.becomes(cls);
45
+ }
46
+ get parent() {
47
+ const resolved = this.content.get('Parent')?.resolve();
48
+ if (!resolved || !(resolved.content instanceof PdfDictionary))
49
+ return undefined;
50
+ return PdfFormField.create(resolved);
51
+ }
52
+ set parent(field) {
53
+ if (!field) {
54
+ this.content.delete('Parent');
55
+ return;
56
+ }
57
+ this.content.set('Parent', field.reference);
58
+ if (field instanceof PdfFormField) {
59
+ field.children = [...field.children, this];
60
+ }
61
+ }
62
+ get children() {
63
+ const kids = this.content.get('Kids')?.items ?? [];
64
+ const result = [];
65
+ for (const ref of kids) {
66
+ const resolved = ref.resolve();
67
+ if (!resolved || !(resolved.content instanceof PdfDictionary))
68
+ continue;
69
+ result.push(PdfFormField.create(resolved));
70
+ }
71
+ return result;
72
+ }
73
+ set children(fields) {
74
+ if (fields.length === 0) {
75
+ this.content.delete('Kids');
76
+ return;
77
+ }
78
+ this.content.set('Kids', PdfArray.refs(fields));
79
+ for (const child of fields) {
80
+ child.content.set('Parent', this.reference);
81
+ }
82
+ }
83
+ get siblings() {
84
+ return this.parent?.children ?? [];
85
+ }
86
+ get font() {
87
+ const fontName = this.fontName;
88
+ if (!fontName)
89
+ return null;
90
+ // Try resolving from default resources (DR) dict
91
+ const dr = this.defaultResources;
92
+ const fontDict = dr?.get('Font');
93
+ if (fontDict instanceof PdfDictionary) {
94
+ const fontEntry = fontDict.get(fontName);
95
+ if (fontEntry instanceof PdfObjectReference) {
96
+ const resolved = fontEntry.resolve();
97
+ if (resolved?.content instanceof PdfDictionary) {
98
+ const font = resolved.becomes(PdfFont);
99
+ font.resourceName = fontName;
100
+ return font;
101
+ }
102
+ }
103
+ }
104
+ // Fallback: standard font lookup (Helv, Helvetica, ZaDb, etc.)
105
+ return PdfFont.getStandardFont(fontName) ?? null;
106
+ }
107
+ get defaultResources() {
108
+ const dr = this.content.get('DR');
109
+ const drDict = dr instanceof PdfObjectReference ? dr.resolve()?.content : dr;
110
+ return ((drDict instanceof PdfDictionary ? drDict : null) ??
111
+ this.parent?.defaultResources ??
112
+ this._form?.defaultResources ??
113
+ null);
114
+ }
115
+ set defaultResources(resources) {
116
+ if (resources === null) {
117
+ this.content.delete('DR');
118
+ }
119
+ else {
120
+ this.content.set('DR', resources);
121
+ }
122
+ }
123
+ /**
124
+ * Builds a Resources dictionary containing the font entry for `fontName`,
125
+ * resolved from DR (handling indirect references) or from a loaded font.
126
+ * Returns undefined if neither source provides the font.
127
+ */
128
+ buildFontResources(fontName) {
129
+ const dr = this.defaultResources;
130
+ const fontRaw = dr?.get('Font');
131
+ let drFontDict;
132
+ if (fontRaw instanceof PdfObjectReference) {
133
+ const resolved = fontRaw.resolve()?.content;
134
+ if (resolved instanceof PdfDictionary)
135
+ drFontDict = resolved;
136
+ }
137
+ else if (fontRaw instanceof PdfDictionary) {
138
+ drFontDict = fontRaw;
139
+ }
140
+ if (drFontDict && drFontDict.get(fontName)) {
141
+ const resFontDict = new PdfDictionary();
142
+ resFontDict.set(fontName, drFontDict.get(fontName));
143
+ const resources = new PdfDictionary();
144
+ resources.set('Font', resFontDict);
145
+ return resources;
146
+ }
147
+ const font = this.font;
148
+ if (font && !PdfFont.getStandardFont(fontName)) {
149
+ const fontDict = new PdfDictionary();
150
+ fontDict.set(fontName, font.reference);
151
+ const resources = new PdfDictionary();
152
+ resources.set('Font', fontDict);
153
+ return resources;
154
+ }
155
+ return undefined;
156
+ }
157
+ get fieldType() {
158
+ const ft = this.content.get('FT')?.as(PdfName)?.value ??
159
+ this.parent?.content.get('FT')?.as(PdfName)?.value;
160
+ switch (ft) {
161
+ case 'Tx':
162
+ return 'Text';
163
+ case 'Btn':
164
+ return 'Button';
165
+ case 'Ch':
166
+ return 'Choice';
167
+ case 'Sig':
168
+ return 'Signature';
169
+ default:
170
+ return null;
171
+ }
172
+ }
173
+ set fieldType(type) {
174
+ if (type === null) {
175
+ this.content.delete('FT');
176
+ }
177
+ else {
178
+ this.content.set('FT', new PdfName(PdfFieldTypeConst[type]));
179
+ }
180
+ }
181
+ get name() {
182
+ const parentName = this.parent?.name ?? '';
183
+ const ownName = this.content.get('T')?.as(PdfString)?.value ?? '';
184
+ if (parentName && ownName) {
185
+ return `${parentName}.${ownName}`;
186
+ }
187
+ return parentName || ownName;
188
+ }
189
+ set name(name) {
190
+ this.content.set('T', new PdfString(name));
191
+ }
192
+ get defaultValue() {
193
+ const dv = this.content.get('DV') ?? this.parent?.content.get('DV');
194
+ if (dv instanceof PdfString)
195
+ return dv.value;
196
+ if (dv instanceof PdfName)
197
+ return dv.value;
198
+ return '';
199
+ }
200
+ set defaultValue(val) {
201
+ this.content.set('DV', new PdfString(val));
202
+ }
203
+ get value() {
204
+ const v = this.content.get('V') ?? this.parent?.content.get('V');
205
+ if (v instanceof PdfString) {
206
+ if (v.isUTF16BE)
207
+ return v.value;
208
+ const encodingMap = this.font?.encodingMap ??
209
+ (this.fontName
210
+ ? this._form?.fontEncodingMaps?.get(this.fontName)
211
+ : undefined);
212
+ if (encodingMap) {
213
+ return decodeWithFontEncoding(v.raw, encodingMap);
214
+ }
215
+ return v.value;
216
+ }
217
+ else if (v instanceof PdfName) {
218
+ return v.value;
219
+ }
220
+ return '';
221
+ }
222
+ set value(val) {
223
+ if (this.value === val)
224
+ return;
225
+ const fieldParent = this.parent?.content.get('FT')
226
+ ? this.parent
227
+ : undefined;
228
+ const generateAppearance = this._storeValue(val, fieldParent);
229
+ if (generateAppearance && this.defaultGenerateAppearance) {
230
+ this.generateAppearance();
231
+ for (const sibling of this.siblings) {
232
+ if (sibling !== this &&
233
+ sibling.rect &&
234
+ sibling.defaultGenerateAppearance) {
235
+ sibling.generateAppearance();
236
+ }
237
+ }
238
+ // Separated field/widget structure: field has no Rect but its Kids
239
+ // are widget annotations that do. Generate appearances for them.
240
+ if (!this.rect) {
241
+ for (const child of this.children) {
242
+ if (child.rect && child.defaultGenerateAppearance) {
243
+ if (this._form)
244
+ child.form = this._form;
245
+ child.generateAppearance();
246
+ }
247
+ }
248
+ }
249
+ }
250
+ if (this._form) {
251
+ this._form.xfa?.datasets?.updateField(this.name, this.value);
252
+ }
253
+ }
254
+ /**
255
+ * Writes the value to the dictionary. Returns true if appearance generation
256
+ * should proceed, false to skip it (e.g. when value was cleared).
257
+ * Override in subclasses to change the stored representation.
258
+ */
259
+ _storeValue(val, fieldParent) {
260
+ const pdfVal = val instanceof PdfString ? val : new PdfString(val);
261
+ this.content.set('V', pdfVal);
262
+ fieldParent?.content.set('V', pdfVal);
263
+ return true;
264
+ }
265
+ get fontSize() {
266
+ const da = this.defaultAppearance || '';
267
+ const parsed = PdfDefaultAppearance.parse(da);
268
+ return parsed?.fontSize ?? null;
269
+ }
270
+ set fontSize(size) {
271
+ const da = this.defaultAppearance || '';
272
+ if (!da) {
273
+ this.content.set('DA', new PdfDefaultAppearance('F1', size, '0 g'));
274
+ return;
275
+ }
276
+ const parsed = PdfDefaultAppearance.parse(da);
277
+ if (parsed) {
278
+ parsed.fontSize = size;
279
+ this.content.set('DA', parsed);
280
+ }
281
+ }
282
+ get fontName() {
283
+ const da = this.defaultAppearance || '';
284
+ const parsed = PdfDefaultAppearance.parse(da);
285
+ return parsed?.fontName ?? null;
286
+ }
287
+ set fontName(fontName) {
288
+ const da = this.defaultAppearance || '';
289
+ if (!da) {
290
+ this.content.set('DA', new PdfDefaultAppearance(fontName, 12, '0 g'));
291
+ return;
292
+ }
293
+ const parsed = PdfDefaultAppearance.parse(da);
294
+ if (parsed) {
295
+ parsed.fontName = fontName;
296
+ this.content.set('DA', parsed);
297
+ }
298
+ }
299
+ set font(font) {
300
+ if (font === null) {
301
+ this.content.set('DA', new PdfString(''));
302
+ return;
303
+ }
304
+ const resourceName = font.resourceName;
305
+ const currentSize = this.fontSize ?? 12;
306
+ const da = this.defaultAppearance || '';
307
+ if (!da) {
308
+ this.content.set('DA', new PdfDefaultAppearance(resourceName, currentSize, '0 g'));
309
+ return;
310
+ }
311
+ const parsed = PdfDefaultAppearance.parse(da);
312
+ if (parsed) {
313
+ parsed.fontName = resourceName;
314
+ this.content.set('DA', parsed);
315
+ }
316
+ }
317
+ get flags() {
318
+ const flags = new PdfFormFieldFlags(this.content.get('Ff') ?? this.parent?.content.get('Ff') ?? 0);
319
+ flags.onChange(() => {
320
+ this.flags = flags;
321
+ });
322
+ return flags;
323
+ }
324
+ set flags(v) {
325
+ this.content.set('Ff', v);
326
+ }
327
+ get readOnly() {
328
+ return this.flags.readOnly;
329
+ }
330
+ set readOnly(v) {
331
+ this.flags.readOnly = v;
332
+ }
333
+ get required() {
334
+ return this.flags.required;
335
+ }
336
+ set required(v) {
337
+ this.flags.required = v;
338
+ }
339
+ get multiline() {
340
+ return this.flags.multiline;
341
+ }
342
+ set multiline(v) {
343
+ this.flags.multiline = v;
344
+ }
345
+ get password() {
346
+ return this.flags.password;
347
+ }
348
+ set password(v) {
349
+ this.flags.password = v;
350
+ }
351
+ get comb() {
352
+ return this.flags.comb;
353
+ }
354
+ get combField() {
355
+ return this.flags.comb;
356
+ }
357
+ set combField(v) {
358
+ this.flags.comb = v;
359
+ }
360
+ get combo() {
361
+ return this.flags.combo;
362
+ }
363
+ set combo(v) {
364
+ this.flags.combo = v;
365
+ }
366
+ get radio() {
367
+ return this.flags.radio;
368
+ }
369
+ set radio(v) {
370
+ this.flags.radio = v;
371
+ }
372
+ get noToggleToOff() {
373
+ return this.flags.noToggleToOff;
374
+ }
375
+ set noToggleToOff(v) {
376
+ this.flags.noToggleToOff = v;
377
+ }
378
+ get noExport() {
379
+ return this.flags.noExport;
380
+ }
381
+ set noExport(v) {
382
+ this.flags.noExport = v;
383
+ }
384
+ get pushButton() {
385
+ return this.flags.pushButton;
386
+ }
387
+ set pushButton(v) {
388
+ this.flags.pushButton = v;
389
+ }
390
+ get edit() {
391
+ return this.flags.edit;
392
+ }
393
+ set edit(v) {
394
+ this.flags.edit = v;
395
+ }
396
+ get sort() {
397
+ return this.flags.sort;
398
+ }
399
+ set sort(v) {
400
+ this.flags.sort = v;
401
+ }
402
+ get multiSelect() {
403
+ return this.flags.multiSelect;
404
+ }
405
+ set multiSelect(v) {
406
+ this.flags.multiSelect = v;
407
+ }
408
+ get doNotSpellCheck() {
409
+ return this.flags.doNotSpellCheck;
410
+ }
411
+ set doNotSpellCheck(v) {
412
+ this.flags.doNotSpellCheck = v;
413
+ }
414
+ get doNotScroll() {
415
+ return this.flags.doNotScroll;
416
+ }
417
+ set doNotScroll(v) {
418
+ this.flags.doNotScroll = v;
419
+ }
420
+ get commitOnSelChange() {
421
+ return this.flags.commitOnSelChange;
422
+ }
423
+ set commitOnSelChange(v) {
424
+ this.flags.commitOnSelChange = v;
425
+ }
426
+ get quadding() {
427
+ return (this.content.get('Q')?.as(PdfNumber)?.value ??
428
+ this.parent?.content.get('Q')?.as(PdfNumber)?.value ??
429
+ 0);
430
+ }
431
+ set quadding(q) {
432
+ this.content.set('Q', new PdfNumber(q));
433
+ }
434
+ get defaultAppearance() {
435
+ return (this.content.get('DA')?.as(PdfString)?.value ??
436
+ this.parent?.defaultAppearance ??
437
+ this._form?.defaultAppearance ??
438
+ null);
439
+ }
440
+ set defaultAppearance(da) {
441
+ this.content.set('DA', new PdfString(da));
442
+ }
443
+ get maxLen() {
444
+ return this.content.get('MaxLen')?.as(PdfNumber)?.value ?? null;
445
+ }
446
+ set maxLen(maxLen) {
447
+ if (maxLen === null) {
448
+ this.content.delete('MaxLen');
449
+ }
450
+ else {
451
+ this.content.set('MaxLen', new PdfNumber(maxLen));
452
+ }
453
+ }
454
+ get kids() {
455
+ const kidsArray = this.content
456
+ .get('Kids')
457
+ ?.as((PdfArray));
458
+ if (!kidsArray)
459
+ return undefined;
460
+ return kidsArray;
461
+ }
462
+ set kids(kids) {
463
+ if (kids.length === 0) {
464
+ this.content.delete('Kids');
465
+ return;
466
+ }
467
+ const kidsArray = new PdfArray(kids);
468
+ this.content.set('Kids', kidsArray);
469
+ }
470
+ setAppearanceStream(stream) {
471
+ this.appearanceStreamDict ||= new PdfDictionary();
472
+ if (stream instanceof PdfIndirectObject) {
473
+ this.appearanceStreamDict.set('N', stream.reference);
474
+ }
475
+ else {
476
+ const dict = new PdfDictionary();
477
+ for (const key in stream) {
478
+ dict.set(key, stream[key].reference);
479
+ }
480
+ this.appearanceStreamDict.set('N', dict);
481
+ }
482
+ }
483
+ getAppearanceStream(setting) {
484
+ const n = this.appearanceStreamDict?.get('N');
485
+ if (!n)
486
+ return null;
487
+ if (n instanceof PdfObjectReference) {
488
+ const resolved = n.resolve();
489
+ if (resolved?.content instanceof PdfStream) {
490
+ return resolved;
491
+ }
492
+ }
493
+ else if (n instanceof PdfDictionary) {
494
+ const key = setting ??
495
+ this.content.get('AS')?.as(PdfName)?.value ??
496
+ undefined;
497
+ if (key) {
498
+ const entry = n.get(key);
499
+ if (entry instanceof PdfObjectReference) {
500
+ const resolved = entry.resolve();
501
+ if (resolved?.content instanceof PdfStream) {
502
+ return resolved;
503
+ }
504
+ }
505
+ }
506
+ }
507
+ return null;
508
+ }
509
+ static _fallbackCtor;
510
+ static _registry = new Map();
511
+ static registerFieldType(ft, ctor, options) {
512
+ PdfFormField._registry.set(ft, ctor);
513
+ if (options?.fallback) {
514
+ PdfFormField._fallbackCtor = ctor;
515
+ }
516
+ }
517
+ }
518
+ /** Backward compatible alias */
519
+ export { PdfFormField as PdfAcroFormField };
@@ -1,4 +1,4 @@
1
- import { PdfFormField } from './PdfFormField.js';
1
+ import { PdfFormField } from './pdf-form-field.js';
2
2
  /**
3
3
  * Signature form field subtype.
4
4
  */
@@ -1,4 +1,4 @@
1
- import { PdfFormField } from './PdfFormField.js';
1
+ import { PdfFormField } from './pdf-form-field.js';
2
2
  /**
3
3
  * Signature form field subtype.
4
4
  */
@@ -1,4 +1,4 @@
1
- import { PdfFormField } from './PdfFormField.js';
1
+ import { PdfFormField } from './pdf-form-field.js';
2
2
  /**
3
3
  * Text form field subtype.
4
4
  */