@pdftron/pdfnet-node-samples 10.7.0 → 10.8.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 (59) hide show
  1. package/package.json +21 -20
  2. package/readme.md +12 -12
  3. package/samples/AddImageTest/AddImageTest.js +115 -115
  4. package/samples/AdvancedImagingTest/AdvancedImagingTest.js +78 -78
  5. package/samples/AnnotationTest/AnnotationTest.js +641 -641
  6. package/samples/BookmarkTest/BookmarkTest.js +219 -219
  7. package/samples/CAD2PDFTest/CAD2PDFTest.js +79 -79
  8. package/samples/ContentReplacerTest/ContentReplacerTest.js +75 -75
  9. package/samples/ConvertPrintTest/ConvertPrintTest.js +153 -153
  10. package/samples/ConvertTest/ConvertTest.js +203 -203
  11. package/samples/DataExtractionTest/DataExtractionTest.js +214 -214
  12. package/samples/DigitalSignaturesTest/DigitalSignaturesTest.js +526 -526
  13. package/samples/DocumentCreationTest/DocumentCreationTest.js +409 -409
  14. package/samples/ElementBuilderTest/ElementBuilderTest.js +513 -513
  15. package/samples/ElementEditTest/ElementEditTest.js +110 -110
  16. package/samples/ElementReaderAdvTest/ElementReaderAdvTest.js +305 -305
  17. package/samples/ElementReaderTest/ElementReaderTest.js +77 -77
  18. package/samples/EncTest/EncTest.js +175 -175
  19. package/samples/FDFTest/FDFTest.js +218 -218
  20. package/samples/HTML2PDFTest/HTML2PDFTest.js +164 -164
  21. package/samples/HighlightsTest/HighlightsTest.js +97 -97
  22. package/samples/ImageExtractTest/ImageExtractTest.js +129 -129
  23. package/samples/ImpositionTest/ImpositionTest.js +86 -86
  24. package/samples/InteractiveFormsTest/InteractiveFormsTest.js +381 -381
  25. package/samples/JBIG2Test/JBIG2Test.js +88 -88
  26. package/samples/LicenseKey/LicenseKey.js +11 -11
  27. package/samples/LogicalStructureTest/LogicalStructureTest.js +250 -250
  28. package/samples/OCRTest/OCRTest.js +235 -235
  29. package/samples/OfficeTemplateTest/OfficeTemplateTest.js +79 -79
  30. package/samples/OfficeToPDFTest/OfficeToPDFTest.js +125 -125
  31. package/samples/OptimizerTest/OptimizerTest.js +191 -191
  32. package/samples/PDF2HtmlTest/PDF2HtmlTest.js +123 -123
  33. package/samples/PDF2OfficeTest/PDF2OfficeTest.js +158 -158
  34. package/samples/PDFATest/PDFATest.js +85 -85
  35. package/samples/PDFDocMemoryTest/PDFDocMemoryTest.js +84 -84
  36. package/samples/PDFDrawTest/PDFDrawTest.js +305 -305
  37. package/samples/PDFLayersTest/PDFLayersTest.js +294 -294
  38. package/samples/PDFPackageTest/PDFPackageTest.js +111 -111
  39. package/samples/PDFPageTest/PDFPageTest.js +189 -189
  40. package/samples/PDFRedactTest/PDFRedactTest.js +74 -74
  41. package/samples/PageLabelsTest/PageLabelsTest.js +138 -138
  42. package/samples/PatternTest/PatternTest.js +226 -226
  43. package/samples/RectTest/RectTest.js +40 -40
  44. package/samples/SDFTest/SDFTest.js +87 -87
  45. package/samples/StamperTest/StamperTest.js +255 -255
  46. package/samples/TestFiles/Misc-Fixed.pfa +1166 -1166
  47. package/samples/TestFiles/SHA-2 Root USERTrust RSA CA Sectigo timestamping.crt +34 -34
  48. package/samples/TestFiles/form1_annots.xfdf +33 -33
  49. package/samples/TestFiles/form1_data.xfdf +139 -139
  50. package/samples/TestFiles/my_stream.txt +2310 -2310
  51. package/samples/TestFiles/tiger.svg +378 -378
  52. package/samples/TextExtractTest/TextExtractTest.js +286 -286
  53. package/samples/TextSearchTest/TextSearchTest.js +121 -121
  54. package/samples/U3DTest/U3DTest.js +104 -104
  55. package/samples/UndoRedoTest/UndoRedoTest.js +101 -101
  56. package/samples/UnicodeWriteTest/UnicodeWriteTest.js +173 -173
  57. package/samples/WebViewerConvertTest/WebViewerConvertTest.js +135 -135
  58. package/samples/runall.bat +12 -12
  59. package/samples/runall.sh +15 -15
@@ -1,306 +1,306 @@
1
- //---------------------------------------------------------------------------------------
2
- // Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3
- // Consult legal.txt regarding legal and license information.
4
- //---------------------------------------------------------------------------------------
5
-
6
- //---------------------------------------------------------------------------------------
7
- // The following sample illustrates how to convert PDF documents to various raster image
8
- // formats (such as PNG, JPEG, BMP, TIFF, etc), as well as how to convert a PDF page to
9
- // GDI+ Bitmap for further manipulation and/or display in WinForms applications.
10
- //---------------------------------------------------------------------------------------
11
-
12
- const fs = require('fs');
13
- const { PDFNet } = require('@pdftron/pdfnet-node');
14
- const PDFTronLicense = require('../LicenseKey/LicenseKey');
15
-
16
- ((exports) => {
17
-
18
- exports.runPDFDrawTest = () => {
19
-
20
- const main = async () => {
21
- // Relative path to the folder containing test files.
22
- const inputPath = '../TestFiles/';
23
- const outputPath = inputPath + 'Output/';
24
-
25
- try {
26
-
27
- // Optional: Set ICC color profiles to fine tune color conversion
28
- // for PDF 'device' color spaces...
29
-
30
- // PDFNet.setResourcesPath('../resources');
31
- // PDFNet.setColorManagement(PDFNet.CMSType.e_lcms);
32
- // PDFNet.setDefaultDeviceCMYKProfile('D:/Misc/ICC/USWebCoatedSWOP.icc');
33
- // PDFNet.setDefaultDeviceRGBProfile('AdobeRGB1998.icc'); // will search in PDFNet resource folder.
34
-
35
- // ----------------------------------------------------
36
- // Optional: Set predefined font mappings to override default font
37
- // substitution for documents with missing fonts...
38
-
39
- // PDFNet.addFontSubst('StoneSans-Semibold', 'C:/WINDOWS/Fonts/comic.ttf');
40
- // PDFNet.addFontSubst('StoneSans', 'comic.ttf'); // search for 'comic.ttf' in PDFNet resource folder.
41
- // PDFNet.addFontSubst(PDFNet.CharacterOrdering.e_Identity, 'C:/WINDOWS/Fonts/arialuni.ttf');
42
- // PDFNet.addFontSubst(PDFNet.CharacterOrdering.e_Japan1, 'C:/Program Files/Adobe/Acrobat 7.0/Resource/CIDFont/KozMinProVI-Regular.otf');
43
- // PDFNet.addFontSubst(PDFNet.CharacterOrdering.e_Japan2, 'c:/myfonts/KozMinProVI-Regular.otf');
44
- // PDFNet.addFontSubst(PDFNet.CharacterOrdering.e_Korea1, 'AdobeMyungjoStd-Medium.otf');
45
- // PDFNet.addFontSubst(PDFNet.CharacterOrdering.e_CNS1, 'AdobeSongStd-Light.otf');
46
- // PDFNet.addFontSubst(PDFNet.CharacterOrdering.e_GB1, 'AdobeMingStd-Light.otf');
47
-
48
- const draw = await PDFNet.PDFDraw.create(); // PDFDraw class is used to rasterize PDF pages.
49
-
50
- //--------------------------------------------------------------------------------
51
- // Example 1) Convert the first page to PNG and TIFF at 92 DPI.
52
- // A three step tutorial to convert PDF page to an image.
53
- try {
54
- // A) Open the PDF document.
55
- const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'tiger.pdf');
56
-
57
- // Initialize the security handler, in case the PDF is encrypted.
58
- doc.initSecurityHandler();
59
-
60
- // B) The output resolution is set to 92 DPI.
61
- draw.setDPI(92);
62
-
63
- const firstPage = await (await doc.getPageIterator()).current();
64
- // C) Rasterize the first page in the document and save the result as PNG.
65
- await draw.export(firstPage, outputPath + 'tiger_92dpi.png');
66
-
67
- console.log('Example 1: tiger_92dpi.png');
68
-
69
- // Export the same page as TIFF
70
- await draw.export(firstPage, outputPath + 'tiger_92dpi.tif', 'TIFF');
71
- } catch (err) {
72
- console.log(err);
73
- }
74
-
75
- //--------------------------------------------------------------------------------
76
- // Example 2) Convert the all pages in a given document to JPEG at 72 DPI.
77
- console.log('Example 2:');
78
- const hint_set = await PDFNet.ObjSet.create(); // A collection of rendering 'hits'.
79
- try {
80
- const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
81
- // Initialize the security handler, in case the PDF is encrypted.
82
- doc.initSecurityHandler();
83
-
84
- draw.setDPI(72); // Set the output resolution is to 72 DPI.
85
-
86
- // Use optional encoder parameter to specify JPEG quality.
87
- const encoderParam = await hint_set.createDict();
88
- await encoderParam.putNumber('Quality', 80);
89
-
90
- // Traverse all pages in the document.
91
- for (const itr = await doc.getPageIterator(); await itr.hasNext(); await itr.next()) {
92
- const currPage = await itr.current();
93
- const currPageIdx = await currPage.getIndex();
94
- const path = outputPath + 'newsletter' + currPageIdx + '.jpg';
95
- console.log('newsletter' + currPageIdx + '.jpg');
96
-
97
- await draw.export(currPage, path, 'JPEG', encoderParam);
98
- }
99
- console.log('Done.');
100
- } catch (err) {
101
- console.log(err);
102
- }
103
-
104
- // Examples 3-6
105
- try {
106
- // Common code for remaining samples.
107
- const tiger_doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'tiger.pdf');
108
- // Initialize the security handler, in case the PDF is encrypted.
109
- tiger_doc.initSecurityHandler();
110
- const page = await tiger_doc.getPage(1);
111
-
112
- //--------------------------------------------------------------------------------
113
- // Example 3) Convert the first page to raw bitmap. Also, rotate the
114
- // page 90 degrees and save the result as RAW.
115
- draw.setDPI(100); // Set the output resolution is to 100 DPI.
116
- draw.setRotate(PDFNet.Page.Rotate.e_90); // Rotate all pages 90 degrees clockwise.
117
-
118
- const bitmapInfo = await draw.getBitmap(page, PDFNet.PDFDraw.PixelFormat.e_rgb, false);
119
- const buf = Buffer.from(bitmapInfo.buf, 0, bitmapInfo.height * bitmapInfo.stride)
120
-
121
- // Save the raw RGB data to disk.
122
- fs.appendFileSync(outputPath + 'tiger_100dpi_rot90.raw', buf, 'binary');
123
-
124
- console.log('Example 3: tiger_100dpi_rot90.raw');
125
- draw.setRotate(PDFNet.Page.Rotate.e_0); // Disable image rotation for remaining samples.
126
-
127
- //--------------------------------------------------------------------------------
128
- // Example 4) Convert PDF page to a fixed image size. Also illustrates some
129
- // other features in PDFDraw class such as rotation, image stretching, exporting
130
- // to grayscale, or monochrome.
131
-
132
- // Initialize render 'gray_hint' parameter, that is used to control the
133
- // rendering process. In this case we tell the rasterizer to export the image as
134
- // 1 Bit Per Component (BPC) image.
135
- const mono_hint = await hint_set.createDict();
136
- await mono_hint.putNumber('BPC', 1);
137
-
138
- // SetImageSize can be used instead of SetDPI() to adjust page scaling
139
- // dynamically so that given image fits into a buffer of given dimensions.
140
- draw.setImageSize(1000, 1000); // Set the output image to be 1000 wide and 1000 pixels tall
141
- draw.export(page, outputPath + 'tiger_1000x1000.png', 'PNG', mono_hint);
142
- console.log('Example 4: tiger_1000x1000.png');
143
-
144
- draw.setImageSize(200, 400); // Set the output image to be 200 wide and 300 pixels tall
145
- draw.setRotate(PDFNet.Page.Rotate.e_180); // Rotate all pages 90 degrees clockwise.
146
-
147
- // 'gray_hint' tells the rasterizer to export the image as grayscale.
148
- const gray_hint = await hint_set.createDict();
149
- await gray_hint.putName('ColorSpace', 'Gray');
150
-
151
- await draw.export(page, outputPath + 'tiger_200x400_rot180.png', 'PNG', gray_hint);
152
- console.log('Example 4: tiger_200x400_rot180.png');
153
-
154
- draw.setImageSize(400, 200, false); // The third parameter sets 'preserve-aspect-ratio' to false.
155
- draw.setRotate(PDFNet.Page.Rotate.e_0); // Disable image rotation.
156
- await draw.export(page, outputPath + 'tiger_400x200_stretch.jpg', 'JPEG');
157
- console.log('Example 4: tiger_400x200_stretch.jpg');
158
-
159
- //--------------------------------------------------------------------------------
160
- // Example 5) Zoom into a specific region of the page and rasterize the
161
- // area at 200 DPI and as a thumbnail (i.e. a 50x50 pixel image).
162
- const zoom_rect = await PDFNet.Rect.init(216, 522, 330, 600);
163
- await page.setCropBox(zoom_rect); // Set the page crop box.
164
-
165
- // Select the crop region to be used for drawing.
166
- draw.setPageBox(PDFNet.Page.Box.e_crop);
167
- draw.setDPI(900); // Set the output image resolution to 900 DPI.
168
- await draw.export(page, outputPath + 'tiger_zoom_900dpi.png', 'PNG');
169
- console.log('Example 5: tiger_zoom_900dpi.png');
170
-
171
- // -------------------------------------------------------------------------------
172
- // Example 6)
173
- draw.setImageSize(50, 50); // Set the thumbnail to be 50x50 pixel image.
174
- await draw.export(page, outputPath + 'tiger_zoom_50x50.png', 'PNG');
175
- console.log('Example 6: tiger_zoom_50x50.png');
176
- } catch (err) {
177
- console.log(err);
178
- }
179
-
180
- //--------------------------------------------------------------------------------
181
- // Example 7) Convert the first PDF page to CMYK TIFF at 92 DPI.
182
- // A three step tutorial to convert PDF page to an image
183
- try {
184
- const cmyk_hint = await hint_set.createDict();
185
- await cmyk_hint.putName('ColorSpace', 'CMYK');
186
- // A) Open the PDF document.
187
- const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'tiger.pdf');
188
- // Initialize the security handler, in case the PDF is encrypted.
189
- doc.initSecurityHandler();
190
-
191
- // B) The output resolution is set to 92 DPI.
192
- draw.setDPI(92);
193
-
194
- // C) Rasterize the first page in the document and save the result as TIFF.
195
- const pg = await doc.getPage(1);
196
- await draw.export(pg, outputPath + 'out1.tif', 'TIFF', cmyk_hint);
197
- console.log('Example 7: out1.tif');
198
- } catch (err) {
199
- console.log(err);
200
- }
201
-
202
- //--------------------------------------------------------------------------------
203
- // Example 8) PDFRasterizer can be used for more complex rendering tasks, such as
204
- // strip by strip or tiled document rendering. In particular, it is useful for
205
- // cases where you cannot simply modify the page crop box (interactive viewing,
206
- // parallel rendering). This example shows how you can rasterize the south-west
207
- // quadrant of a page.
208
- try {
209
- // A) Open the PDF document.
210
- const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'tiger.pdf');
211
- // Initialize the security handler, in case the PDF is encrypted.
212
- doc.initSecurityHandler();
213
-
214
- // B) Get the page matrix
215
- const pg = await doc.getPage(1);
216
- const box = PDFNet.Page.Rotate.e_crop;
217
- let mtx = await pg.getDefaultMatrix(true, box);
218
- // We want to render a quadrant, so use half of width and height
219
- const pg_w = await pg.getPageWidth(box) / 2;
220
- const pg_h = await pg.getPageHeight(box) / 2;
221
-
222
- // C) Scale matrix from PDF space to buffer space
223
- const dpi = 96.0;
224
- const scale = dpi / 72.0; // PDF space is 72 dpi
225
- const buf_w = Math.floor(scale * pg_w);
226
- const buf_h = Math.floor(scale * pg_h);
227
- const bytes_per_pixel = 4; // BGRA buffer
228
- await mtx.translate(0, -pg_h); // translate by '-pg_h' since we want south-west quadrant
229
- const scale_mtx = await PDFNet.Matrix2D.create(scale, 0, 0, scale, 0, 0);
230
- await scale_mtx.multiply(mtx);
231
- mtx = scale_mtx;
232
-
233
- // D) Rasterize page into memory buffer, according to our parameters
234
- const rast = await PDFNet.PDFRasterizer.create();
235
- const buf = await rast.rasterize(pg, buf_w, buf_h, buf_w * bytes_per_pixel, bytes_per_pixel, true, mtx);
236
-
237
- // buf now contains raw BGRA bitmap.
238
- console.log('Example 8: Successfully rasterized into memory buffer.');
239
- } catch (err) {
240
- console.log(err);
241
- }
242
-
243
- //--------------------------------------------------------------------------------
244
- // Example 9) Export raster content to PNG using different image smoothing settings.
245
- try {
246
- const text_doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'lorem_ipsum.pdf');
247
- text_doc.initSecurityHandler();
248
- const itr = await text_doc.getPageIterator();
249
- const page = await itr.current();
250
-
251
- draw.setImageSmoothing(false, false);
252
- let filename = 'raster_text_no_smoothing.png';
253
- await draw.export(page, outputPath + filename);
254
- console.log('Example 9 a): ' + filename + '. Done.');
255
-
256
- filename = 'raster_text_smoothed.png';
257
- draw.setImageSmoothing(true, false /*default quality bilinear resampling*/);
258
- await draw.export(page, outputPath + filename);
259
- console.log('Example 9 b): ' + filename + '. Done.');
260
-
261
- filename = 'raster_text_high_quality.png';
262
- draw.setImageSmoothing(true, true /*high quality area resampling*/);
263
- await draw.export(page, outputPath + filename);
264
- console.log('Example 9 c): ' + filename + '. Done.');
265
- } catch (err) {
266
- console.log(err);
267
- }
268
-
269
- //--------------------------------------------------------------------------------
270
- // Example 10) Export separations directly, without conversion to an output colorspace
271
- try {
272
- const separation_doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'op_blend_test.pdf');
273
- separation_doc.initSecurityHandler();
274
- const separation_hint = await hint_set.createDict();
275
- await separation_hint.putName('ColorSpace', 'Separation');
276
- draw.setDPI(96);
277
- draw.setImageSmoothing(true, true);
278
- draw.setOverprint(PDFNet.PDFRasterizer.OverprintPreviewMode.e_op_on);
279
-
280
- const itr = await separation_doc.getPageIterator();
281
- const page = await itr.current();
282
- let filename = 'merged_separations.png';
283
- await draw.export(page, outputPath + filename, 'PNG');
284
- console.log('Example 10 a): ' + filename + '. Done.');
285
-
286
- filename = 'separation';
287
- await draw.export(page, outputPath + filename, 'PNG', separation_hint);
288
- console.log('Example 10 b): ' + filename + '_[ink].png. Done.');
289
-
290
- filename = 'separation_NChannel.tif';
291
- await draw.export(page, outputPath + filename, 'TIFF', separation_hint);
292
- console.log('Example 10 c): ' + filename + '. Done.');
293
- } catch (err) {
294
- console.log(err);
295
- }
296
- } catch (err) {
297
- console.log(err);
298
- }
299
- };
300
-
301
- PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
302
- };
303
- exports.runPDFDrawTest();
304
- })(exports);
305
- // eslint-disable-next-line spaced-comment
1
+ //---------------------------------------------------------------------------------------
2
+ // Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3
+ // Consult legal.txt regarding legal and license information.
4
+ //---------------------------------------------------------------------------------------
5
+
6
+ //---------------------------------------------------------------------------------------
7
+ // The following sample illustrates how to convert PDF documents to various raster image
8
+ // formats (such as PNG, JPEG, BMP, TIFF, etc), as well as how to convert a PDF page to
9
+ // GDI+ Bitmap for further manipulation and/or display in WinForms applications.
10
+ //---------------------------------------------------------------------------------------
11
+
12
+ const fs = require('fs');
13
+ const { PDFNet } = require('@pdftron/pdfnet-node');
14
+ const PDFTronLicense = require('../LicenseKey/LicenseKey');
15
+
16
+ ((exports) => {
17
+
18
+ exports.runPDFDrawTest = () => {
19
+
20
+ const main = async () => {
21
+ // Relative path to the folder containing test files.
22
+ const inputPath = '../TestFiles/';
23
+ const outputPath = inputPath + 'Output/';
24
+
25
+ try {
26
+
27
+ // Optional: Set ICC color profiles to fine tune color conversion
28
+ // for PDF 'device' color spaces...
29
+
30
+ // PDFNet.setResourcesPath('../resources');
31
+ // PDFNet.setColorManagement(PDFNet.CMSType.e_lcms);
32
+ // PDFNet.setDefaultDeviceCMYKProfile('D:/Misc/ICC/USWebCoatedSWOP.icc');
33
+ // PDFNet.setDefaultDeviceRGBProfile('AdobeRGB1998.icc'); // will search in PDFNet resource folder.
34
+
35
+ // ----------------------------------------------------
36
+ // Optional: Set predefined font mappings to override default font
37
+ // substitution for documents with missing fonts...
38
+
39
+ // PDFNet.addFontSubst('StoneSans-Semibold', 'C:/WINDOWS/Fonts/comic.ttf');
40
+ // PDFNet.addFontSubst('StoneSans', 'comic.ttf'); // search for 'comic.ttf' in PDFNet resource folder.
41
+ // PDFNet.addFontSubst(PDFNet.CharacterOrdering.e_Identity, 'C:/WINDOWS/Fonts/arialuni.ttf');
42
+ // PDFNet.addFontSubst(PDFNet.CharacterOrdering.e_Japan1, 'C:/Program Files/Adobe/Acrobat 7.0/Resource/CIDFont/KozMinProVI-Regular.otf');
43
+ // PDFNet.addFontSubst(PDFNet.CharacterOrdering.e_Japan2, 'c:/myfonts/KozMinProVI-Regular.otf');
44
+ // PDFNet.addFontSubst(PDFNet.CharacterOrdering.e_Korea1, 'AdobeMyungjoStd-Medium.otf');
45
+ // PDFNet.addFontSubst(PDFNet.CharacterOrdering.e_CNS1, 'AdobeSongStd-Light.otf');
46
+ // PDFNet.addFontSubst(PDFNet.CharacterOrdering.e_GB1, 'AdobeMingStd-Light.otf');
47
+
48
+ const draw = await PDFNet.PDFDraw.create(); // PDFDraw class is used to rasterize PDF pages.
49
+
50
+ //--------------------------------------------------------------------------------
51
+ // Example 1) Convert the first page to PNG and TIFF at 92 DPI.
52
+ // A three step tutorial to convert PDF page to an image.
53
+ try {
54
+ // A) Open the PDF document.
55
+ const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'tiger.pdf');
56
+
57
+ // Initialize the security handler, in case the PDF is encrypted.
58
+ doc.initSecurityHandler();
59
+
60
+ // B) The output resolution is set to 92 DPI.
61
+ draw.setDPI(92);
62
+
63
+ const firstPage = await (await doc.getPageIterator()).current();
64
+ // C) Rasterize the first page in the document and save the result as PNG.
65
+ await draw.export(firstPage, outputPath + 'tiger_92dpi.png');
66
+
67
+ console.log('Example 1: tiger_92dpi.png');
68
+
69
+ // Export the same page as TIFF
70
+ await draw.export(firstPage, outputPath + 'tiger_92dpi.tif', 'TIFF');
71
+ } catch (err) {
72
+ console.log(err);
73
+ }
74
+
75
+ //--------------------------------------------------------------------------------
76
+ // Example 2) Convert the all pages in a given document to JPEG at 72 DPI.
77
+ console.log('Example 2:');
78
+ const hint_set = await PDFNet.ObjSet.create(); // A collection of rendering 'hits'.
79
+ try {
80
+ const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
81
+ // Initialize the security handler, in case the PDF is encrypted.
82
+ doc.initSecurityHandler();
83
+
84
+ draw.setDPI(72); // Set the output resolution is to 72 DPI.
85
+
86
+ // Use optional encoder parameter to specify JPEG quality.
87
+ const encoderParam = await hint_set.createDict();
88
+ await encoderParam.putNumber('Quality', 80);
89
+
90
+ // Traverse all pages in the document.
91
+ for (const itr = await doc.getPageIterator(); await itr.hasNext(); await itr.next()) {
92
+ const currPage = await itr.current();
93
+ const currPageIdx = await currPage.getIndex();
94
+ const path = outputPath + 'newsletter' + currPageIdx + '.jpg';
95
+ console.log('newsletter' + currPageIdx + '.jpg');
96
+
97
+ await draw.export(currPage, path, 'JPEG', encoderParam);
98
+ }
99
+ console.log('Done.');
100
+ } catch (err) {
101
+ console.log(err);
102
+ }
103
+
104
+ // Examples 3-6
105
+ try {
106
+ // Common code for remaining samples.
107
+ const tiger_doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'tiger.pdf');
108
+ // Initialize the security handler, in case the PDF is encrypted.
109
+ tiger_doc.initSecurityHandler();
110
+ const page = await tiger_doc.getPage(1);
111
+
112
+ //--------------------------------------------------------------------------------
113
+ // Example 3) Convert the first page to raw bitmap. Also, rotate the
114
+ // page 90 degrees and save the result as RAW.
115
+ draw.setDPI(100); // Set the output resolution is to 100 DPI.
116
+ draw.setRotate(PDFNet.Page.Rotate.e_90); // Rotate all pages 90 degrees clockwise.
117
+
118
+ const bitmapInfo = await draw.getBitmap(page, PDFNet.PDFDraw.PixelFormat.e_rgb, false);
119
+ const buf = Buffer.from(bitmapInfo.buf, 0, bitmapInfo.height * bitmapInfo.stride)
120
+
121
+ // Save the raw RGB data to disk.
122
+ fs.appendFileSync(outputPath + 'tiger_100dpi_rot90.raw', buf, 'binary');
123
+
124
+ console.log('Example 3: tiger_100dpi_rot90.raw');
125
+ draw.setRotate(PDFNet.Page.Rotate.e_0); // Disable image rotation for remaining samples.
126
+
127
+ //--------------------------------------------------------------------------------
128
+ // Example 4) Convert PDF page to a fixed image size. Also illustrates some
129
+ // other features in PDFDraw class such as rotation, image stretching, exporting
130
+ // to grayscale, or monochrome.
131
+
132
+ // Initialize render 'gray_hint' parameter, that is used to control the
133
+ // rendering process. In this case we tell the rasterizer to export the image as
134
+ // 1 Bit Per Component (BPC) image.
135
+ const mono_hint = await hint_set.createDict();
136
+ await mono_hint.putNumber('BPC', 1);
137
+
138
+ // SetImageSize can be used instead of SetDPI() to adjust page scaling
139
+ // dynamically so that given image fits into a buffer of given dimensions.
140
+ draw.setImageSize(1000, 1000); // Set the output image to be 1000 wide and 1000 pixels tall
141
+ draw.export(page, outputPath + 'tiger_1000x1000.png', 'PNG', mono_hint);
142
+ console.log('Example 4: tiger_1000x1000.png');
143
+
144
+ draw.setImageSize(200, 400); // Set the output image to be 200 wide and 300 pixels tall
145
+ draw.setRotate(PDFNet.Page.Rotate.e_180); // Rotate all pages 90 degrees clockwise.
146
+
147
+ // 'gray_hint' tells the rasterizer to export the image as grayscale.
148
+ const gray_hint = await hint_set.createDict();
149
+ await gray_hint.putName('ColorSpace', 'Gray');
150
+
151
+ await draw.export(page, outputPath + 'tiger_200x400_rot180.png', 'PNG', gray_hint);
152
+ console.log('Example 4: tiger_200x400_rot180.png');
153
+
154
+ draw.setImageSize(400, 200, false); // The third parameter sets 'preserve-aspect-ratio' to false.
155
+ draw.setRotate(PDFNet.Page.Rotate.e_0); // Disable image rotation.
156
+ await draw.export(page, outputPath + 'tiger_400x200_stretch.jpg', 'JPEG');
157
+ console.log('Example 4: tiger_400x200_stretch.jpg');
158
+
159
+ //--------------------------------------------------------------------------------
160
+ // Example 5) Zoom into a specific region of the page and rasterize the
161
+ // area at 200 DPI and as a thumbnail (i.e. a 50x50 pixel image).
162
+ const zoom_rect = await PDFNet.Rect.init(216, 522, 330, 600);
163
+ await page.setCropBox(zoom_rect); // Set the page crop box.
164
+
165
+ // Select the crop region to be used for drawing.
166
+ draw.setPageBox(PDFNet.Page.Box.e_crop);
167
+ draw.setDPI(900); // Set the output image resolution to 900 DPI.
168
+ await draw.export(page, outputPath + 'tiger_zoom_900dpi.png', 'PNG');
169
+ console.log('Example 5: tiger_zoom_900dpi.png');
170
+
171
+ // -------------------------------------------------------------------------------
172
+ // Example 6)
173
+ draw.setImageSize(50, 50); // Set the thumbnail to be 50x50 pixel image.
174
+ await draw.export(page, outputPath + 'tiger_zoom_50x50.png', 'PNG');
175
+ console.log('Example 6: tiger_zoom_50x50.png');
176
+ } catch (err) {
177
+ console.log(err);
178
+ }
179
+
180
+ //--------------------------------------------------------------------------------
181
+ // Example 7) Convert the first PDF page to CMYK TIFF at 92 DPI.
182
+ // A three step tutorial to convert PDF page to an image
183
+ try {
184
+ const cmyk_hint = await hint_set.createDict();
185
+ await cmyk_hint.putName('ColorSpace', 'CMYK');
186
+ // A) Open the PDF document.
187
+ const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'tiger.pdf');
188
+ // Initialize the security handler, in case the PDF is encrypted.
189
+ doc.initSecurityHandler();
190
+
191
+ // B) The output resolution is set to 92 DPI.
192
+ draw.setDPI(92);
193
+
194
+ // C) Rasterize the first page in the document and save the result as TIFF.
195
+ const pg = await doc.getPage(1);
196
+ await draw.export(pg, outputPath + 'out1.tif', 'TIFF', cmyk_hint);
197
+ console.log('Example 7: out1.tif');
198
+ } catch (err) {
199
+ console.log(err);
200
+ }
201
+
202
+ //--------------------------------------------------------------------------------
203
+ // Example 8) PDFRasterizer can be used for more complex rendering tasks, such as
204
+ // strip by strip or tiled document rendering. In particular, it is useful for
205
+ // cases where you cannot simply modify the page crop box (interactive viewing,
206
+ // parallel rendering). This example shows how you can rasterize the south-west
207
+ // quadrant of a page.
208
+ try {
209
+ // A) Open the PDF document.
210
+ const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'tiger.pdf');
211
+ // Initialize the security handler, in case the PDF is encrypted.
212
+ doc.initSecurityHandler();
213
+
214
+ // B) Get the page matrix
215
+ const pg = await doc.getPage(1);
216
+ const box = PDFNet.Page.Rotate.e_crop;
217
+ let mtx = await pg.getDefaultMatrix(true, box);
218
+ // We want to render a quadrant, so use half of width and height
219
+ const pg_w = await pg.getPageWidth(box) / 2;
220
+ const pg_h = await pg.getPageHeight(box) / 2;
221
+
222
+ // C) Scale matrix from PDF space to buffer space
223
+ const dpi = 96.0;
224
+ const scale = dpi / 72.0; // PDF space is 72 dpi
225
+ const buf_w = Math.floor(scale * pg_w);
226
+ const buf_h = Math.floor(scale * pg_h);
227
+ const bytes_per_pixel = 4; // BGRA buffer
228
+ await mtx.translate(0, -pg_h); // translate by '-pg_h' since we want south-west quadrant
229
+ const scale_mtx = await PDFNet.Matrix2D.create(scale, 0, 0, scale, 0, 0);
230
+ await scale_mtx.multiply(mtx);
231
+ mtx = scale_mtx;
232
+
233
+ // D) Rasterize page into memory buffer, according to our parameters
234
+ const rast = await PDFNet.PDFRasterizer.create();
235
+ const buf = await rast.rasterize(pg, buf_w, buf_h, buf_w * bytes_per_pixel, bytes_per_pixel, true, mtx);
236
+
237
+ // buf now contains raw BGRA bitmap.
238
+ console.log('Example 8: Successfully rasterized into memory buffer.');
239
+ } catch (err) {
240
+ console.log(err);
241
+ }
242
+
243
+ //--------------------------------------------------------------------------------
244
+ // Example 9) Export raster content to PNG using different image smoothing settings.
245
+ try {
246
+ const text_doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'lorem_ipsum.pdf');
247
+ text_doc.initSecurityHandler();
248
+ const itr = await text_doc.getPageIterator();
249
+ const page = await itr.current();
250
+
251
+ draw.setImageSmoothing(false, false);
252
+ let filename = 'raster_text_no_smoothing.png';
253
+ await draw.export(page, outputPath + filename);
254
+ console.log('Example 9 a): ' + filename + '. Done.');
255
+
256
+ filename = 'raster_text_smoothed.png';
257
+ draw.setImageSmoothing(true, false /*default quality bilinear resampling*/);
258
+ await draw.export(page, outputPath + filename);
259
+ console.log('Example 9 b): ' + filename + '. Done.');
260
+
261
+ filename = 'raster_text_high_quality.png';
262
+ draw.setImageSmoothing(true, true /*high quality area resampling*/);
263
+ await draw.export(page, outputPath + filename);
264
+ console.log('Example 9 c): ' + filename + '. Done.');
265
+ } catch (err) {
266
+ console.log(err);
267
+ }
268
+
269
+ //--------------------------------------------------------------------------------
270
+ // Example 10) Export separations directly, without conversion to an output colorspace
271
+ try {
272
+ const separation_doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'op_blend_test.pdf');
273
+ separation_doc.initSecurityHandler();
274
+ const separation_hint = await hint_set.createDict();
275
+ await separation_hint.putName('ColorSpace', 'Separation');
276
+ draw.setDPI(96);
277
+ draw.setImageSmoothing(true, true);
278
+ draw.setOverprint(PDFNet.PDFRasterizer.OverprintPreviewMode.e_op_on);
279
+
280
+ const itr = await separation_doc.getPageIterator();
281
+ const page = await itr.current();
282
+ let filename = 'merged_separations.png';
283
+ await draw.export(page, outputPath + filename, 'PNG');
284
+ console.log('Example 10 a): ' + filename + '. Done.');
285
+
286
+ filename = 'separation';
287
+ await draw.export(page, outputPath + filename, 'PNG', separation_hint);
288
+ console.log('Example 10 b): ' + filename + '_[ink].png. Done.');
289
+
290
+ filename = 'separation_NChannel.tif';
291
+ await draw.export(page, outputPath + filename, 'TIFF', separation_hint);
292
+ console.log('Example 10 c): ' + filename + '. Done.');
293
+ } catch (err) {
294
+ console.log(err);
295
+ }
296
+ } catch (err) {
297
+ console.log(err);
298
+ }
299
+ };
300
+
301
+ PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
302
+ };
303
+ exports.runPDFDrawTest();
304
+ })(exports);
305
+ // eslint-disable-next-line spaced-comment
306
306
  //# sourceURL=PDFDrawTest.js