@peculiar/pdf-form-json 1.17.2 → 2.0.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.
- package/README.md +18 -7
- package/build/index.d.ts +152 -166
- package/build/index.js +183 -0
- package/package.json +27 -28
- package/build/peculiar.pdf.form.es.js +0 -203
- package/build/peculiar.pdf.form.js +0 -234
package/README.md
CHANGED
|
@@ -1,20 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
@peculiar/pdf-form-json
|
|
3
|
+
</h1>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+

|
|
9
|
+
[](https://www.npmjs.com/package/@peculiar/pdf-form-json)
|
|
10
|
+
[](https://www.npmjs.com/package/@peculiar/pdf-form-json)
|
|
11
|
+
|
|
12
|
+
</div>
|
|
2
13
|
|
|
3
14
|
`@peculiar/pdf-form-json` is a library for working with PDF form data in JSON format. This library provides a FormConverter class that can convert a PDF document with form data into a JSON representation of that data, and also set values on form components based on a JSON payload.
|
|
4
15
|
|
|
5
16
|
## Installation
|
|
6
17
|
|
|
7
|
-
Using npm:
|
|
8
|
-
|
|
9
18
|
```bash
|
|
10
19
|
npm install @peculiar/pdf-form-json
|
|
11
20
|
```
|
|
12
21
|
|
|
13
|
-
|
|
22
|
+
## Breaking Changes In v2
|
|
14
23
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
24
|
+
v2 is ESM-only and requires Node.js `^20.19.0 || >=22.12.0` (1.x declared no `engines`). CommonJS code can still `require("@peculiar/pdf-form-json")` on these versions; Node.js 22.0–22.11 can't. See the [full notes](https://github.com/PeculiarVentures/pdf#breaking-changes-in-v2).
|
|
25
|
+
|
|
26
|
+
- **TypeScript compiling to CommonJS:** use `"module": "node20"` or `"nodenext"` (`node16` fails with TS1479). ESM and bundler setups need no changes.
|
|
27
|
+
- **Jest in CommonJS mode:** switch to ESM mode or Vitest, or configure an ESM-to-CommonJS transformer and make sure `transformIgnorePatterns` doesn't exclude the `@peculiar/*` packages.
|
|
28
|
+
- **Deep imports** (`@peculiar/pdf-form-json/build/...`) are blocked by `exports`; import from `@peculiar/pdf-form-json`.
|
|
18
29
|
|
|
19
30
|
## Usage
|
|
20
31
|
|
package/build/index.d.ts
CHANGED
|
@@ -1,96 +1,93 @@
|
|
|
1
|
-
import * as pdfDoc from
|
|
2
|
-
import { FormComponentType } from
|
|
3
|
-
|
|
1
|
+
import * as pdfDoc from "@peculiar/pdf-doc";
|
|
2
|
+
import { FormComponentType } from "@peculiar/pdf-doc";
|
|
4
3
|
/**
|
|
5
4
|
* Type representing a constructor for an IComponent instance.
|
|
6
5
|
*/
|
|
7
|
-
type IComponentConstructor<T extends pdfDoc.IComponent> = new (target: any, doc: pdfDoc.PDFDocument) => T;
|
|
6
|
+
export type IComponentConstructor<T extends pdfDoc.IComponent> = new (target: any, doc: pdfDoc.PDFDocument) => T;
|
|
8
7
|
/**
|
|
9
8
|
* Interface representing the JSON object for a form component.
|
|
10
9
|
*/
|
|
11
|
-
interface JsonComponent {
|
|
12
|
-
|
|
10
|
+
export interface JsonComponent {
|
|
11
|
+
name: string;
|
|
13
12
|
}
|
|
14
13
|
/**
|
|
15
14
|
* Interface representing the JSON object for a PDF form.
|
|
16
15
|
*/
|
|
17
|
-
interface JsonForm {
|
|
18
|
-
|
|
16
|
+
export interface JsonForm {
|
|
17
|
+
form: Record<string, JsonComponent>;
|
|
19
18
|
}
|
|
20
19
|
/**
|
|
21
20
|
* Interface representing the update data for a form component.
|
|
22
21
|
*/
|
|
23
|
-
interface JsonComponentUpdate {
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
export interface JsonComponentUpdate {
|
|
23
|
+
type: FormComponentType[keyof FormComponentType];
|
|
24
|
+
name: string;
|
|
26
25
|
}
|
|
27
26
|
/**
|
|
28
27
|
* Interface representing the update data for a check box form component.
|
|
29
28
|
*/
|
|
30
|
-
interface JsonCheckBoxUpdate extends JsonComponentUpdate {
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
export interface JsonCheckBoxUpdate extends JsonComponentUpdate {
|
|
30
|
+
type: "check_box";
|
|
31
|
+
checked: boolean;
|
|
33
32
|
}
|
|
34
33
|
/**
|
|
35
34
|
* Interface representing the update data for a radio button form component.
|
|
36
35
|
*/
|
|
37
|
-
interface JsonRadioButtonUpdate extends JsonComponentUpdate {
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
export interface JsonRadioButtonUpdate extends JsonComponentUpdate {
|
|
37
|
+
type: "radio_button";
|
|
38
|
+
checked: boolean;
|
|
40
39
|
}
|
|
41
40
|
/**
|
|
42
41
|
* Interface representing the update data for a radio button group form component.
|
|
43
42
|
*/
|
|
44
|
-
interface JsonRadioButtonGroupUpdate extends JsonComponentUpdate {
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
export interface JsonRadioButtonGroupUpdate extends JsonComponentUpdate {
|
|
44
|
+
type: "radio_button_group";
|
|
45
|
+
selected: string;
|
|
47
46
|
}
|
|
48
47
|
/**
|
|
49
48
|
* Interface representing the update data for a combo box form component.
|
|
50
49
|
*/
|
|
51
|
-
interface JsonComboBoxUpdate extends JsonComponentUpdate {
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
export interface JsonComboBoxUpdate extends JsonComponentUpdate {
|
|
51
|
+
type: "combo_box";
|
|
52
|
+
selected: string[];
|
|
54
53
|
}
|
|
55
54
|
/**
|
|
56
55
|
* Interface representing the update data for a text editor form component.
|
|
57
56
|
*/
|
|
58
|
-
interface JsonTextEditorUpdate extends JsonComponentUpdate {
|
|
59
|
-
|
|
60
|
-
|
|
57
|
+
export interface JsonTextEditorUpdate extends JsonComponentUpdate {
|
|
58
|
+
type: "text_editor";
|
|
59
|
+
text: string;
|
|
61
60
|
}
|
|
62
61
|
/**
|
|
63
62
|
* Union type for all possible types of form component updates.
|
|
64
63
|
*/
|
|
65
|
-
type JsonUpdateMixed = JsonCheckBoxUpdate | JsonRadioButtonGroupUpdate | JsonRadioButtonUpdate | JsonComboBoxUpdate | JsonTextEditorUpdate;
|
|
66
|
-
|
|
64
|
+
export type JsonUpdateMixed = JsonCheckBoxUpdate | JsonRadioButtonGroupUpdate | JsonRadioButtonUpdate | JsonComboBoxUpdate | JsonTextEditorUpdate;
|
|
67
65
|
/**
|
|
68
66
|
* Abstract class representing a ComponentConverter for an IComponent type.
|
|
69
67
|
*/
|
|
70
|
-
declare abstract class ComponentConverter<T extends pdfDoc.IComponent> {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
|
|
68
|
+
export declare abstract class ComponentConverter<T extends pdfDoc.IComponent> {
|
|
69
|
+
/**
|
|
70
|
+
* The constructor function for the associated IComponent type.
|
|
71
|
+
*/
|
|
72
|
+
type: pdfDoc.IComponentConstructor<T>;
|
|
73
|
+
/**
|
|
74
|
+
* The string value of the FormComponentType for the associated IComponent type.
|
|
75
|
+
*/
|
|
76
|
+
abstract typeJSON: string;
|
|
77
|
+
constructor(type: pdfDoc.IComponentConstructor<T>);
|
|
78
|
+
/**
|
|
79
|
+
* Exports a component to a JSON object.
|
|
80
|
+
* @param component - The component to export.
|
|
81
|
+
* @returns The JSON object representing the component.
|
|
82
|
+
*/
|
|
83
|
+
abstract export(component: T): JsonComponent;
|
|
84
|
+
/**
|
|
85
|
+
* Sets the value of a component based on update data.
|
|
86
|
+
* @param component - The component to update.
|
|
87
|
+
* @param data - The update data for the component.
|
|
88
|
+
*/
|
|
89
|
+
abstract setValue(component: T, data: JsonComponentUpdate): void;
|
|
90
|
+
}
|
|
94
91
|
/**
|
|
95
92
|
* The ComponentConverterFactory is a factory that creates ComponentConverter instances for IComponent types.
|
|
96
93
|
* It contains a map of ComponentConverter instances, keyed by their associated IComponent type.
|
|
@@ -98,153 +95,142 @@ declare abstract class ComponentConverter<T extends pdfDoc.IComponent> {
|
|
|
98
95
|
* @remarks
|
|
99
96
|
* To create a new ComponentConverter instance, call the register method with a new instance of the converter.
|
|
100
97
|
*/
|
|
101
|
-
declare class ComponentConverterFactory {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
98
|
+
export declare class ComponentConverterFactory {
|
|
99
|
+
/**
|
|
100
|
+
* A map of ComponentConverter instances, keyed by their associated IComponent type.
|
|
101
|
+
*/
|
|
102
|
+
protected items: Map<IComponentConstructor<pdfDoc.IComponent>, ComponentConverter<pdfDoc.IComponent>>;
|
|
103
|
+
constructor(...converters: ComponentConverter<pdfDoc.IComponent>[]);
|
|
104
|
+
/**
|
|
105
|
+
* Registers a new ComponentConverter instance for a given IComponent type.
|
|
106
|
+
*
|
|
107
|
+
* @param converter - The ComponentConverter instance to register.
|
|
108
|
+
*/
|
|
109
|
+
register(converter: ComponentConverter<pdfDoc.IComponent>): void;
|
|
110
|
+
/**
|
|
111
|
+
* Finds a ComponentConverter instance for a given IComponent type.
|
|
112
|
+
*
|
|
113
|
+
* @typeparam T - The IComponent type to find a converter for.
|
|
114
|
+
* @param type - The constructor function for the IComponent type to find a converter for.
|
|
115
|
+
* @returns The ComponentConverter instance for the given IComponent type, or null if not found.
|
|
116
|
+
*/
|
|
117
|
+
find<T extends pdfDoc.IComponent>(type: IComponentConstructor<T>): ComponentConverter<T> | null;
|
|
118
|
+
/**
|
|
119
|
+
* Gets a ComponentConverter instance for a given IComponent type.
|
|
120
|
+
*
|
|
121
|
+
* @typeparam T - The IComponent type to get a converter for.
|
|
122
|
+
* @param type - The constructor function for the IComponent type to get a converter for.
|
|
123
|
+
* @returns The ComponentConverter instance for the given IComponent type.
|
|
124
|
+
* @throws If no converter is found for the given IComponent type.
|
|
125
|
+
*/
|
|
126
|
+
get<T extends pdfDoc.IComponent>(type: IComponentConstructor<T>): ComponentConverter<T>;
|
|
127
|
+
}
|
|
132
128
|
/**
|
|
133
129
|
* Abstract class representing a ComponentConverter for a FormComponentGroup type.
|
|
134
130
|
* @typeparam T - The FormComponentGroup type to convert.
|
|
135
131
|
*/
|
|
136
|
-
declare abstract class FieldConverter<T extends pdfDoc.FormComponentGroup> extends ComponentConverter<T> {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
132
|
+
export declare abstract class FieldConverter<T extends pdfDoc.FormComponentGroup> extends ComponentConverter<T> {
|
|
133
|
+
export(component: T): JsonComponent;
|
|
134
|
+
/**
|
|
135
|
+
*
|
|
136
|
+
* @param component Called to add additional data to the JSON object representing the form component.
|
|
137
|
+
* @param component - The form component to export.
|
|
138
|
+
* @param json - The JSON object representing the form component.
|
|
139
|
+
*/
|
|
140
|
+
protected abstract onExport(component: T, json: Record<string, unknown>): void;
|
|
145
141
|
}
|
|
146
|
-
|
|
147
142
|
/**
|
|
148
143
|
* Abstract class representing a ComponentConverter for a FormComponent type.
|
|
149
144
|
* @typeparam T - The FormComponent type to convert.
|
|
150
145
|
*/
|
|
151
|
-
declare abstract class WidgetConverter<T extends pdfDoc.FormComponent> extends ComponentConverter<T> {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
146
|
+
export declare abstract class WidgetConverter<T extends pdfDoc.FormComponent> extends ComponentConverter<T> {
|
|
147
|
+
export(component: T): JsonComponent;
|
|
148
|
+
/**
|
|
149
|
+
* Called to add additional data to the JSON object representing the form component.
|
|
150
|
+
* @param component - The form component to export.
|
|
151
|
+
* @param json - The JSON object representing the form component.
|
|
152
|
+
*/
|
|
153
|
+
protected abstract onExport(component: T, json: Record<string, unknown>): void;
|
|
159
154
|
}
|
|
160
|
-
|
|
161
155
|
/**
|
|
162
156
|
* A ComponentConverter for CheckBox form components.
|
|
163
157
|
*/
|
|
164
|
-
declare class CheckBoxConverter extends WidgetConverter<pdfDoc.CheckBox> {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
158
|
+
export declare class CheckBoxConverter extends WidgetConverter<pdfDoc.CheckBox> {
|
|
159
|
+
typeJSON: pdfDoc.FormComponentType.checkBox;
|
|
160
|
+
constructor();
|
|
161
|
+
protected onExport(component: pdfDoc.CheckBox, json: Record<string, unknown>): void;
|
|
162
|
+
setValue(component: pdfDoc.CheckBox, data: JsonCheckBoxUpdate): void;
|
|
169
163
|
}
|
|
170
|
-
|
|
171
164
|
/**
|
|
172
165
|
* A ComponentConverter for ComboBox form components.
|
|
173
166
|
*/
|
|
174
|
-
declare class ComboBoxConverter extends WidgetConverter<pdfDoc.ComboBox> {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
167
|
+
export declare class ComboBoxConverter extends WidgetConverter<pdfDoc.ComboBox> {
|
|
168
|
+
typeJSON: pdfDoc.FormComponentType.comboBox;
|
|
169
|
+
constructor();
|
|
170
|
+
protected onExport(component: pdfDoc.ComboBox, json: Record<string, unknown>): void;
|
|
171
|
+
setValue(component: pdfDoc.ComboBox, data: JsonComboBoxUpdate): void;
|
|
179
172
|
}
|
|
180
|
-
|
|
181
173
|
/**
|
|
182
174
|
* A ComponentConverter for RadioButton form components.
|
|
183
175
|
*/
|
|
184
|
-
declare class RadioButtonConverter extends WidgetConverter<pdfDoc.RadioButton> {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
176
|
+
export declare class RadioButtonConverter extends WidgetConverter<pdfDoc.RadioButton> {
|
|
177
|
+
typeJSON: pdfDoc.FormComponentType.radioButton;
|
|
178
|
+
constructor();
|
|
179
|
+
protected onExport(component: pdfDoc.RadioButton, json: Record<string, unknown>): void;
|
|
180
|
+
setValue(component: pdfDoc.RadioButton, data: JsonRadioButtonUpdate): void;
|
|
189
181
|
}
|
|
190
|
-
|
|
191
182
|
/**
|
|
192
183
|
* A ComponentConverter for RadioButtonGroup form components.
|
|
193
184
|
*/
|
|
194
|
-
declare class RadioButtonGroupConverter extends FieldConverter<pdfDoc.RadioButtonGroup> {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
185
|
+
export declare class RadioButtonGroupConverter extends FieldConverter<pdfDoc.RadioButtonGroup> {
|
|
186
|
+
typeJSON: pdfDoc.FormComponentType.radioButtonGroup;
|
|
187
|
+
constructor();
|
|
188
|
+
protected onExport(component: pdfDoc.RadioButtonGroup, json: Record<string, unknown>): void;
|
|
189
|
+
setValue(component: pdfDoc.RadioButtonGroup, data: JsonRadioButtonGroupUpdate): void;
|
|
199
190
|
}
|
|
200
|
-
|
|
201
191
|
/**
|
|
202
192
|
* A ComponentConverter for TextEditor form components.
|
|
203
193
|
*/
|
|
204
|
-
declare class TextEditorConverter extends WidgetConverter<pdfDoc.TextEditor> {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
194
|
+
export declare class TextEditorConverter extends WidgetConverter<pdfDoc.TextEditor> {
|
|
195
|
+
typeJSON: pdfDoc.FormComponentType.textEditor;
|
|
196
|
+
constructor();
|
|
197
|
+
protected onExport(component: pdfDoc.TextEditor, json: Record<string, unknown>): void;
|
|
198
|
+
setValue(component: pdfDoc.TextEditor, data: JsonTextEditorUpdate): void;
|
|
209
199
|
}
|
|
210
|
-
|
|
211
200
|
/**
|
|
212
201
|
* A utility class for exporting and updating PDF form components.
|
|
213
202
|
*/
|
|
214
|
-
declare class FormConverter {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}
|
|
244
|
-
|
|
203
|
+
export declare class FormConverter {
|
|
204
|
+
/**
|
|
205
|
+
* The ComponentConverterFactory instance to use for converting PDF form components to JSON.
|
|
206
|
+
*/
|
|
207
|
+
registry: ComponentConverterFactory;
|
|
208
|
+
/**
|
|
209
|
+
* Creates a FormConverter instance.
|
|
210
|
+
* @param registry - The ComponentConverterFactory instance to use for converting PDF form components to JSON.
|
|
211
|
+
*/
|
|
212
|
+
constructor(registry: ComponentConverterFactory);
|
|
213
|
+
/**
|
|
214
|
+
* Exports all PDF form components in a document to JSON format.
|
|
215
|
+
* @param doc - The PDF document to export the form components from.
|
|
216
|
+
* @returns An object containing JSON representations of all form components in the document.
|
|
217
|
+
*/
|
|
218
|
+
export(doc: pdfDoc.PDFDocument): JsonForm;
|
|
219
|
+
/**
|
|
220
|
+
* Exports a single PDF form component to JSON format.
|
|
221
|
+
* @param component - The PDF form component to export.
|
|
222
|
+
* @returns The JSON representation of the specified form component,
|
|
223
|
+
* or null if the component is not supported by the current converter registry.
|
|
224
|
+
*/
|
|
225
|
+
protected exportComponent(component: pdfDoc.IComponent): JsonComponent | null;
|
|
226
|
+
/**
|
|
227
|
+
* Sets the value of one or more PDF form components based on provided JSON updates.
|
|
228
|
+
* @param doc - The PDF document containing the form components to update.
|
|
229
|
+
* @param data - An array of JSON updates to apply to the PDF form components.
|
|
230
|
+
*/
|
|
231
|
+
setValue(doc: pdfDoc.PDFDocument, data: JsonUpdateMixed[]): void;
|
|
232
|
+
}
|
|
245
233
|
/**
|
|
246
234
|
* A global instance of FormConverter that uses the ComponentConverter registry.
|
|
247
235
|
*/
|
|
248
|
-
declare const globalFormConverter: FormConverter;
|
|
249
|
-
|
|
250
|
-
export { CheckBoxConverter, ComboBoxConverter, ComponentConverter, ComponentConverterFactory, FieldConverter, FormConverter, type IComponentConstructor, type JsonCheckBoxUpdate, type JsonComboBoxUpdate, type JsonComponent, type JsonComponentUpdate, type JsonForm, type JsonRadioButtonGroupUpdate, type JsonRadioButtonUpdate, type JsonTextEditorUpdate, type JsonUpdateMixed, RadioButtonConverter, RadioButtonGroupConverter, TextEditorConverter, WidgetConverter, globalFormConverter };
|
|
236
|
+
export declare const globalFormConverter: FormConverter;
|
package/build/index.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import * as pdfDoc from "@peculiar/pdf-doc";
|
|
2
|
+
// src/converters/ComponentConverter.ts
|
|
3
|
+
var ComponentConverter = class {
|
|
4
|
+
constructor(type) {
|
|
5
|
+
this.type = type;
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
// src/converters/ComponentConverter.Factory.ts
|
|
9
|
+
var ComponentConverterFactory = class {
|
|
10
|
+
constructor(...converters) {
|
|
11
|
+
this.items = /* @__PURE__ */ new Map();
|
|
12
|
+
for (const converter of converters) this.register(converter);
|
|
13
|
+
}
|
|
14
|
+
register(converter) {
|
|
15
|
+
this.items.set(converter.type, converter);
|
|
16
|
+
}
|
|
17
|
+
find(type) {
|
|
18
|
+
return this.items.get(type) || null;
|
|
19
|
+
}
|
|
20
|
+
get(type) {
|
|
21
|
+
const converter = this.find(type);
|
|
22
|
+
if (!converter) throw new Error(`Converter not found for type ${type.name}`);
|
|
23
|
+
return converter;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
// src/converters/FieldConverter.ts
|
|
27
|
+
var FieldConverter = class extends ComponentConverter {
|
|
28
|
+
export(component) {
|
|
29
|
+
const json = {
|
|
30
|
+
type: this.typeJSON,
|
|
31
|
+
id: component.id,
|
|
32
|
+
name: component.name
|
|
33
|
+
};
|
|
34
|
+
this.onExport(component, json);
|
|
35
|
+
return json;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
// src/converters/WidgetConverter.ts
|
|
39
|
+
var WidgetConverter = class extends ComponentConverter {
|
|
40
|
+
export(component) {
|
|
41
|
+
const json = {
|
|
42
|
+
type: this.typeJSON,
|
|
43
|
+
id: component.id,
|
|
44
|
+
name: component.name,
|
|
45
|
+
position: {
|
|
46
|
+
left: component.left,
|
|
47
|
+
top: component.top,
|
|
48
|
+
height: component.height,
|
|
49
|
+
width: component.width
|
|
50
|
+
},
|
|
51
|
+
flags: {
|
|
52
|
+
hidden: component.hidden,
|
|
53
|
+
invisible: component.invisible,
|
|
54
|
+
locked: component.locked,
|
|
55
|
+
lockedContents: component.lockedContents,
|
|
56
|
+
noExport: component.noExport,
|
|
57
|
+
noRotate: component.noRotate,
|
|
58
|
+
noView: component.noView,
|
|
59
|
+
noZoom: component.noZoom,
|
|
60
|
+
print: component.print,
|
|
61
|
+
readOnly: component.readOnly,
|
|
62
|
+
readOnlyAnnot: component.readOnlyAnnot,
|
|
63
|
+
required: component.required,
|
|
64
|
+
toggleNoView: component.toggleNoView
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
this.onExport(component, json);
|
|
68
|
+
return json;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
// src/converters/CheckBoxConverter.ts
|
|
72
|
+
var CheckBoxConverter = class extends WidgetConverter {
|
|
73
|
+
constructor() {
|
|
74
|
+
super(pdfDoc.CheckBox);
|
|
75
|
+
this.typeJSON = pdfDoc.FormComponentType.checkBox;
|
|
76
|
+
}
|
|
77
|
+
onExport(component, json) {
|
|
78
|
+
json.checked = component.checked;
|
|
79
|
+
json.value = component.value;
|
|
80
|
+
}
|
|
81
|
+
setValue(component, data) {
|
|
82
|
+
component.checked = data.checked;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
// src/converters/ComboBoxConverter.ts
|
|
86
|
+
var ComboBoxConverter = class extends WidgetConverter {
|
|
87
|
+
constructor() {
|
|
88
|
+
super(pdfDoc.ComboBox);
|
|
89
|
+
this.typeJSON = pdfDoc.FormComponentType.comboBox;
|
|
90
|
+
}
|
|
91
|
+
onExport(component, json) {
|
|
92
|
+
let flags = {};
|
|
93
|
+
if (json.flags && typeof json.flags === "object") flags = json.flags;
|
|
94
|
+
json.flags = {
|
|
95
|
+
...flags,
|
|
96
|
+
combo: component.combo,
|
|
97
|
+
edit: component.edit,
|
|
98
|
+
sort: component.sort,
|
|
99
|
+
multiSelect: component.multiSelect,
|
|
100
|
+
doNotSpellCheck: component.doNotSpellCheck,
|
|
101
|
+
commitOnSelChange: component.commitOnSelChange
|
|
102
|
+
};
|
|
103
|
+
json.options = component.options;
|
|
104
|
+
json.selected = component.selected;
|
|
105
|
+
}
|
|
106
|
+
setValue(component, data) {
|
|
107
|
+
component.selected = data.selected;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
// src/converters/RadioButtonConverter.ts
|
|
111
|
+
var RadioButtonConverter = class extends WidgetConverter {
|
|
112
|
+
constructor() {
|
|
113
|
+
super(pdfDoc.RadioButton);
|
|
114
|
+
this.typeJSON = pdfDoc.FormComponentType.radioButton;
|
|
115
|
+
}
|
|
116
|
+
onExport(component, json) {
|
|
117
|
+
json.checked = component.checked;
|
|
118
|
+
json.value = component.value;
|
|
119
|
+
}
|
|
120
|
+
setValue(component, data) {
|
|
121
|
+
component.checked = data.checked;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
// src/converters/RadioButtonGroupConverter.ts
|
|
125
|
+
var RadioButtonGroupConverter = class extends FieldConverter {
|
|
126
|
+
constructor() {
|
|
127
|
+
super(pdfDoc.RadioButtonGroup);
|
|
128
|
+
this.typeJSON = pdfDoc.FormComponentType.radioButtonGroup;
|
|
129
|
+
}
|
|
130
|
+
onExport(component, json) {
|
|
131
|
+
json.selected = component.selected;
|
|
132
|
+
}
|
|
133
|
+
setValue(component, data) {
|
|
134
|
+
const button = component.get(data.selected);
|
|
135
|
+
button.checked = true;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
// src/converters/TextEditorConverter.ts
|
|
139
|
+
var TextEditorConverter = class extends WidgetConverter {
|
|
140
|
+
constructor() {
|
|
141
|
+
super(pdfDoc.TextEditor);
|
|
142
|
+
this.typeJSON = pdfDoc.FormComponentType.textEditor;
|
|
143
|
+
}
|
|
144
|
+
onExport(component, json) {
|
|
145
|
+
json.maxLen = component.maxLen;
|
|
146
|
+
json.multiline = component.multiline;
|
|
147
|
+
json.text = component.text;
|
|
148
|
+
}
|
|
149
|
+
setValue(component, data) {
|
|
150
|
+
component.text = data.text;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
// src/FormConverter.ts
|
|
154
|
+
var FormConverter = class {
|
|
155
|
+
constructor(registry) {
|
|
156
|
+
this.registry = registry;
|
|
157
|
+
}
|
|
158
|
+
export(doc) {
|
|
159
|
+
const res = { form: {} };
|
|
160
|
+
const components = doc.getComponents();
|
|
161
|
+
for (const component of components) {
|
|
162
|
+
const obj = this.exportComponent(component);
|
|
163
|
+
if (obj) res.form[obj.name] = obj;
|
|
164
|
+
}
|
|
165
|
+
return res;
|
|
166
|
+
}
|
|
167
|
+
exportComponent(component) {
|
|
168
|
+
const converter = this.registry.find(component.constructor);
|
|
169
|
+
if (converter) return converter.export(component);
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
setValue(doc, data) {
|
|
173
|
+
for (const item of data) {
|
|
174
|
+
const component = doc.getComponentByName(item.name);
|
|
175
|
+
if (!component) continue;
|
|
176
|
+
const converter = this.registry.find(component.constructor);
|
|
177
|
+
if (!converter || converter.typeJSON !== item.type) continue;
|
|
178
|
+
converter.setValue(component, item);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
const globalFormConverter = new FormConverter(new ComponentConverterFactory(new CheckBoxConverter(), new RadioButtonConverter(), new TextEditorConverter(), new ComboBoxConverter(), new ComboBoxConverter(), new RadioButtonGroupConverter()));
|
|
183
|
+
export { CheckBoxConverter, ComboBoxConverter, ComponentConverter, ComponentConverterFactory, FieldConverter, FormConverter, RadioButtonConverter, RadioButtonGroupConverter, TextEditorConverter, WidgetConverter, globalFormConverter };
|
package/package.json
CHANGED
|
@@ -1,7 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peculiar/pdf-form-json",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A PDF module enabling working with form elements through JSON.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "./build/index.js",
|
|
8
|
+
"module": "./build/index.js",
|
|
9
|
+
"types": "./build/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": "./build/index.js",
|
|
12
|
+
"./package.json": "./package.json"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"build/",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
24
|
+
},
|
|
5
25
|
"keywords": [
|
|
6
26
|
"pdf",
|
|
7
27
|
"form",
|
|
@@ -9,39 +29,18 @@
|
|
|
9
29
|
"acroform"
|
|
10
30
|
],
|
|
11
31
|
"author": "microshine <microshine@mail.ru>",
|
|
12
|
-
"homepage": "https://github.com/PeculiarVentures/pdf/tree/master/packages/form-json#readme",
|
|
13
32
|
"license": "AGPL",
|
|
14
|
-
"main": "build/peculiar.pdf.form.js",
|
|
15
|
-
"module": "build/peculiar.pdf.form.es.js",
|
|
16
|
-
"types": "build/index.d.ts",
|
|
17
|
-
"publishConfig": {
|
|
18
|
-
"access": "public",
|
|
19
|
-
"registry": "https://registry.npmjs.org/"
|
|
20
|
-
},
|
|
21
|
-
"files": [
|
|
22
|
-
"build",
|
|
23
|
-
"LICENSE",
|
|
24
|
-
"README.md"
|
|
25
|
-
],
|
|
26
33
|
"repository": {
|
|
27
34
|
"type": "git",
|
|
28
|
-
"url": "
|
|
29
|
-
|
|
30
|
-
"scripts": {
|
|
31
|
-
"test": "echo \"Error: run tests from root\" && exit 1",
|
|
32
|
-
"clear": "rimraf build/*",
|
|
33
|
-
"build": "rollup -c",
|
|
34
|
-
"rebuild": "yarn run clear && yarn run build",
|
|
35
|
-
"prepack": "npm run rebuild"
|
|
35
|
+
"url": "https://github.com/PeculiarVentures/pdf",
|
|
36
|
+
"directory": "packages/form-json"
|
|
36
37
|
},
|
|
37
38
|
"bugs": {
|
|
38
39
|
"url": "https://github.com/PeculiarVentures/pdf/issues"
|
|
39
40
|
},
|
|
41
|
+
"homepage": "https://github.com/PeculiarVentures/pdf/tree/main/packages/form-json#readme",
|
|
40
42
|
"dependencies": {
|
|
41
|
-
"@peculiar/pdf-core": "^
|
|
42
|
-
"@peculiar/pdf-doc": "^
|
|
43
|
-
|
|
44
|
-
"tslib": "^2.8.1"
|
|
45
|
-
},
|
|
46
|
-
"gitHead": "5f19d4933bddeca2579656d9fea7d313e795c67b"
|
|
43
|
+
"@peculiar/pdf-core": "^2.0.0",
|
|
44
|
+
"@peculiar/pdf-doc": "^2.0.0"
|
|
45
|
+
}
|
|
47
46
|
}
|
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
import * as pdfDoc from '@peculiar/pdf-doc';
|
|
2
|
-
|
|
3
|
-
class ComponentConverter {
|
|
4
|
-
constructor(type) {
|
|
5
|
-
this.type = type;
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
class ComponentConverterFactory {
|
|
10
|
-
constructor(...converters) {
|
|
11
|
-
this.items = new Map();
|
|
12
|
-
for (const converter of converters) {
|
|
13
|
-
this.register(converter);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
register(converter) {
|
|
17
|
-
this.items.set(converter.type, converter);
|
|
18
|
-
}
|
|
19
|
-
find(type) {
|
|
20
|
-
const converter = this.items.get(type);
|
|
21
|
-
return converter || null;
|
|
22
|
-
}
|
|
23
|
-
get(type) {
|
|
24
|
-
const converter = this.find(type);
|
|
25
|
-
if (!converter) {
|
|
26
|
-
throw new Error(`Converter not found for type ${type.name}`);
|
|
27
|
-
}
|
|
28
|
-
return converter;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
class FieldConverter extends ComponentConverter {
|
|
33
|
-
export(component) {
|
|
34
|
-
const json = {
|
|
35
|
-
type: this.typeJSON,
|
|
36
|
-
id: component.id,
|
|
37
|
-
name: component.name
|
|
38
|
-
};
|
|
39
|
-
this.onExport(component, json);
|
|
40
|
-
return json;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
class WidgetConverter extends ComponentConverter {
|
|
45
|
-
export(component) {
|
|
46
|
-
const json = {
|
|
47
|
-
type: this.typeJSON,
|
|
48
|
-
id: component.id,
|
|
49
|
-
name: component.name,
|
|
50
|
-
position: {
|
|
51
|
-
left: component.left,
|
|
52
|
-
top: component.top,
|
|
53
|
-
height: component.height,
|
|
54
|
-
width: component.width
|
|
55
|
-
},
|
|
56
|
-
flags: {
|
|
57
|
-
hidden: component.hidden,
|
|
58
|
-
invisible: component.invisible,
|
|
59
|
-
locked: component.locked,
|
|
60
|
-
lockedContents: component.lockedContents,
|
|
61
|
-
noExport: component.noExport,
|
|
62
|
-
noRotate: component.noRotate,
|
|
63
|
-
noView: component.noView,
|
|
64
|
-
noZoom: component.noZoom,
|
|
65
|
-
print: component.print,
|
|
66
|
-
readOnly: component.readOnly,
|
|
67
|
-
readOnlyAnnot: component.readOnlyAnnot,
|
|
68
|
-
required: component.required,
|
|
69
|
-
toggleNoView: component.toggleNoView
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
this.onExport(component, json);
|
|
73
|
-
return json;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
class CheckBoxConverter extends WidgetConverter {
|
|
78
|
-
constructor() {
|
|
79
|
-
super(pdfDoc.CheckBox);
|
|
80
|
-
this.typeJSON = pdfDoc.FormComponentType.checkBox;
|
|
81
|
-
}
|
|
82
|
-
onExport(component, json) {
|
|
83
|
-
json.checked = component.checked;
|
|
84
|
-
json.value = component.value;
|
|
85
|
-
}
|
|
86
|
-
setValue(component, data) {
|
|
87
|
-
component.checked = data.checked;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
class ComboBoxConverter extends WidgetConverter {
|
|
92
|
-
constructor() {
|
|
93
|
-
super(pdfDoc.ComboBox);
|
|
94
|
-
this.typeJSON = pdfDoc.FormComponentType.comboBox;
|
|
95
|
-
}
|
|
96
|
-
onExport(component, json) {
|
|
97
|
-
let flags = {};
|
|
98
|
-
if (json.flags && typeof json.flags === "object") {
|
|
99
|
-
flags = json.flags;
|
|
100
|
-
}
|
|
101
|
-
json.flags = {
|
|
102
|
-
...flags,
|
|
103
|
-
combo: component.combo,
|
|
104
|
-
edit: component.edit,
|
|
105
|
-
sort: component.sort,
|
|
106
|
-
multiSelect: component.multiSelect,
|
|
107
|
-
doNotSpellCheck: component.doNotSpellCheck,
|
|
108
|
-
commitOnSelChange: component.commitOnSelChange
|
|
109
|
-
};
|
|
110
|
-
json.options = component.options;
|
|
111
|
-
json.selected = component.selected;
|
|
112
|
-
}
|
|
113
|
-
setValue(component, data) {
|
|
114
|
-
component.selected = data.selected;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
class RadioButtonConverter extends WidgetConverter {
|
|
119
|
-
constructor() {
|
|
120
|
-
super(pdfDoc.RadioButton);
|
|
121
|
-
this.typeJSON = pdfDoc.FormComponentType.radioButton;
|
|
122
|
-
}
|
|
123
|
-
onExport(component, json) {
|
|
124
|
-
json.checked = component.checked;
|
|
125
|
-
json.value = component.value;
|
|
126
|
-
}
|
|
127
|
-
setValue(component, data) {
|
|
128
|
-
component.checked = data.checked;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
class RadioButtonGroupConverter extends FieldConverter {
|
|
133
|
-
constructor() {
|
|
134
|
-
super(pdfDoc.RadioButtonGroup);
|
|
135
|
-
this.typeJSON = pdfDoc.FormComponentType.radioButtonGroup;
|
|
136
|
-
}
|
|
137
|
-
onExport(component, json) {
|
|
138
|
-
json.selected = component.selected;
|
|
139
|
-
}
|
|
140
|
-
setValue(component, data) {
|
|
141
|
-
const button = component.get(data.selected);
|
|
142
|
-
button.checked = true;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
class TextEditorConverter extends WidgetConverter {
|
|
147
|
-
constructor() {
|
|
148
|
-
super(pdfDoc.TextEditor);
|
|
149
|
-
this.typeJSON = pdfDoc.FormComponentType.textEditor;
|
|
150
|
-
}
|
|
151
|
-
onExport(component, json) {
|
|
152
|
-
json.maxLen = component.maxLen;
|
|
153
|
-
json.multiline = component.multiline;
|
|
154
|
-
json.text = component.text;
|
|
155
|
-
}
|
|
156
|
-
setValue(component, data) {
|
|
157
|
-
component.text = data.text;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
class FormConverter {
|
|
162
|
-
constructor(registry) {
|
|
163
|
-
this.registry = registry;
|
|
164
|
-
}
|
|
165
|
-
export(doc) {
|
|
166
|
-
const res = {
|
|
167
|
-
form: {}
|
|
168
|
-
};
|
|
169
|
-
const components = doc.getComponents();
|
|
170
|
-
for (const component of components) {
|
|
171
|
-
const obj = this.exportComponent(component);
|
|
172
|
-
if (obj) {
|
|
173
|
-
res.form[obj.name] = obj;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
return res;
|
|
177
|
-
}
|
|
178
|
-
exportComponent(component) {
|
|
179
|
-
const converter = this.registry.find(component.constructor);
|
|
180
|
-
if (converter) {
|
|
181
|
-
return converter.export(component);
|
|
182
|
-
}
|
|
183
|
-
return null;
|
|
184
|
-
}
|
|
185
|
-
setValue(doc, data) {
|
|
186
|
-
for (const item of data) {
|
|
187
|
-
const component = doc.getComponentByName(item.name);
|
|
188
|
-
if (!component) {
|
|
189
|
-
continue;
|
|
190
|
-
}
|
|
191
|
-
const converter = this.registry.find(component.constructor);
|
|
192
|
-
if (!converter || converter.typeJSON !== item.type) {
|
|
193
|
-
continue;
|
|
194
|
-
}
|
|
195
|
-
converter.setValue(component, item);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const registry = new ComponentConverterFactory(new CheckBoxConverter(), new RadioButtonConverter(), new TextEditorConverter(), new ComboBoxConverter(), new ComboBoxConverter(), new RadioButtonGroupConverter());
|
|
201
|
-
const globalFormConverter = new FormConverter(registry);
|
|
202
|
-
|
|
203
|
-
export { CheckBoxConverter, ComboBoxConverter, ComponentConverter, ComponentConverterFactory, FieldConverter, FormConverter, RadioButtonConverter, RadioButtonGroupConverter, TextEditorConverter, WidgetConverter, globalFormConverter };
|
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var pdfDoc = require('@peculiar/pdf-doc');
|
|
4
|
-
|
|
5
|
-
function _interopNamespaceDefault(e) {
|
|
6
|
-
var n = Object.create(null);
|
|
7
|
-
if (e) {
|
|
8
|
-
Object.keys(e).forEach(function (k) {
|
|
9
|
-
if (k !== 'default') {
|
|
10
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
11
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
12
|
-
enumerable: true,
|
|
13
|
-
get: function () { return e[k]; }
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
n.default = e;
|
|
19
|
-
return Object.freeze(n);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
var pdfDoc__namespace = /*#__PURE__*/_interopNamespaceDefault(pdfDoc);
|
|
23
|
-
|
|
24
|
-
class ComponentConverter {
|
|
25
|
-
constructor(type) {
|
|
26
|
-
this.type = type;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
class ComponentConverterFactory {
|
|
31
|
-
constructor(...converters) {
|
|
32
|
-
this.items = new Map();
|
|
33
|
-
for (const converter of converters) {
|
|
34
|
-
this.register(converter);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
register(converter) {
|
|
38
|
-
this.items.set(converter.type, converter);
|
|
39
|
-
}
|
|
40
|
-
find(type) {
|
|
41
|
-
const converter = this.items.get(type);
|
|
42
|
-
return converter || null;
|
|
43
|
-
}
|
|
44
|
-
get(type) {
|
|
45
|
-
const converter = this.find(type);
|
|
46
|
-
if (!converter) {
|
|
47
|
-
throw new Error(`Converter not found for type ${type.name}`);
|
|
48
|
-
}
|
|
49
|
-
return converter;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
class FieldConverter extends ComponentConverter {
|
|
54
|
-
export(component) {
|
|
55
|
-
const json = {
|
|
56
|
-
type: this.typeJSON,
|
|
57
|
-
id: component.id,
|
|
58
|
-
name: component.name
|
|
59
|
-
};
|
|
60
|
-
this.onExport(component, json);
|
|
61
|
-
return json;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
class WidgetConverter extends ComponentConverter {
|
|
66
|
-
export(component) {
|
|
67
|
-
const json = {
|
|
68
|
-
type: this.typeJSON,
|
|
69
|
-
id: component.id,
|
|
70
|
-
name: component.name,
|
|
71
|
-
position: {
|
|
72
|
-
left: component.left,
|
|
73
|
-
top: component.top,
|
|
74
|
-
height: component.height,
|
|
75
|
-
width: component.width
|
|
76
|
-
},
|
|
77
|
-
flags: {
|
|
78
|
-
hidden: component.hidden,
|
|
79
|
-
invisible: component.invisible,
|
|
80
|
-
locked: component.locked,
|
|
81
|
-
lockedContents: component.lockedContents,
|
|
82
|
-
noExport: component.noExport,
|
|
83
|
-
noRotate: component.noRotate,
|
|
84
|
-
noView: component.noView,
|
|
85
|
-
noZoom: component.noZoom,
|
|
86
|
-
print: component.print,
|
|
87
|
-
readOnly: component.readOnly,
|
|
88
|
-
readOnlyAnnot: component.readOnlyAnnot,
|
|
89
|
-
required: component.required,
|
|
90
|
-
toggleNoView: component.toggleNoView
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
this.onExport(component, json);
|
|
94
|
-
return json;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
class CheckBoxConverter extends WidgetConverter {
|
|
99
|
-
constructor() {
|
|
100
|
-
super(pdfDoc__namespace.CheckBox);
|
|
101
|
-
this.typeJSON = pdfDoc__namespace.FormComponentType.checkBox;
|
|
102
|
-
}
|
|
103
|
-
onExport(component, json) {
|
|
104
|
-
json.checked = component.checked;
|
|
105
|
-
json.value = component.value;
|
|
106
|
-
}
|
|
107
|
-
setValue(component, data) {
|
|
108
|
-
component.checked = data.checked;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
class ComboBoxConverter extends WidgetConverter {
|
|
113
|
-
constructor() {
|
|
114
|
-
super(pdfDoc__namespace.ComboBox);
|
|
115
|
-
this.typeJSON = pdfDoc__namespace.FormComponentType.comboBox;
|
|
116
|
-
}
|
|
117
|
-
onExport(component, json) {
|
|
118
|
-
let flags = {};
|
|
119
|
-
if (json.flags && typeof json.flags === "object") {
|
|
120
|
-
flags = json.flags;
|
|
121
|
-
}
|
|
122
|
-
json.flags = {
|
|
123
|
-
...flags,
|
|
124
|
-
combo: component.combo,
|
|
125
|
-
edit: component.edit,
|
|
126
|
-
sort: component.sort,
|
|
127
|
-
multiSelect: component.multiSelect,
|
|
128
|
-
doNotSpellCheck: component.doNotSpellCheck,
|
|
129
|
-
commitOnSelChange: component.commitOnSelChange
|
|
130
|
-
};
|
|
131
|
-
json.options = component.options;
|
|
132
|
-
json.selected = component.selected;
|
|
133
|
-
}
|
|
134
|
-
setValue(component, data) {
|
|
135
|
-
component.selected = data.selected;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
class RadioButtonConverter extends WidgetConverter {
|
|
140
|
-
constructor() {
|
|
141
|
-
super(pdfDoc__namespace.RadioButton);
|
|
142
|
-
this.typeJSON = pdfDoc__namespace.FormComponentType.radioButton;
|
|
143
|
-
}
|
|
144
|
-
onExport(component, json) {
|
|
145
|
-
json.checked = component.checked;
|
|
146
|
-
json.value = component.value;
|
|
147
|
-
}
|
|
148
|
-
setValue(component, data) {
|
|
149
|
-
component.checked = data.checked;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
class RadioButtonGroupConverter extends FieldConverter {
|
|
154
|
-
constructor() {
|
|
155
|
-
super(pdfDoc__namespace.RadioButtonGroup);
|
|
156
|
-
this.typeJSON = pdfDoc__namespace.FormComponentType.radioButtonGroup;
|
|
157
|
-
}
|
|
158
|
-
onExport(component, json) {
|
|
159
|
-
json.selected = component.selected;
|
|
160
|
-
}
|
|
161
|
-
setValue(component, data) {
|
|
162
|
-
const button = component.get(data.selected);
|
|
163
|
-
button.checked = true;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
class TextEditorConverter extends WidgetConverter {
|
|
168
|
-
constructor() {
|
|
169
|
-
super(pdfDoc__namespace.TextEditor);
|
|
170
|
-
this.typeJSON = pdfDoc__namespace.FormComponentType.textEditor;
|
|
171
|
-
}
|
|
172
|
-
onExport(component, json) {
|
|
173
|
-
json.maxLen = component.maxLen;
|
|
174
|
-
json.multiline = component.multiline;
|
|
175
|
-
json.text = component.text;
|
|
176
|
-
}
|
|
177
|
-
setValue(component, data) {
|
|
178
|
-
component.text = data.text;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
class FormConverter {
|
|
183
|
-
constructor(registry) {
|
|
184
|
-
this.registry = registry;
|
|
185
|
-
}
|
|
186
|
-
export(doc) {
|
|
187
|
-
const res = {
|
|
188
|
-
form: {}
|
|
189
|
-
};
|
|
190
|
-
const components = doc.getComponents();
|
|
191
|
-
for (const component of components) {
|
|
192
|
-
const obj = this.exportComponent(component);
|
|
193
|
-
if (obj) {
|
|
194
|
-
res.form[obj.name] = obj;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return res;
|
|
198
|
-
}
|
|
199
|
-
exportComponent(component) {
|
|
200
|
-
const converter = this.registry.find(component.constructor);
|
|
201
|
-
if (converter) {
|
|
202
|
-
return converter.export(component);
|
|
203
|
-
}
|
|
204
|
-
return null;
|
|
205
|
-
}
|
|
206
|
-
setValue(doc, data) {
|
|
207
|
-
for (const item of data) {
|
|
208
|
-
const component = doc.getComponentByName(item.name);
|
|
209
|
-
if (!component) {
|
|
210
|
-
continue;
|
|
211
|
-
}
|
|
212
|
-
const converter = this.registry.find(component.constructor);
|
|
213
|
-
if (!converter || converter.typeJSON !== item.type) {
|
|
214
|
-
continue;
|
|
215
|
-
}
|
|
216
|
-
converter.setValue(component, item);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
const registry = new ComponentConverterFactory(new CheckBoxConverter(), new RadioButtonConverter(), new TextEditorConverter(), new ComboBoxConverter(), new ComboBoxConverter(), new RadioButtonGroupConverter());
|
|
222
|
-
const globalFormConverter = new FormConverter(registry);
|
|
223
|
-
|
|
224
|
-
exports.CheckBoxConverter = CheckBoxConverter;
|
|
225
|
-
exports.ComboBoxConverter = ComboBoxConverter;
|
|
226
|
-
exports.ComponentConverter = ComponentConverter;
|
|
227
|
-
exports.ComponentConverterFactory = ComponentConverterFactory;
|
|
228
|
-
exports.FieldConverter = FieldConverter;
|
|
229
|
-
exports.FormConverter = FormConverter;
|
|
230
|
-
exports.RadioButtonConverter = RadioButtonConverter;
|
|
231
|
-
exports.RadioButtonGroupConverter = RadioButtonGroupConverter;
|
|
232
|
-
exports.TextEditorConverter = TextEditorConverter;
|
|
233
|
-
exports.WidgetConverter = WidgetConverter;
|
|
234
|
-
exports.globalFormConverter = globalFormConverter;
|