@pdftron/pdfnet-node-samples 10.8.0-beta → 10.9.0-1-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 (135) hide show
  1. package/{samples/AddImageTest → AddImageTest}/AddImageTest.js +115 -115
  2. package/{samples/AdvancedImagingTest → AdvancedImagingTest}/AdvancedImagingTest.js +78 -78
  3. package/{samples/AnnotationTest → AnnotationTest}/AnnotationTest.js +641 -641
  4. package/{samples/BookmarkTest → BookmarkTest}/BookmarkTest.js +219 -219
  5. package/{samples/CAD2PDFTest → CAD2PDFTest}/CAD2PDFTest.js +79 -79
  6. package/{samples/ContentReplacerTest → ContentReplacerTest}/ContentReplacerTest.js +75 -75
  7. package/{samples/ConvertPrintTest → ConvertPrintTest}/ConvertPrintTest.js +153 -153
  8. package/{samples/ConvertTest → ConvertTest}/ConvertTest.js +203 -203
  9. package/{samples/DataExtractionTest → DataExtractionTest}/DataExtractionTest.js +214 -214
  10. package/{samples/DigitalSignaturesTest → DigitalSignaturesTest}/DigitalSignaturesTest.js +621 -526
  11. package/{samples/DocumentCreationTest → DocumentCreationTest}/DocumentCreationTest.js +409 -409
  12. package/{samples/ElementBuilderTest → ElementBuilderTest}/ElementBuilderTest.js +513 -513
  13. package/{samples/ElementEditTest → ElementEditTest}/ElementEditTest.js +110 -110
  14. package/{samples/ElementReaderAdvTest → ElementReaderAdvTest}/ElementReaderAdvTest.js +305 -305
  15. package/{samples/ElementReaderTest → ElementReaderTest}/ElementReaderTest.js +77 -77
  16. package/{samples/EncTest → EncTest}/EncTest.js +175 -175
  17. package/{samples/FDFTest → FDFTest}/FDFTest.js +218 -218
  18. package/{samples/HTML2PDFTest → HTML2PDFTest}/HTML2PDFTest.js +164 -164
  19. package/{samples/HighlightsTest → HighlightsTest}/HighlightsTest.js +97 -97
  20. package/{samples/ImageExtractTest → ImageExtractTest}/ImageExtractTest.js +129 -129
  21. package/{samples/ImpositionTest → ImpositionTest}/ImpositionTest.js +86 -86
  22. package/{samples/InteractiveFormsTest → InteractiveFormsTest}/InteractiveFormsTest.js +381 -381
  23. package/{samples/JBIG2Test → JBIG2Test}/JBIG2Test.js +88 -88
  24. package/{samples/LicenseKey → LicenseKey}/LicenseKey.js +11 -11
  25. package/{samples/LogicalStructureTest → LogicalStructureTest}/LogicalStructureTest.js +250 -250
  26. package/{samples/OCRTest → OCRTest}/OCRTest.js +235 -235
  27. package/{samples/OfficeTemplateTest → OfficeTemplateTest}/OfficeTemplateTest.js +79 -79
  28. package/{samples/OfficeToPDFTest → OfficeToPDFTest}/OfficeToPDFTest.js +125 -125
  29. package/{samples/OptimizerTest → OptimizerTest}/OptimizerTest.js +191 -191
  30. package/{samples/PDF2HtmlTest → PDF2HtmlTest}/PDF2HtmlTest.js +123 -123
  31. package/{samples/PDF2OfficeTest → PDF2OfficeTest}/PDF2OfficeTest.js +158 -158
  32. package/{samples/PDFATest → PDFATest}/PDFATest.js +85 -85
  33. package/{samples/PDFDocMemoryTest → PDFDocMemoryTest}/PDFDocMemoryTest.js +84 -84
  34. package/{samples/PDFDrawTest → PDFDrawTest}/PDFDrawTest.js +305 -305
  35. package/{samples/PDFLayersTest → PDFLayersTest}/PDFLayersTest.js +294 -294
  36. package/{samples/PDFPackageTest → PDFPackageTest}/PDFPackageTest.js +111 -111
  37. package/{samples/PDFPageTest → PDFPageTest}/PDFPageTest.js +189 -189
  38. package/{samples/PDFRedactTest → PDFRedactTest}/PDFRedactTest.js +74 -74
  39. package/{samples/PageLabelsTest → PageLabelsTest}/PageLabelsTest.js +138 -138
  40. package/{samples/PatternTest → PatternTest}/PatternTest.js +226 -226
  41. package/{samples/RectTest → RectTest}/RectTest.js +40 -40
  42. package/{samples/SDFTest → SDFTest}/SDFTest.js +87 -87
  43. package/{samples/StamperTest → StamperTest}/StamperTest.js +255 -255
  44. package/{samples/TestFiles → TestFiles}/Misc-Fixed.pfa +1166 -1166
  45. package/{samples/TestFiles → TestFiles}/SHA-2 Root USERTrust RSA CA Sectigo timestamping.crt +34 -34
  46. package/{samples/TestFiles → TestFiles}/form1_annots.xfdf +33 -33
  47. package/{samples/TestFiles → TestFiles}/form1_data.xfdf +139 -139
  48. package/{samples/TestFiles → TestFiles}/my_stream.txt +2310 -2310
  49. package/{samples/TestFiles → TestFiles}/tiger.svg +378 -378
  50. package/{samples/TextExtractTest → TextExtractTest}/TextExtractTest.js +286 -286
  51. package/{samples/TextSearchTest → TextSearchTest}/TextSearchTest.js +121 -121
  52. package/{samples/U3DTest → U3DTest}/U3DTest.js +104 -104
  53. package/{samples/UndoRedoTest → UndoRedoTest}/UndoRedoTest.js +101 -101
  54. package/{samples/UnicodeWriteTest → UnicodeWriteTest}/UnicodeWriteTest.js +173 -173
  55. package/{samples/WebViewerConvertTest → WebViewerConvertTest}/WebViewerConvertTest.js +135 -135
  56. package/legal.txt +632 -0
  57. package/license.pdf +0 -0
  58. package/package.json +20 -20
  59. package/readme.md +38 -13
  60. package/{samples/runall.bat → runall.bat} +12 -12
  61. package/{samples/runall.sh → runall.sh} +15 -15
  62. /package/{samples/TestFiles → TestFiles}/BusinessCardTemplate.pdf +0 -0
  63. /package/{samples/TestFiles → TestFiles}/Fishermen.docx +0 -0
  64. /package/{samples/TestFiles → TestFiles}/Font_licenses.txt +0 -0
  65. /package/{samples/TestFiles → TestFiles}/GlobalSignRootForTST.cer +0 -0
  66. /package/{samples/TestFiles → TestFiles}/License.txt +0 -0
  67. /package/{samples/TestFiles → TestFiles}/NotoSans_with_hindi.ttf +0 -0
  68. /package/{samples/TestFiles → TestFiles}/Output/empty +0 -0
  69. /package/{samples/TestFiles → TestFiles}/SYH_Letter.docx +0 -0
  70. /package/{samples/TestFiles → TestFiles}/TigerText.pdf +0 -0
  71. /package/{samples/TestFiles → TestFiles}/US061222892-a.pdf +0 -0
  72. /package/{samples/TestFiles → TestFiles}/butterfly.png +0 -0
  73. /package/{samples/TestFiles → TestFiles}/credit card numbers.pdf +0 -0
  74. /package/{samples/TestFiles → TestFiles}/dice.jpg +0 -0
  75. /package/{samples/TestFiles → TestFiles}/dice.u3d +0 -0
  76. /package/{samples/TestFiles → TestFiles}/doc_to_sign.pdf +0 -0
  77. /package/{samples/TestFiles → TestFiles}/factsheet_Arabic.docx +0 -0
  78. /package/{samples/TestFiles → TestFiles}/financial.pdf +0 -0
  79. /package/{samples/TestFiles → TestFiles}/fish.pdf +0 -0
  80. /package/{samples/TestFiles → TestFiles}/font.ttf +0 -0
  81. /package/{samples/TestFiles → TestFiles}/form1.pdf +0 -0
  82. /package/{samples/TestFiles → TestFiles}/form1_data.fdf +0 -0
  83. /package/{samples/TestFiles → TestFiles}/formfields-scanned-withfields.pdf +0 -0
  84. /package/{samples/TestFiles → TestFiles}/formfields-scanned.pdf +0 -0
  85. /package/{samples/TestFiles → TestFiles}/formfields.pdf +0 -0
  86. /package/{samples/TestFiles → TestFiles}/grayscale.tif +0 -0
  87. /package/{samples/TestFiles → TestFiles}/hindi_sample_utf16le.txt +0 -0
  88. /package/{samples/TestFiles → TestFiles}/imagemask.dat +0 -0
  89. /package/{samples/TestFiles → TestFiles}/logo_red.png +0 -0
  90. /package/{samples/TestFiles → TestFiles}/lorem_ipsum.pdf +0 -0
  91. /package/{samples/TestFiles → TestFiles}/multipage.tif +0 -0
  92. /package/{samples/TestFiles → TestFiles}/newsletter.pdf +0 -0
  93. /package/{samples/TestFiles → TestFiles}/newsletter.xod +0 -0
  94. /package/{samples/TestFiles → TestFiles}/numbered.pdf +0 -0
  95. /package/{samples/TestFiles → TestFiles}/op_blend_test.pdf +0 -0
  96. /package/{samples/TestFiles → TestFiles}/palm.jp2 +0 -0
  97. /package/{samples/TestFiles → TestFiles}/paragraphs_and_tables.pdf +0 -0
  98. /package/{samples/TestFiles → TestFiles}/pdfnet.gif +0 -0
  99. /package/{samples/TestFiles → TestFiles}/pdftron.bmp +0 -0
  100. /package/{samples/TestFiles → TestFiles}/pdftron.cer +0 -0
  101. /package/{samples/TestFiles → TestFiles}/pdftron.pfx +0 -0
  102. /package/{samples/TestFiles → TestFiles}/pdftron_smart_substitution.plugin +0 -0
  103. /package/{samples/TestFiles → TestFiles}/peppers.jpg +0 -0
  104. /package/{samples/TestFiles → TestFiles}/signature.jpg +0 -0
  105. /package/{samples/TestFiles → TestFiles}/simple-emf.emf +0 -0
  106. /package/{samples/TestFiles → TestFiles}/simple-excel_2007.xlsx +0 -0
  107. /package/{samples/TestFiles → TestFiles}/simple-outlook.msg +0 -0
  108. /package/{samples/TestFiles → TestFiles}/simple-powerpoint_2007.pptx +0 -0
  109. /package/{samples/TestFiles → TestFiles}/simple-publisher.pub +0 -0
  110. /package/{samples/TestFiles → TestFiles}/simple-rtf.rtf +0 -0
  111. /package/{samples/TestFiles → TestFiles}/simple-text.txt +0 -0
  112. /package/{samples/TestFiles → TestFiles}/simple-visio.vsd +0 -0
  113. /package/{samples/TestFiles → TestFiles}/simple-webpage.html +0 -0
  114. /package/{samples/TestFiles → TestFiles}/simple-webpage.mht +0 -0
  115. /package/{samples/TestFiles → TestFiles}/simple-webpage_files/colorschememapping.xml +0 -0
  116. /package/{samples/TestFiles → TestFiles}/simple-webpage_files/filelist.xml +0 -0
  117. /package/{samples/TestFiles → TestFiles}/simple-webpage_files/image001.gif +0 -0
  118. /package/{samples/TestFiles → TestFiles}/simple-webpage_files/image002.png +0 -0
  119. /package/{samples/TestFiles → TestFiles}/simple-webpage_files/image003.jpg +0 -0
  120. /package/{samples/TestFiles → TestFiles}/simple-webpage_files/image004.emz +0 -0
  121. /package/{samples/TestFiles → TestFiles}/simple-webpage_files/image005.gif +0 -0
  122. /package/{samples/TestFiles → TestFiles}/simple-webpage_files/image006.png +0 -0
  123. /package/{samples/TestFiles → TestFiles}/simple-webpage_files/image007.gif +0 -0
  124. /package/{samples/TestFiles → TestFiles}/simple-webpage_files/oledata.mso +0 -0
  125. /package/{samples/TestFiles → TestFiles}/simple-webpage_files/themedata.thmx +0 -0
  126. /package/{samples/TestFiles → TestFiles}/simple-word_2007.docx +0 -0
  127. /package/{samples/TestFiles → TestFiles}/simple-xps.xps +0 -0
  128. /package/{samples/TestFiles → TestFiles}/table.pdf +0 -0
  129. /package/{samples/TestFiles → TestFiles}/tagged.pdf +0 -0
  130. /package/{samples/TestFiles → TestFiles}/the_rime_of_the_ancient_mariner.docx +0 -0
  131. /package/{samples/TestFiles → TestFiles}/tiger.pdf +0 -0
  132. /package/{samples/TestFiles → TestFiles}/waiver.pdf +0 -0
  133. /package/{samples/TestFiles → TestFiles}/waiver_withApprovalField.pdf +0 -0
  134. /package/{samples/TestFiles → TestFiles}/waiver_withApprovalField_certified.pdf +0 -0
  135. /package/{samples/TestFiles → TestFiles}/waiver_withApprovalField_certified_approved.pdf +0 -0
@@ -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