pdf-oxide 0.3.24
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 +218 -0
- package/binding.gyp +35 -0
- package/package.json +78 -0
- package/src/builders/annotation-builder.ts +367 -0
- package/src/builders/conversion-options-builder.ts +257 -0
- package/src/builders/index.ts +12 -0
- package/src/builders/metadata-builder.ts +317 -0
- package/src/builders/pdf-builder.ts +386 -0
- package/src/builders/search-options-builder.ts +151 -0
- package/src/document-editor-manager.ts +318 -0
- package/src/errors.ts +1629 -0
- package/src/form-field-manager.ts +666 -0
- package/src/hybrid-ml-manager.ts +283 -0
- package/src/index.ts +453 -0
- package/src/managers/accessibility-manager.ts +338 -0
- package/src/managers/annotation-manager.ts +439 -0
- package/src/managers/barcode-manager.ts +235 -0
- package/src/managers/batch-manager.ts +533 -0
- package/src/managers/cache-manager.ts +486 -0
- package/src/managers/compliance-manager.ts +375 -0
- package/src/managers/content-manager.ts +339 -0
- package/src/managers/document-utility-manager.ts +922 -0
- package/src/managers/dom-pdf-creator.ts +365 -0
- package/src/managers/editing-manager.ts +514 -0
- package/src/managers/enterprise-manager.ts +478 -0
- package/src/managers/extended-managers.ts +437 -0
- package/src/managers/extraction-manager.ts +583 -0
- package/src/managers/final-utilities.ts +429 -0
- package/src/managers/hybrid-ml-advanced.ts +479 -0
- package/src/managers/index.ts +239 -0
- package/src/managers/layer-manager.ts +500 -0
- package/src/managers/metadata-manager.ts +303 -0
- package/src/managers/ocr-manager.ts +756 -0
- package/src/managers/optimization-manager.ts +262 -0
- package/src/managers/outline-manager.ts +196 -0
- package/src/managers/page-manager.ts +289 -0
- package/src/managers/pattern-detection.ts +440 -0
- package/src/managers/rendering-manager.ts +863 -0
- package/src/managers/search-manager.ts +385 -0
- package/src/managers/security-manager.ts +345 -0
- package/src/managers/signature-manager.ts +1664 -0
- package/src/managers/streams.ts +618 -0
- package/src/managers/xfa-manager.ts +500 -0
- package/src/pdf-creator-manager.ts +494 -0
- package/src/properties.ts +522 -0
- package/src/result-accessors-manager.ts +867 -0
- package/src/tests/advanced-features.test.ts +414 -0
- package/src/tests/advanced.test.ts +266 -0
- package/src/tests/extended-managers.test.ts +316 -0
- package/src/tests/final-utilities.test.ts +455 -0
- package/src/tests/foundation.test.ts +315 -0
- package/src/tests/high-demand.test.ts +257 -0
- package/src/tests/specialized.test.ts +97 -0
- package/src/thumbnail-manager.ts +272 -0
- package/src/types/common.ts +142 -0
- package/src/types/document-types.ts +457 -0
- package/src/types/index.ts +6 -0
- package/src/types/manager-types.ts +284 -0
- package/src/types/native-bindings.ts +517 -0
- package/src/workers/index.ts +7 -0
- package/src/workers/pool.ts +274 -0
- package/src/workers/worker.ts +131 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DocumentEditorManager for PDF document editing operations
|
|
3
|
+
*
|
|
4
|
+
* Provides methods to edit and modify PDF documents including page operations,
|
|
5
|
+
* content manipulation, and document merging. API is consistent with Python,
|
|
6
|
+
* Java, C#, Go, and Swift implementations.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { EventEmitter } from 'events';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Page rotation options
|
|
13
|
+
*/
|
|
14
|
+
export enum PageRotation {
|
|
15
|
+
None = 0,
|
|
16
|
+
Rotate90 = 90,
|
|
17
|
+
Rotate180 = 180,
|
|
18
|
+
Rotate270 = 270,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Configuration for page insertion
|
|
23
|
+
*/
|
|
24
|
+
export interface InsertConfig {
|
|
25
|
+
pageIndex: number;
|
|
26
|
+
width?: number;
|
|
27
|
+
height?: number;
|
|
28
|
+
content?: Buffer;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Configuration for page extraction
|
|
33
|
+
*/
|
|
34
|
+
export interface ExtractConfig {
|
|
35
|
+
startPage: number;
|
|
36
|
+
endPage: number;
|
|
37
|
+
outputPath?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Configuration for document merging
|
|
42
|
+
*/
|
|
43
|
+
export interface MergeConfig {
|
|
44
|
+
documents: any[];
|
|
45
|
+
outputPath?: string;
|
|
46
|
+
preserveOutlines?: boolean;
|
|
47
|
+
preserveAnnotations?: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Edit operation record
|
|
52
|
+
*/
|
|
53
|
+
export interface EditOperation {
|
|
54
|
+
type: string;
|
|
55
|
+
timestamp: Date;
|
|
56
|
+
details: Record<string, any>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Document Editor Manager for document manipulation
|
|
61
|
+
*
|
|
62
|
+
* Provides methods to:
|
|
63
|
+
* - Delete, insert, and reorder pages
|
|
64
|
+
* - Rotate pages
|
|
65
|
+
* - Extract page ranges to new documents
|
|
66
|
+
* - Merge multiple documents
|
|
67
|
+
* - Track edit history
|
|
68
|
+
*/
|
|
69
|
+
export class DocumentEditorManager extends EventEmitter {
|
|
70
|
+
private document: any;
|
|
71
|
+
private editHistory: EditOperation[] = [];
|
|
72
|
+
private hasUnsavedChanges = false;
|
|
73
|
+
|
|
74
|
+
constructor(document: any) {
|
|
75
|
+
super();
|
|
76
|
+
if (!document) {
|
|
77
|
+
throw new Error('Document is required');
|
|
78
|
+
}
|
|
79
|
+
this.document = document;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Deletes a page from the document
|
|
84
|
+
* Matches: Python deletePage(), Java deletePage(), C# DeletePage()
|
|
85
|
+
*/
|
|
86
|
+
async deletePage(pageIndex: number): Promise<void> {
|
|
87
|
+
this.validatePageIndex(pageIndex);
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
if (this.document?.removePage) {
|
|
91
|
+
this.document.removePage(pageIndex);
|
|
92
|
+
}
|
|
93
|
+
this.recordOperation('deletePage', { pageIndex });
|
|
94
|
+
this.emit('pageDeleted', pageIndex);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
this.emit('error', error);
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Deletes multiple pages from the document
|
|
103
|
+
* Matches: Python deletePages(), Java deletePages(), C# DeletePages()
|
|
104
|
+
*/
|
|
105
|
+
async deletePages(pageIndices: number[]): Promise<void> {
|
|
106
|
+
// Sort in descending order to avoid index shifting issues
|
|
107
|
+
const sorted = [...pageIndices].sort((a, b) => b - a);
|
|
108
|
+
|
|
109
|
+
for (const pageIndex of sorted) {
|
|
110
|
+
await this.deletePage(pageIndex);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Inserts a blank page at the specified position
|
|
116
|
+
* Matches: Python insertBlankPage(), Java insertBlankPage(), C# InsertBlankPage()
|
|
117
|
+
*/
|
|
118
|
+
async insertBlankPage(pageIndex: number, width = 612, height = 792): Promise<void> {
|
|
119
|
+
try {
|
|
120
|
+
if (this.document?.insertPage) {
|
|
121
|
+
this.document.insertPage(pageIndex, width, height);
|
|
122
|
+
}
|
|
123
|
+
this.recordOperation('insertBlankPage', { pageIndex, width, height });
|
|
124
|
+
this.emit('pageInserted', pageIndex);
|
|
125
|
+
} catch (error) {
|
|
126
|
+
this.emit('error', error);
|
|
127
|
+
throw error;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Rotates a page by the specified angle
|
|
133
|
+
* Matches: Python rotatePage(), Java rotatePage(), C# RotatePage()
|
|
134
|
+
*/
|
|
135
|
+
async rotatePage(pageIndex: number, rotation: PageRotation): Promise<void> {
|
|
136
|
+
this.validatePageIndex(pageIndex);
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
if (this.document?.rotatePage) {
|
|
140
|
+
this.document.rotatePage(pageIndex, rotation);
|
|
141
|
+
}
|
|
142
|
+
this.recordOperation('rotatePage', { pageIndex, rotation });
|
|
143
|
+
this.emit('pageRotated', pageIndex, rotation);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
this.emit('error', error);
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Rotates multiple pages
|
|
152
|
+
* Matches: Python rotatePages(), Java rotatePages(), C# RotatePages()
|
|
153
|
+
*/
|
|
154
|
+
async rotatePages(pageIndices: number[], rotation: PageRotation): Promise<void> {
|
|
155
|
+
for (const pageIndex of pageIndices) {
|
|
156
|
+
await this.rotatePage(pageIndex, rotation);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Moves a page to a new position
|
|
162
|
+
* Matches: Python movePage(), Java movePage(), C# MovePage()
|
|
163
|
+
*/
|
|
164
|
+
async movePage(fromIndex: number, toIndex: number): Promise<void> {
|
|
165
|
+
this.validatePageIndex(fromIndex);
|
|
166
|
+
this.validatePageIndex(toIndex);
|
|
167
|
+
|
|
168
|
+
try {
|
|
169
|
+
// Implement move by extracting and re-inserting
|
|
170
|
+
if (this.document?.getPage && this.document?.removePage && this.document?.insertPage) {
|
|
171
|
+
const page = this.document.getPage(fromIndex);
|
|
172
|
+
this.document.removePage(fromIndex);
|
|
173
|
+
// Note: After removal, toIndex may need adjustment
|
|
174
|
+
const adjustedIndex = toIndex > fromIndex ? toIndex - 1 : toIndex;
|
|
175
|
+
if (page) {
|
|
176
|
+
this.document.insertPage(adjustedIndex, page.getWidth?.(), page.getHeight?.());
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
this.recordOperation('movePage', { fromIndex, toIndex });
|
|
180
|
+
this.emit('pageMoved', fromIndex, toIndex);
|
|
181
|
+
} catch (error) {
|
|
182
|
+
this.emit('error', error);
|
|
183
|
+
throw error;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Extracts pages to a new document
|
|
189
|
+
* Matches: Python extractPages(), Java extractPages(), C# ExtractPages()
|
|
190
|
+
*/
|
|
191
|
+
async extractPages(config: ExtractConfig): Promise<any> {
|
|
192
|
+
const { startPage, endPage } = config;
|
|
193
|
+
this.validatePageIndex(startPage);
|
|
194
|
+
this.validatePageIndex(endPage);
|
|
195
|
+
|
|
196
|
+
// FFI call placeholder
|
|
197
|
+
// const newDoc = await FFI.extractPages(this.document.handle, startPage, endPage);
|
|
198
|
+
|
|
199
|
+
this.recordOperation('extractPages', { startPage, endPage });
|
|
200
|
+
this.emit('pagesExtracted', startPage, endPage);
|
|
201
|
+
|
|
202
|
+
return null; // Placeholder - would return new document
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Merges multiple documents into one
|
|
207
|
+
* Matches: Python mergeDocuments(), Java mergeDocuments(), C# MergeDocuments()
|
|
208
|
+
*/
|
|
209
|
+
static async mergeDocuments(config: MergeConfig): Promise<any> {
|
|
210
|
+
const { documents, preserveOutlines = true, preserveAnnotations = true } = config;
|
|
211
|
+
|
|
212
|
+
if (!documents || documents.length === 0) {
|
|
213
|
+
throw new Error('At least one document is required for merging');
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// FFI call placeholder
|
|
217
|
+
// const merged = await FFI.mergeDocuments(documents, preserveOutlines, preserveAnnotations);
|
|
218
|
+
|
|
219
|
+
return null; // Placeholder - would return merged document
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Saves the document with all modifications
|
|
224
|
+
* Matches: Python save(), Java save(), C# Save()
|
|
225
|
+
*/
|
|
226
|
+
async save(outputPath?: string): Promise<void> {
|
|
227
|
+
try {
|
|
228
|
+
if (outputPath) {
|
|
229
|
+
// Save to file path
|
|
230
|
+
if (this.document?.save_async) {
|
|
231
|
+
await this.document.save_async(outputPath);
|
|
232
|
+
} else if (this.document?.saveToPath) {
|
|
233
|
+
this.document.saveToPath(outputPath);
|
|
234
|
+
}
|
|
235
|
+
} else {
|
|
236
|
+
// Save changes to current document
|
|
237
|
+
if (this.document?.save_async) {
|
|
238
|
+
await this.document.save_async(this.document.filePath || 'output.pdf');
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
this.hasUnsavedChanges = false;
|
|
242
|
+
this.emit('saved', outputPath);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
this.emit('error', error);
|
|
245
|
+
throw error;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Gets the edit history
|
|
251
|
+
* Matches: Python getEditHistory(), Java getEditHistory(), C# GetEditHistory()
|
|
252
|
+
*/
|
|
253
|
+
getEditHistory(): EditOperation[] {
|
|
254
|
+
return [...this.editHistory];
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Checks if there are unsaved changes
|
|
259
|
+
* Matches: Python hasUnsavedChanges(), Java hasUnsavedChanges(), C# HasUnsavedChanges()
|
|
260
|
+
*/
|
|
261
|
+
hasChanges(): boolean {
|
|
262
|
+
return this.hasUnsavedChanges;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Undoes the last operation (if supported)
|
|
267
|
+
* Matches: Python undo(), Java undo(), C# Undo()
|
|
268
|
+
*/
|
|
269
|
+
async undo(): Promise<boolean> {
|
|
270
|
+
if (this.editHistory.length === 0) {
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Placeholder - would implement undo logic
|
|
275
|
+
this.editHistory.pop();
|
|
276
|
+
this.emit('undone');
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Clears the edit history
|
|
282
|
+
* Matches: Python clearHistory(), Java clearHistory(), C# ClearHistory()
|
|
283
|
+
*/
|
|
284
|
+
clearHistory(): void {
|
|
285
|
+
this.editHistory = [];
|
|
286
|
+
this.emit('historyCleared');
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Gets the page count after modifications
|
|
291
|
+
*/
|
|
292
|
+
getPageCount(): number {
|
|
293
|
+
try {
|
|
294
|
+
return this.document.pageCount || 0;
|
|
295
|
+
} catch {
|
|
296
|
+
return 0;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Private helper methods
|
|
301
|
+
private validatePageIndex(pageIndex: number): void {
|
|
302
|
+
const count = this.getPageCount();
|
|
303
|
+
if (pageIndex < 0 || pageIndex >= count) {
|
|
304
|
+
throw new Error(`Invalid page index: ${pageIndex}. Document has ${count} pages.`);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
private recordOperation(type: string, details: Record<string, any>): void {
|
|
309
|
+
this.editHistory.push({
|
|
310
|
+
type,
|
|
311
|
+
timestamp: new Date(),
|
|
312
|
+
details,
|
|
313
|
+
});
|
|
314
|
+
this.hasUnsavedChanges = true;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export default DocumentEditorManager;
|