@pdftron/pdfnet-node-samples 9.4.2 → 9.5.0-beta

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 (147) hide show
  1. package/package.json +18 -18
  2. package/readme.md +12 -12
  3. package/samples/AddImageTest/AddImageTest.js +1 -1
  4. package/samples/AddImageTest/NODEJS/AddImageTest.js +116 -0
  5. package/samples/AddImageTest/NODEJS/RunTest.sh +2 -0
  6. package/samples/AdvancedImagingTest/AdvancedImagingTest.js +1 -1
  7. package/samples/AdvancedImagingTest/NODEJS/AdvancedImagingTest.js +78 -0
  8. package/samples/AdvancedImagingTest/NODEJS/RunTest.sh +2 -0
  9. package/samples/AnnotationTest/AnnotationTest.js +1 -1
  10. package/samples/AnnotationTest/NODEJS/AnnotationTest.js +642 -0
  11. package/samples/AnnotationTest/NODEJS/RunTest.sh +2 -0
  12. package/samples/BookmarkTest/BookmarkTest.js +1 -1
  13. package/samples/BookmarkTest/NODEJS/BookmarkTest.js +220 -0
  14. package/samples/BookmarkTest/NODEJS/RunTest.sh +2 -0
  15. package/samples/CAD2PDFTest/CAD2PDFTest.js +79 -79
  16. package/samples/CAD2PDFTest/NODEJS/CAD2PDFTest.js +80 -0
  17. package/samples/CAD2PDFTest/NODEJS/RunTest.sh +2 -0
  18. package/samples/ContentReplacerTest/ContentReplacerTest.js +1 -1
  19. package/samples/ContentReplacerTest/NODEJS/ContentReplacerTest.js +75 -0
  20. package/samples/ContentReplacerTest/NODEJS/RunTest.sh +2 -0
  21. package/samples/ConvertTest/ConvertTest.js +270 -256
  22. package/samples/ConvertTest/NODEJS/ConvertTest.js +270 -0
  23. package/samples/ConvertTest/NODEJS/RunTest.sh +2 -0
  24. package/samples/DataExtractionTest/DataExtractionTest.js +180 -0
  25. package/samples/DataExtractionTest/NODEJS/DataExtractionTest.js +180 -0
  26. package/samples/DataExtractionTest/NODEJS/RunTest.sh +2 -0
  27. package/samples/DigitalSignaturesTest/DigitalSignaturesTest.js +526 -526
  28. package/samples/DigitalSignaturesTest/NODEJS/DigitalSignaturesTest.js +527 -0
  29. package/samples/DigitalSignaturesTest/NODEJS/RunTest.sh +2 -0
  30. package/samples/ElementBuilderTest/ElementBuilderTest.js +1 -1
  31. package/samples/ElementBuilderTest/NODEJS/ElementBuilderTest.js +514 -0
  32. package/samples/ElementBuilderTest/NODEJS/RunTest.sh +2 -0
  33. package/samples/ElementEditTest/ElementEditTest.js +1 -1
  34. package/samples/ElementEditTest/NODEJS/ElementEditTest.js +111 -0
  35. package/samples/ElementEditTest/NODEJS/RunTest.sh +2 -0
  36. package/samples/ElementReaderAdvTest/ElementReaderAdvTest.js +1 -1
  37. package/samples/ElementReaderAdvTest/NODEJS/ElementReaderAdvTest.js +305 -0
  38. package/samples/ElementReaderAdvTest/NODEJS/RunTest.sh +2 -0
  39. package/samples/ElementReaderTest/ElementReaderTest.js +1 -1
  40. package/samples/ElementReaderTest/NODEJS/ElementReaderTest.js +77 -0
  41. package/samples/ElementReaderTest/NODEJS/RunTest.sh +2 -0
  42. package/samples/EncTest/EncTest.js +1 -1
  43. package/samples/EncTest/NODEJS/EncTest.js +176 -0
  44. package/samples/EncTest/NODEJS/RunTest.sh +2 -0
  45. package/samples/FDFTest/FDFTest.js +218 -218
  46. package/samples/FDFTest/NODEJS/FDFTest.js +219 -0
  47. package/samples/FDFTest/NODEJS/RunTest.sh +2 -0
  48. package/samples/HTML2PDFTest/HTML2PDFTest.js +165 -165
  49. package/samples/HTML2PDFTest/NODEJS/HTML2PDFTest.js +166 -0
  50. package/samples/HTML2PDFTest/NODEJS/RunTest.sh +2 -0
  51. package/samples/HighlightsTest/HighlightsTest.js +1 -1
  52. package/samples/HighlightsTest/NODEJS/HighlightsTest.js +97 -0
  53. package/samples/HighlightsTest/NODEJS/RunTest.sh +2 -0
  54. package/samples/ImageExtractTest/ImageExtractTest.js +129 -129
  55. package/samples/ImageExtractTest/NODEJS/ImageExtractTest.js +130 -0
  56. package/samples/ImageExtractTest/NODEJS/RunTest.sh +2 -0
  57. package/samples/ImpositionTest/ImpositionTest.js +86 -86
  58. package/samples/ImpositionTest/NODEJS/ImpositionTest.js +87 -0
  59. package/samples/ImpositionTest/NODEJS/RunTest.sh +2 -0
  60. package/samples/InteractiveFormsTest/InteractiveFormsTest.js +381 -381
  61. package/samples/InteractiveFormsTest/NODEJS/InteractiveFormsTest.js +382 -0
  62. package/samples/InteractiveFormsTest/NODEJS/RunTest.sh +2 -0
  63. package/samples/JBIG2Test/JBIG2Test.js +88 -88
  64. package/samples/JBIG2Test/NODEJS/JBIG2Test.js +89 -0
  65. package/samples/JBIG2Test/NODEJS/RunTest.sh +2 -0
  66. package/samples/LicenseKey/NODEJS/LicenseKey.js +11 -0
  67. package/samples/LogicalStructureTest/LogicalStructureTest.js +1 -1
  68. package/samples/LogicalStructureTest/NODEJS/LogicalStructureTest.js +251 -0
  69. package/samples/LogicalStructureTest/NODEJS/RunTest.sh +2 -0
  70. package/samples/OCRTest/NODEJS/OCRTest.js +235 -0
  71. package/samples/OCRTest/NODEJS/RunTest.sh +2 -0
  72. package/samples/OCRTest/OCRTest.js +234 -234
  73. package/samples/OfficeTemplateTest/NODEJS/OfficeTemplateTest.js +79 -0
  74. package/samples/OfficeTemplateTest/NODEJS/RunTest.sh +2 -0
  75. package/samples/OfficeTemplateTest/OfficeTemplateTest.js +79 -77
  76. package/samples/OfficeToPDFTest/NODEJS/OfficeToPDFTest.js +125 -0
  77. package/samples/OfficeToPDFTest/NODEJS/RunTest.sh +2 -0
  78. package/samples/OfficeToPDFTest/OfficeToPDFTest.js +125 -125
  79. package/samples/OptimizerTest/NODEJS/OptimizerTest.js +192 -0
  80. package/samples/OptimizerTest/NODEJS/RunTest.sh +2 -0
  81. package/samples/OptimizerTest/OptimizerTest.js +191 -191
  82. package/samples/PDF2HtmlTest/NODEJS/PDF2HtmlTest.js +123 -0
  83. package/samples/PDF2HtmlTest/NODEJS/RunTest.sh +2 -0
  84. package/samples/PDF2HtmlTest/PDF2HtmlTest.js +1 -1
  85. package/samples/PDF2OfficeTest/NODEJS/PDF2OfficeTest.js +158 -0
  86. package/samples/PDF2OfficeTest/NODEJS/RunTest.sh +2 -0
  87. package/samples/PDF2OfficeTest/PDF2OfficeTest.js +1 -1
  88. package/samples/PDFATest/NODEJS/PDFATest.js +85 -0
  89. package/samples/PDFATest/NODEJS/RunTest.sh +2 -0
  90. package/samples/PDFATest/PDFATest.js +1 -1
  91. package/samples/PDFDocMemoryTest/NODEJS/PDFDocMemoryTest.js +85 -0
  92. package/samples/PDFDocMemoryTest/NODEJS/RunTest.sh +2 -0
  93. package/samples/PDFDocMemoryTest/PDFDocMemoryTest.js +84 -84
  94. package/samples/PDFDrawTest/NODEJS/PDFDrawTest.js +306 -0
  95. package/samples/PDFDrawTest/NODEJS/RunTest.sh +2 -0
  96. package/samples/PDFDrawTest/PDFDrawTest.js +1 -1
  97. package/samples/PDFLayersTest/NODEJS/PDFLayersTest.js +295 -0
  98. package/samples/PDFLayersTest/NODEJS/RunTest.sh +2 -0
  99. package/samples/PDFLayersTest/PDFLayersTest.js +1 -1
  100. package/samples/PDFPackageTest/NODEJS/PDFPackageTest.js +112 -0
  101. package/samples/PDFPackageTest/NODEJS/RunTest.sh +2 -0
  102. package/samples/PDFPackageTest/PDFPackageTest.js +111 -111
  103. package/samples/PDFPageTest/NODEJS/PDFPageTest.js +190 -0
  104. package/samples/PDFPageTest/NODEJS/RunTest.sh +2 -0
  105. package/samples/PDFPageTest/PDFPageTest.js +1 -1
  106. package/samples/PDFRedactTest/NODEJS/PDFRedactTest.js +75 -0
  107. package/samples/PDFRedactTest/NODEJS/RunTest.sh +2 -0
  108. package/samples/PDFRedactTest/PDFRedactTest.js +1 -1
  109. package/samples/PageLabelsTest/NODEJS/PageLabelsTest.js +139 -0
  110. package/samples/PageLabelsTest/NODEJS/RunTest.sh +2 -0
  111. package/samples/PageLabelsTest/PageLabelsTest.js +138 -138
  112. package/samples/PatternTest/NODEJS/PatternTest.js +227 -0
  113. package/samples/PatternTest/NODEJS/RunTest.sh +2 -0
  114. package/samples/PatternTest/PatternTest.js +226 -226
  115. package/samples/RectTest/NODEJS/RectTest.js +41 -0
  116. package/samples/RectTest/NODEJS/RunTest.sh +2 -0
  117. package/samples/RectTest/RectTest.js +1 -1
  118. package/samples/SDFTest/NODEJS/RunTest.sh +2 -0
  119. package/samples/SDFTest/NODEJS/SDFTest.js +88 -0
  120. package/samples/SDFTest/SDFTest.js +2 -3
  121. package/samples/StamperTest/NODEJS/RunTest.sh +2 -0
  122. package/samples/StamperTest/NODEJS/StamperTest.js +256 -0
  123. package/samples/StamperTest/StamperTest.js +1 -1
  124. package/samples/TestFiles/financial.pdf +0 -0
  125. package/samples/TestFiles/formfields-scanned.pdf +0 -0
  126. package/samples/TestFiles/formfields.pdf +0 -0
  127. package/samples/TestFiles/table.pdf +0 -0
  128. package/samples/TestFiles/tiger.svg +378 -0
  129. package/samples/TextExtractTest/NODEJS/RunTest.sh +2 -0
  130. package/samples/TextExtractTest/NODEJS/TextExtractTest.js +287 -0
  131. package/samples/TextExtractTest/TextExtractTest.js +1 -1
  132. package/samples/TextSearchTest/NODEJS/RunTest.sh +2 -0
  133. package/samples/TextSearchTest/NODEJS/TextSearchTest.js +122 -0
  134. package/samples/TextSearchTest/TextSearchTest.js +1 -1
  135. package/samples/U3DTest/NODEJS/RunTest.sh +2 -0
  136. package/samples/U3DTest/NODEJS/U3DTest.js +105 -0
  137. package/samples/U3DTest/U3DTest.js +104 -104
  138. package/samples/UndoRedoTest/NODEJS/RunTest.sh +2 -0
  139. package/samples/UndoRedoTest/NODEJS/UndoRedoTest.js +101 -0
  140. package/samples/UndoRedoTest/UndoRedoTest.js +1 -1
  141. package/samples/UnicodeWriteTest/NODEJS/RunTest.sh +2 -0
  142. package/samples/UnicodeWriteTest/NODEJS/UnicodeWriteTest.js +174 -0
  143. package/samples/UnicodeWriteTest/UnicodeWriteTest.js +1 -1
  144. package/samples/WebViewerConvertTest/NODEJS/RunTest.sh +2 -0
  145. package/samples/WebViewerConvertTest/NODEJS/WebViewerConvertTest.js +136 -0
  146. package/samples/WebViewerConvertTest/WebViewerConvertTest.js +135 -135
  147. package/samples/runall.bat +12 -12
@@ -0,0 +1,305 @@
1
+ //---------------------------------------------------------------------------------------
2
+ // Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3
+ // Consult legal.txt regarding legal and license information.
4
+ //---------------------------------------------------------------------------------------
5
+
6
+
7
+ const { PDFNet } = require('../../../lib/pdfnet.js');
8
+ const PDFTronLicense = require('../../LicenseKey/NODEJS/LicenseKey');
9
+
10
+ ((exports) => {
11
+
12
+ exports.runElementReaderAdvTest = () => {
13
+
14
+ const processPath = async (reader, path) => {
15
+ if (await path.isClippingPath()) {
16
+ console.log('This is a clipping path');
17
+ }
18
+
19
+ const d = await path.getPathData();
20
+
21
+ const opr = d.operators;
22
+ const opr_len = opr.byteLength;
23
+ const data = d.points;
24
+ let data_idx = 0, data_len = data.byteLength / data.BYTES_PER_ELEMENT;
25
+
26
+ let x1, y1, x2, y2, x3, y3;
27
+
28
+ // Use path.GetCTM() if you are interested in CTM (current transformation matrix).
29
+
30
+ let path_str = ' Path Data Points := "';
31
+ for (let opr_idx = 0; opr_idx < opr_len; ++opr_idx) {
32
+ switch (opr[opr_idx]) {
33
+ case PDFNet.Element.PathSegmentType.e_moveto:
34
+ x1 = data[data_idx]; ++data_idx;
35
+ y1 = data[data_idx]; ++data_idx;
36
+ path_str += 'M' + Math.round(x1) + ' ' + Math.round(y1);
37
+ break;
38
+ case PDFNet.Element.PathSegmentType.e_lineto:
39
+ x1 = data[data_idx]; ++data_idx;
40
+ y1 = data[data_idx]; ++data_idx;
41
+ path_str += 'L' + Math.round(x1) + ' ' + Math.round(y1);
42
+ break;
43
+ case PDFNet.Element.PathSegmentType.e_cubicto:
44
+ x1 = data[data_idx]; ++data_idx;
45
+ y1 = data[data_idx]; ++data_idx;
46
+ x2 = data[data_idx]; ++data_idx;
47
+ y2 = data[data_idx]; ++data_idx;
48
+ x3 = data[data_idx]; ++data_idx;
49
+ y3 = data[data_idx]; ++data_idx;
50
+ path_str += 'C' + Math.round(x1) + ' ' + Math.round(y1) + ' ' + Math.round(x2)
51
+ + ' ' + Math.round(y2) + ' ' + Math.round(x3) + ' ' + Math.round(y3);
52
+ break;
53
+ case PDFNet.Element.PathSegmentType.e_rect:
54
+ x1 = data[data_idx]; ++data_idx;
55
+ y1 = data[data_idx]; ++data_idx;
56
+ const w = data[data_idx]; ++data_idx;
57
+ const h = data[data_idx]; ++data_idx;
58
+ x2 = x1 + w;
59
+ y2 = y1;
60
+ x3 = x2;
61
+ y3 = y1 + h;
62
+ const x4 = x1;
63
+ const y4 = y3;
64
+ path_str += 'M' + Math.round(x1) + ' ' + Math.round(y1) + ' L' + Math.round(x2) + ' ' + Math.round(y2)
65
+ + ' L' + Math.round(x3) + ' ' + Math.round(y3) + ' L' + Math.round(x4) + ' ' + Math.round(y4) + ' Z';
66
+ break;
67
+ case PDFNet.Element.PathSegmentType.e_closepath:
68
+ path_str += ' Close Path\n';
69
+ break;
70
+ default:
71
+ throw ''
72
+ break;
73
+ }
74
+ }
75
+
76
+ path_str += '" ';
77
+
78
+ const gs = await path.getGState();
79
+
80
+ // Set Path State 0 (stroke, fill, fill-rule) -----------------------------------
81
+ if (await path.isStroked()) {
82
+ console.log(path_str + 'Stroke path');
83
+ path_str = '';
84
+
85
+ if (await (await gs.getStrokeColorSpace()).getType() === PDFNet.ColorSpace.Type.e_pattern) {
86
+ console.log('Path has associated pattern');
87
+ } else {
88
+ // Get stroke color (you can use PDFNet color conversion facilities)
89
+ // ColorPt rgb;
90
+ // gs.GetStrokeColorSpace().Convert2RGB(gs.GetStrokeColor(), rgb);
91
+ }
92
+ } else {
93
+ // Do not stroke path
94
+ }
95
+
96
+ if (await path.isFilled()) {
97
+ console.log(path_str + 'Fill path');
98
+ path_str = '';
99
+
100
+ if (await (await gs.getFillColorSpace()).getType() === PDFNet.ColorSpace.Type.e_pattern) {
101
+ console.log('Path has associated pattern');
102
+ } else {
103
+ // ColorPt rgb;
104
+ // gs.GetFillColorSpace().Convert2RGB(gs.GetFillColor(), rgb);
105
+ }
106
+ } else {
107
+ // Do not fill path
108
+ }
109
+
110
+ if (path_str) {
111
+ console.log(path_str);
112
+ }
113
+
114
+ // Process any changes in graphics state ---------------------------------
115
+
116
+ const gs_itr = await reader.getChangesIterator();
117
+ for (; await gs_itr.hasNext(); await gs_itr.next()) {
118
+ switch (await gs_itr.current()) {
119
+ case PDFNet.GState.Attribute.e_transform:
120
+ // Get transform matrix for this element. Unlike path.GetCTM()
121
+ // that return full transformation matrix gs.GetTransform() return
122
+ // only the transformation matrix that was installed for this element.
123
+ //
124
+ // gs.GetTransform();
125
+ break;
126
+ case PDFNet.GState.Attribute.e_line_width:
127
+ // gs.GetLineWidth();
128
+ break;
129
+ case PDFNet.GState.Attribute.e_line_cap:
130
+ // gs.GetLineCap();
131
+ break;
132
+ case PDFNet.GState.Attribute.e_line_join:
133
+ // gs.GetLineJoin();
134
+ break;
135
+ case PDFNet.GState.Attribute.e_flatness:
136
+ break;
137
+ case PDFNet.GState.Attribute.e_miter_limit:
138
+ // gs.GetMiterLimit();
139
+ break;
140
+ case PDFNet.GState.Attribute.e_dash_pattern:
141
+ {
142
+ // std::vector<double> dashes;
143
+ // gs.GetDashes(dashes);
144
+ // gs.GetPhase()
145
+ }
146
+ break;
147
+ case PDFNet.GState.Attribute.e_fill_color:
148
+ {
149
+ if (await (await gs.getFillColorSpace()).getType() === PDFNet.ColorSpace.Type.e_pattern &&
150
+ await (await gs.getFillPattern()).getType() !== PDFNet.PatternColor.Type.e_shading) {
151
+ //process the pattern data
152
+ await reader.patternBegin(true);
153
+ await processElements(reader);
154
+ await reader.end();
155
+ }
156
+ }
157
+ break;
158
+ }
159
+ }
160
+ await reader.clearChangeList();
161
+ };
162
+
163
+ const processText = async (pageReader) => {
164
+ // Begin text element
165
+ console.log('Begin Text Block:');
166
+
167
+ let element;
168
+ while (element = await pageReader.next()) {
169
+ switch (await element.getType()) {
170
+ case PDFNet.Element.Type.e_text_end:
171
+ // Finish the text block
172
+ console.log('End Text Block.');
173
+ return;
174
+
175
+ case PDFNet.Element.Type.e_text:
176
+ const gs = await element.getGState();
177
+
178
+ const cs_fill = await gs.getFillColorSpace();
179
+ const fill = await gs.getFillColor();
180
+
181
+ const out = await cs_fill.convert2RGB(fill);
182
+
183
+
184
+ const cs_stroke = await gs.getStrokeColorSpace();
185
+ const stroke = await gs.getStrokeColor();
186
+
187
+ const font = await gs.getFont();
188
+
189
+ console.log('Font Name: ' + await font.getName());
190
+
191
+ let outPutStr = '';
192
+ if (await font.getType() == PDFNet.Font.Type.e_Type3) {
193
+ //type 3 font, process its data
194
+ for (const itr = await element.getCharIterator(); await itr.hasNext(); await itr.next()) {
195
+ await pageReader.type3FontBegin(await itr.current());
196
+ await processElements(pageReader);
197
+ await pageReader.end();
198
+ }
199
+ } else {
200
+ const text_mtx = await element.getTextMatrix();
201
+
202
+ for (const itr = await element.getCharIterator(); await itr.hasNext(); await itr.next()) {
203
+ outPutStr += 'Character code: ';
204
+ const charData = await itr.current();
205
+ const charCode = charData.char_code;
206
+ if (charCode >= 32 || charCode <= 127) {
207
+ // Print if in ASCII range...
208
+ outPutStr += String.fromCharCode(charCode);
209
+ }
210
+
211
+ const x = charData.x; // character positioning information
212
+ const y = charData.y;
213
+
214
+ // Use element.GetCTM() if you are interested in the CTM
215
+ // (current transformation matrix).
216
+ const ctm = await element.getCTM();
217
+
218
+ // To get the exact character positioning information you need to
219
+ // concatenate current text matrix with CTM and then multiply
220
+ // relative positioning coordinates with the resulting matrix.
221
+ await ctm.multiply(text_mtx);
222
+ await ctm.mult(x, y);
223
+ }
224
+ }
225
+ console.log(outPutStr);
226
+ break;
227
+ }
228
+ }
229
+ };
230
+
231
+ const processImage = async (image) => {
232
+ const width = await image.getImageWidth();
233
+ const height = await image.getImageHeight();
234
+ const out_data_sz = await width * height * 3;
235
+
236
+ console.log('Image: width=\'' + width + '\' height=\'' + height + '\'');
237
+
238
+ const img_conv = await PDFNet.Filter.createImage2RGBFromElement(image); // Extract and convert image to RGB 8-bpc format
239
+ const reader = await PDFNet.FilterReader.create(img_conv);
240
+
241
+ const image_data_out = await reader.read(out_data_sz);
242
+
243
+ // Note that you don't need to read a whole image at a time. Alternatively
244
+ // you can read a chuck at a time by repeatedly calling reader.Read(buf, buf_sz)
245
+ // until the function returns 0.
246
+ }
247
+
248
+ const processElements = async (reader) => {
249
+ let element;
250
+ while (element = await reader.next()) { // Read page contents
251
+ switch (await element.getType()) {
252
+ case PDFNet.Element.Type.e_path: // Process path data...
253
+ await processPath(reader, element);
254
+ break;
255
+ case PDFNet.Element.Type.e_text_begin: // Process text block...
256
+ await processText(reader);
257
+ break;
258
+ case PDFNet.Element.Type.e_form: // Process form XObjects
259
+ await reader.formBegin();
260
+ await processElements(reader);
261
+ await reader.end();
262
+ break;
263
+ case PDFNet.Element.Type.e_image: // Process Images
264
+ await processImage(element);
265
+ break;
266
+ }
267
+ }
268
+ }
269
+
270
+ const main = async () => {
271
+ // Relative path to the folder containing test files.
272
+ const inputPath = '../../TestFiles/';
273
+ try {
274
+ console.log('-------------------------------------------------');
275
+ console.log('Extract page element information from all ');
276
+ console.log('pages in the document.');
277
+
278
+ const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
279
+ doc.initSecurityHandler();
280
+
281
+ const pgnum = await doc.getPageCount();
282
+ const pageBegin = await doc.getPageIterator();
283
+
284
+ const pageReader = await PDFNet.ElementReader.create();
285
+
286
+ for (const itr = pageBegin; await itr.hasNext(); await itr.next()) // Read every page
287
+ {
288
+ const curPage = await itr.current();
289
+ console.log('Page ' + await curPage.getIndex() + '----------------------------------------');
290
+ await pageReader.beginOnPage(curPage);
291
+ await processElements(pageReader);
292
+ await pageReader.end();
293
+ }
294
+
295
+ console.log('Done.');
296
+ } catch (err) {
297
+ console.log(err);
298
+ }
299
+ };
300
+ PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
301
+ };
302
+ exports.runElementReaderAdvTest();
303
+ })(exports);
304
+ // eslint-disable-next-line spaced-comment
305
+ //# sourceURL=ElementReaderAdvTest.js
@@ -0,0 +1,2 @@
1
+ #!/bin/sh
2
+ node ElementReaderAdvTest.js
@@ -1,5 +1,5 @@
1
1
  //---------------------------------------------------------------------------------------
2
- // Copyright (c) 2001-2022 by PDFTron Systems Inc. All Rights Reserved.
2
+ // Copyright (c) 2001-2023 by PDFTron Systems Inc. All Rights Reserved.
3
3
  // Consult legal.txt regarding legal and license information.
4
4
  //---------------------------------------------------------------------------------------
5
5
 
@@ -0,0 +1,77 @@
1
+ //---------------------------------------------------------------------------------------
2
+ // Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3
+ // Consult legal.txt regarding legal and license information.
4
+ //---------------------------------------------------------------------------------------
5
+
6
+
7
+ const { PDFNet } = require('../../../lib/pdfnet.js');
8
+ const PDFTronLicense = require('../../LicenseKey/NODEJS/LicenseKey');
9
+
10
+ ((exports) => {
11
+
12
+ exports.runElementReaderTest = () => {
13
+
14
+ const ProcessElements = async(reader) => {
15
+ // Read page contents
16
+ for (let element = await reader.next(); element !== null; element = await reader.next()) {
17
+ const temp = await element.getType();
18
+ switch (temp) {
19
+ case PDFNet.Element.Type.e_path: // Process path data...
20
+ {
21
+ const data = await element.getPathData();
22
+ /* eslint-disable no-unused-vars */
23
+ const operators = data.operators;
24
+ const points = data.points;
25
+ /* eslint-enable no-unused-vars */
26
+ }
27
+ break;
28
+ case PDFNet.Element.Type.e_text: // Process text strings...
29
+ {
30
+ const data = await element.getTextString();
31
+ console.log(data);
32
+ }
33
+ break;
34
+ case PDFNet.Element.Type.e_form: // Process form XObjects
35
+ reader.formBegin();
36
+ await ProcessElements(reader);
37
+ reader.end();
38
+ break;
39
+ default:
40
+ }
41
+ }
42
+ };
43
+
44
+ const main = async() => {
45
+ console.log('-------------------------------------------------');
46
+ console.log('Sample 1 - Extract text data from all pages in the document.');
47
+ console.log('Opening the input pdf...');
48
+ const ret = 0;
49
+
50
+ // Relative path to the folder containing test files.
51
+ const inputUrl = '../../TestFiles/';
52
+
53
+ const doc = await PDFNet.PDFDoc.createFromFilePath(inputUrl + 'newsletter.pdf');// await if there is ret that we care about.
54
+ doc.initSecurityHandler();
55
+
56
+ // eslint-disable-next-line no-unused-vars
57
+ const pgnum = await doc.getPageCount();
58
+ const pageReader = await PDFNet.ElementReader.create();
59
+ const itr = await doc.getPageIterator(1);
60
+
61
+ // Read every page
62
+ for (itr; await itr.hasNext(); itr.next()) {
63
+ const curritr = await itr.current();
64
+ pageReader.beginOnPage(curritr);
65
+ await ProcessElements(pageReader);
66
+ pageReader.end();
67
+ }
68
+
69
+ console.log('Done.');
70
+ return ret;
71
+ };
72
+ PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error){console.log('Error: ' + JSON.stringify(error));}).then(function(){return PDFNet.shutdown();});
73
+ };
74
+ exports.runElementReaderTest();
75
+ })(exports);
76
+ // eslint-disable-next-line spaced-comment
77
+ //# sourceURL=ElementReaderTest.js
@@ -0,0 +1,2 @@
1
+ #!/bin/sh
2
+ node ElementReaderTest.js
@@ -1,5 +1,5 @@
1
1
  //---------------------------------------------------------------------------------------
2
- // Copyright (c) 2001-2022 by PDFTron Systems Inc. All Rights Reserved.
2
+ // Copyright (c) 2001-2023 by PDFTron Systems Inc. All Rights Reserved.
3
3
  // Consult legal.txt regarding legal and license information.
4
4
  //---------------------------------------------------------------------------------------
5
5
 
@@ -0,0 +1,176 @@
1
+ //---------------------------------------------------------------------------------------
2
+ // Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3
+ // Consult legal.txt regarding legal and license information.
4
+ //---------------------------------------------------------------------------------------
5
+
6
+ //---------------------------------------------------------------------------------------
7
+ // This sample shows encryption support in PDFNet. The sample reads an encrypted document and
8
+ // sets a new SecurityHandler. The sample also illustrates how password protection can
9
+ // be removed from an existing PDF document.
10
+ //---------------------------------------------------------------------------------------
11
+ const { PDFNet } = require('../../../lib/pdfnet.js');
12
+ const PDFTronLicense = require('../../LicenseKey/NODEJS/LicenseKey');
13
+
14
+ ((exports) => {
15
+ exports.runEncTest = () => {
16
+
17
+ const main = async () => {
18
+ let ret = 0;
19
+ // Relative path to the folder containing test files.
20
+ const inputPath = '../../TestFiles/';
21
+ const outputPath = inputPath + 'Output/';
22
+ // Example 1:
23
+ // secure a PDF document with password protection and adjust permissions
24
+ try {
25
+ // Open the test file
26
+ console.log('-------------------------------------------------/nSecuring an existing document...');
27
+ const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'fish.pdf');
28
+ if (!(await doc.initSecurityHandler())) {
29
+ console.log('Document authentication error...');
30
+ ret = 1;
31
+ }
32
+
33
+ const performOperation = true; // optional parameter
34
+
35
+ // Perform some operation on the document. In this case we use low level SDF API
36
+ // to replace the content stream of the first page with contents of file 'my_stream.txt'
37
+ // Results in fish.pdf becoming a pair of feathers.
38
+ if (performOperation) {
39
+ console.log('Replacing the content stream, use Flate compression...');
40
+ // Get the page dictionary using the following path: trailer/Root/Pages/Kids/0
41
+ const pageTrailer = await doc.getTrailer();
42
+ const pageRoot = await pageTrailer.get('Root');
43
+ const pageRootValue = await pageRoot.value();
44
+ const pages = await pageRootValue.get('Pages');
45
+ const pagesVal = await pages.value();
46
+ const kids = await pagesVal.get('Kids');
47
+ const kidsVal = await kids.value();
48
+ const pageDict = await kidsVal.getAt(0);
49
+
50
+ const embedFile = await PDFNet.Filter.createMappedFileFromUString(inputPath + 'my_stream.txt');
51
+ const mystm = await PDFNet.FilterReader.create(embedFile);
52
+
53
+ const flateEncode = await PDFNet.Filter.createFlateEncode();
54
+
55
+ const indStream = await doc.createIndirectStreamFromFilter(mystm, flateEncode);
56
+ await pageDict.put('Contents', indStream);
57
+ }
58
+
59
+ // Encrypt the document
60
+ // Apply a new security handler with given security settings.
61
+ // In order to open saved PDF you will need a user password 'test'.
62
+ const newHandler = await PDFNet.SecurityHandler.createDefault();
63
+
64
+ // Set a new password required to open a document
65
+ newHandler.changeUserPasswordUString('test');
66
+
67
+ // Set Permissions
68
+ newHandler.setPermission(PDFNet.SecurityHandler.Permission.e_print, true);
69
+ await newHandler.setPermission(PDFNet.SecurityHandler.Permission.e_extract_content, false);
70
+
71
+ // Note: document takes the ownership of newHandler.
72
+ doc.setSecurityHandler(newHandler);
73
+
74
+ // Save the changes
75
+ console.log('Saving modified file...');
76
+ await doc.save(outputPath + 'secured.pdf', 0);
77
+ console.log('Done. Result saved in secured.pdf');
78
+ } catch (err) {
79
+ console.log(err);
80
+ console.log(err.stack);
81
+ ret = 1;
82
+ }
83
+
84
+ // Example 2:
85
+ // Opens an encrypted PDF document and removes its security.
86
+ try {
87
+ console.log('-------------------------------------------------');
88
+ console.log('Open the password protected document from the first example...');
89
+ const securedDoc = await PDFNet.PDFDoc.createFromFilePath(outputPath + 'secured.pdf');
90
+ console.log('Initializing security handler without any user interaction...');
91
+
92
+ // At this point MySecurityHandler callbacks will be invoked.
93
+ // MySecurityHandler.GetAuthorizationData() should collect the password and
94
+ // AuthorizeFailed() is called if user repeatedly enters a wrong password.
95
+ if (!(await securedDoc.initStdSecurityHandlerUString('test'))) {
96
+ console.log('Document authentication error.../nThe password is not valid.');
97
+ ret = 1;
98
+ return ret;
99
+ }
100
+
101
+ console.log('The password is correct! Document can now be used for reading and editing');
102
+
103
+ // Remove the password security and save the changes to a new file.
104
+ securedDoc.removeSecurity();
105
+ await securedDoc.save(outputPath + 'secured_nomore1.pdf', 0);
106
+ console.log('Done. Result saved in secured_nomore1.pdf');
107
+
108
+ /*
109
+ const hdlr = await securedDoc.getSecurityHandler();
110
+
111
+ console.log('Document Open Password: ' + (await hdlr.isUserPasswordRequired()));
112
+ console.log('Permissions Password: ' + (await hdlr.isMasterPasswordRequired()));
113
+ console.log('Permissions: ');
114
+ console.log("\tHas 'owner' permissions: " + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_owner)));
115
+
116
+ console.log('\tOpen and decrypt the document: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_doc_open)));
117
+ console.log('\tAllow content extraction: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_extract_content)));
118
+ console.log('\tAllow full document editing: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_doc_modify)));
119
+ console.log('\tAllow printing: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_print)));
120
+ console.log('\tAllow high resolution printing: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_print_high)));
121
+ console.log('\tAllow annotation editing: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_mod_annot)));
122
+ console.log('\tAllow form fill: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_fill_forms)));
123
+ console.log('\tAllow content extraction for accessibility: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_access_support)));
124
+ console.log('\tAllow document assembly: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_assemble_doc)));
125
+ */
126
+ } catch (err) {
127
+ console.log(err.stack);
128
+ ret = 1;
129
+ }
130
+
131
+ // Example 3:
132
+ // Encrypt/Decrypt a PDF using PDFTron custom security handler
133
+ try {
134
+ console.log('-------------------------------------------------');
135
+ console.log('Encrypt a document using PDFTron Custom Security handler with a custom id and password...');
136
+ const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + "BusinessCardTemplate.pdf");
137
+
138
+ // Create PDFTron custom security handler with a custom id. Replace this with your own integer
139
+ const custom_id = 123456789;
140
+ const custom_handler = await PDFNet.PDFTronCustomSecurityHandler.create(custom_id);
141
+
142
+ // Add a password to the custom security handler
143
+ const pass = 'test';
144
+ await custom_handler.changeUserPasswordUString(pass);
145
+
146
+ // Save the encrypted document
147
+ doc.setSecurityHandler(custom_handler);
148
+ await doc.save(outputPath + 'BusinessCardTemplate_enc.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
149
+
150
+ console.log('Decrypt the PDFTron custom security encrypted document above...');
151
+ // Register the PDFTron Custom Security handler with the same custom id used in encryption
152
+ await PDFNet.addPDFTronCustomHandler(custom_id);
153
+
154
+ const doc_enc = await PDFNet.PDFDoc.createFromFilePath(outputPath + 'BusinessCardTemplate_enc.pdf');
155
+ doc_enc.initStdSecurityHandlerUString(pass);
156
+ doc_enc.removeSecurity();
157
+ // Save the decrypted document
158
+ await doc_enc.save(outputPath + 'BusinessCardTemplate_enc_dec.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
159
+ console.log('Done. Result saved in BusinessCardTemplate_enc_dec.pdf');
160
+ } catch (err) {
161
+ console.log(err.stack);
162
+ ret = 1;
163
+ }
164
+
165
+ console.log('-------------------------------------------------');
166
+ console.log('Tests completed.');
167
+
168
+ return ret;
169
+ };
170
+
171
+ PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
172
+ };
173
+ exports.runEncTest();
174
+ })(exports);
175
+ // eslint-disable-next-line spaced-comment
176
+ //# sourceURL=EncTest.js
@@ -0,0 +1,2 @@
1
+ #!/bin/sh
2
+ node EncTest.js