@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,165 +1,165 @@
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 HTML pages to PDF format using
8
- // the HTML2PDF class.
9
- //
10
- // 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be
11
- // used to convert HTML web pages into PDF documents by using an external module (html2pdf).
12
- //
13
- // html2pdf modules can be downloaded from http://www.pdftron.com/pdfnet/downloads.html.
14
- //
15
- // Users can convert HTML pages to PDF using the following operations:
16
- // - Simple one line static method to convert a single web page to PDF.
17
- // - Convert HTML pages from URL or string, plus optional table of contents, in user defined order.
18
- // - Optionally configure settings for proxy, images, java script, and more for each HTML page.
19
- // - Optionally configure the PDF output, including page size, margins, orientation, and more.
20
- // - Optionally add table of contents, including setting the depth and appearance.
21
- //---------------------------------------------------------------------------------------
22
-
23
- const { PDFNet } = require('@pdftron/pdfnet-node');
24
- const PDFTronLicense = require('../LicenseKey/LicenseKey');
25
-
26
- ((exports) => {
27
- 'use strict';
28
-
29
- exports.runHTML2PDFTest = () => {
30
- const main = async () => {
31
- const outputPath = '../TestFiles/Output/html2pdf_example';
32
- const host = 'https://docs.apryse.com';
33
- const page0 = '/';
34
- const page1 = '/all-products/';
35
- const page2 = '/documentation/web/faq';
36
-
37
- // For HTML2PDF we need to locate the html2pdf module. If placed with the
38
- // PDFNet library, or in the current working directory, it will be loaded
39
- // automatically. Otherwise, it must be set manually using HTML2PDF.setModulePath.
40
- await PDFNet.HTML2PDF.setModulePath('../../lib/');
41
-
42
- if(!(await PDFNet.HTML2PDF.isModuleAvailable())) {
43
- console.log('Unable to run HTML2PDFTest: Apryse SDK HTML2PDF module not available.');
44
- console.log('---------------------------------------------------------------');
45
- console.log('The HTML2PDF module is an optional add-on, available for download');
46
- console.log('at https://www.pdftron.com/. If you have already downloaded this');
47
- console.log('module, ensure that the SDK is able to find the required files');
48
- console.log('using the HTML2PDF.setModulePath() function.');
49
-
50
- return;
51
- }
52
-
53
- //--------------------------------------------------------------------------------
54
- // Example 1) Simple conversion of a web page to a PDF doc.
55
-
56
- try {
57
- const html2pdf = await PDFNet.HTML2PDF.create();
58
- const doc = await PDFNet.PDFDoc.create();
59
-
60
- html2pdf.insertFromUrl(host.concat(page0));
61
- // now convert a web page, sending generated PDF pages to doc
62
- await html2pdf.convert(doc);
63
- doc.save(outputPath.concat('_01.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
64
- } catch (err) {
65
- console.log(err);
66
- }
67
-
68
- //--------------------------------------------------------------------------------
69
- // Example 2) Modify the settings of the generated PDF pages and attach to an
70
- // existing PDF document.
71
-
72
- try {
73
- // open the existing PDF, and initialize the security handler
74
- const doc = await PDFNet.PDFDoc.createFromFilePath('../TestFiles/numbered.pdf');
75
- await doc.initSecurityHandler();
76
-
77
- // create the HTML2PDF converter object and modify the output of the PDF pages
78
- const html2pdf = await PDFNet.HTML2PDF.create();
79
- html2pdf.setPaperSize(PDFNet.PrinterMode.PaperSize.e_11x17);
80
-
81
- // insert the web page to convert
82
- html2pdf.insertFromUrl(host.concat(page0));
83
-
84
- // convert the web page, appending generated PDF pages to doc
85
- await html2pdf.convert(doc);
86
- doc.save(outputPath.concat('_02.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
87
- } catch (err) {
88
- console.log(err);
89
- }
90
-
91
- //--------------------------------------------------------------------------------
92
- // Example 3) Convert multiple web pages
93
-
94
- try {
95
- // convert page 0 into pdf
96
- const doc = await PDFNet.PDFDoc.create();
97
-
98
- const converter = await PDFNet.HTML2PDF.create();
99
-
100
- const header = '<div style=\'width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF\'><span class=\'date\'></span></div><div style=\'width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF\'><span>PDFTRON HEADER EXAMPLE</span></div><div style=\'width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF\'><span class=\'pageNumber\'></span> of <span class=\'totalPages\'></span></div>';
101
- const footer = '<div style=\'width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF\'><span class=\'date\'></span></div><div style=\'width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF\'><span>PDFTRON FOOTER EXAMPLE</span></div><div style=\'width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF\'><span class=\'pageNumber\'></span> of <span class=\'totalPages\'></span></div>';
102
- converter.setHeader(header);
103
- converter.setFooter(footer);
104
- converter.setMargins('1cm', '2cm', '.5cm', '1.5cm');
105
- const settings = await PDFNet.HTML2PDF.WebPageSettings.create();
106
- await settings.setZoom(0.5);
107
- converter.insertFromUrl2(host.concat(page0), settings);
108
- await converter.convert(doc);
109
-
110
- // convert page 1 with the same settings, appending generated PDF pages to doc
111
- converter.insertFromUrl2(host.concat(page1), settings);
112
- await converter.convert(doc);
113
-
114
- // convert page 2 with different settings, appending generated PDF pages to doc
115
- const another_converter = await PDFNet.HTML2PDF.create();
116
- another_converter.setLandscape(true);
117
- const another_settings = await PDFNet.HTML2PDF.WebPageSettings.create();
118
- another_settings.setPrintBackground(false);
119
- another_converter.insertFromUrl2(host.concat(page2), another_settings);
120
- await another_converter.convert(doc);
121
-
122
- doc.save(outputPath.concat('_03.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
123
- } catch (err) {
124
- console.log(err);
125
- }
126
-
127
- //--------------------------------------------------------------------------------
128
- // Example 4) Convert HTML string to PDF.
129
-
130
- try {
131
- const html2pdf = await PDFNet.HTML2PDF.create();
132
- const doc = await PDFNet.PDFDoc.create();
133
- const html = '<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>';
134
-
135
- html2pdf.insertFromHtmlString(html);
136
- await html2pdf.convert(doc);
137
- doc.save(outputPath.concat('_04.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
138
- } catch (err) {
139
- console.log(err);
140
- }
141
-
142
- //--------------------------------------------------------------------------------
143
- // Example 5) Set the location of the log file to be used during conversion.
144
-
145
- try {
146
- const html2pdf = await PDFNet.HTML2PDF.create();
147
- const doc = await PDFNet.PDFDoc.create();
148
- html2pdf.setLogFilePath('../TestFiles/Output/html2pdf.log');
149
- html2pdf.insertFromUrl(host.concat(page0));
150
- await html2pdf.convert(doc);
151
- doc.save(outputPath.concat('_05.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
152
- } catch (err) {
153
- console.log(err);
154
- }
155
-
156
- console.log('Test Complete!');
157
- }
158
- PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error) {
159
- console.log('Error: ' + JSON.stringify(error));
160
- }).then(function(){ return PDFNet.shutdown(); });
161
- };
162
- exports.runHTML2PDFTest();
163
- })(exports);
164
- // 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 HTML pages to PDF format using
8
+ // the HTML2PDF class.
9
+ //
10
+ // 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be
11
+ // used to convert HTML web pages into PDF documents by using an external module (html2pdf).
12
+ //
13
+ // html2pdf modules can be downloaded from http://www.pdftron.com/pdfnet/downloads.html.
14
+ //
15
+ // Users can convert HTML pages to PDF using the following operations:
16
+ // - Simple one line static method to convert a single web page to PDF.
17
+ // - Convert HTML pages from URL or string, plus optional table of contents, in user defined order.
18
+ // - Optionally configure settings for proxy, images, java script, and more for each HTML page.
19
+ // - Optionally configure the PDF output, including page size, margins, orientation, and more.
20
+ // - Optionally add table of contents, including setting the depth and appearance.
21
+ //---------------------------------------------------------------------------------------
22
+
23
+ const { PDFNet } = require('@pdftron/pdfnet-node');
24
+ const PDFTronLicense = require('../LicenseKey/LicenseKey');
25
+
26
+ ((exports) => {
27
+ 'use strict';
28
+
29
+ exports.runHTML2PDFTest = () => {
30
+ const main = async () => {
31
+ const outputPath = '../TestFiles/Output/html2pdf_example';
32
+ const host = 'https://docs.apryse.com';
33
+ const page0 = '/';
34
+ const page1 = '/all-products/';
35
+ const page2 = '/documentation/web/faq';
36
+
37
+ // For HTML2PDF we need to locate the html2pdf module. If placed with the
38
+ // PDFNet library, or in the current working directory, it will be loaded
39
+ // automatically. Otherwise, it must be set manually using HTML2PDF.setModulePath.
40
+ await PDFNet.HTML2PDF.setModulePath('../../lib/');
41
+
42
+ if(!(await PDFNet.HTML2PDF.isModuleAvailable())) {
43
+ console.log('Unable to run HTML2PDFTest: Apryse SDK HTML2PDF module not available.');
44
+ console.log('---------------------------------------------------------------');
45
+ console.log('The HTML2PDF module is an optional add-on, available for download');
46
+ console.log('at https://www.pdftron.com/. If you have already downloaded this');
47
+ console.log('module, ensure that the SDK is able to find the required files');
48
+ console.log('using the HTML2PDF.setModulePath() function.');
49
+
50
+ return;
51
+ }
52
+
53
+ //--------------------------------------------------------------------------------
54
+ // Example 1) Simple conversion of a web page to a PDF doc.
55
+
56
+ try {
57
+ const html2pdf = await PDFNet.HTML2PDF.create();
58
+ const doc = await PDFNet.PDFDoc.create();
59
+
60
+ html2pdf.insertFromUrl(host.concat(page0));
61
+ // now convert a web page, sending generated PDF pages to doc
62
+ await html2pdf.convert(doc);
63
+ doc.save(outputPath.concat('_01.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
64
+ } catch (err) {
65
+ console.log(err);
66
+ }
67
+
68
+ //--------------------------------------------------------------------------------
69
+ // Example 2) Modify the settings of the generated PDF pages and attach to an
70
+ // existing PDF document.
71
+
72
+ try {
73
+ // open the existing PDF, and initialize the security handler
74
+ const doc = await PDFNet.PDFDoc.createFromFilePath('../TestFiles/numbered.pdf');
75
+ await doc.initSecurityHandler();
76
+
77
+ // create the HTML2PDF converter object and modify the output of the PDF pages
78
+ const html2pdf = await PDFNet.HTML2PDF.create();
79
+ html2pdf.setPaperSize(PDFNet.PrinterMode.PaperSize.e_11x17);
80
+
81
+ // insert the web page to convert
82
+ html2pdf.insertFromUrl(host.concat(page0));
83
+
84
+ // convert the web page, appending generated PDF pages to doc
85
+ await html2pdf.convert(doc);
86
+ doc.save(outputPath.concat('_02.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
87
+ } catch (err) {
88
+ console.log(err);
89
+ }
90
+
91
+ //--------------------------------------------------------------------------------
92
+ // Example 3) Convert multiple web pages
93
+
94
+ try {
95
+ // convert page 0 into pdf
96
+ const doc = await PDFNet.PDFDoc.create();
97
+
98
+ const converter = await PDFNet.HTML2PDF.create();
99
+
100
+ const header = '<div style=\'width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF\'><span class=\'date\'></span></div><div style=\'width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF\'><span>PDFTRON HEADER EXAMPLE</span></div><div style=\'width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF\'><span class=\'pageNumber\'></span> of <span class=\'totalPages\'></span></div>';
101
+ const footer = '<div style=\'width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF\'><span class=\'date\'></span></div><div style=\'width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF\'><span>PDFTRON FOOTER EXAMPLE</span></div><div style=\'width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF\'><span class=\'pageNumber\'></span> of <span class=\'totalPages\'></span></div>';
102
+ converter.setHeader(header);
103
+ converter.setFooter(footer);
104
+ converter.setMargins('1cm', '2cm', '.5cm', '1.5cm');
105
+ const settings = await PDFNet.HTML2PDF.WebPageSettings.create();
106
+ await settings.setZoom(0.5);
107
+ converter.insertFromUrl2(host.concat(page0), settings);
108
+ await converter.convert(doc);
109
+
110
+ // convert page 1 with the same settings, appending generated PDF pages to doc
111
+ converter.insertFromUrl2(host.concat(page1), settings);
112
+ await converter.convert(doc);
113
+
114
+ // convert page 2 with different settings, appending generated PDF pages to doc
115
+ const another_converter = await PDFNet.HTML2PDF.create();
116
+ another_converter.setLandscape(true);
117
+ const another_settings = await PDFNet.HTML2PDF.WebPageSettings.create();
118
+ another_settings.setPrintBackground(false);
119
+ another_converter.insertFromUrl2(host.concat(page2), another_settings);
120
+ await another_converter.convert(doc);
121
+
122
+ doc.save(outputPath.concat('_03.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
123
+ } catch (err) {
124
+ console.log(err);
125
+ }
126
+
127
+ //--------------------------------------------------------------------------------
128
+ // Example 4) Convert HTML string to PDF.
129
+
130
+ try {
131
+ const html2pdf = await PDFNet.HTML2PDF.create();
132
+ const doc = await PDFNet.PDFDoc.create();
133
+ const html = '<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>';
134
+
135
+ html2pdf.insertFromHtmlString(html);
136
+ await html2pdf.convert(doc);
137
+ doc.save(outputPath.concat('_04.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
138
+ } catch (err) {
139
+ console.log(err);
140
+ }
141
+
142
+ //--------------------------------------------------------------------------------
143
+ // Example 5) Set the location of the log file to be used during conversion.
144
+
145
+ try {
146
+ const html2pdf = await PDFNet.HTML2PDF.create();
147
+ const doc = await PDFNet.PDFDoc.create();
148
+ html2pdf.setLogFilePath('../TestFiles/Output/html2pdf.log');
149
+ html2pdf.insertFromUrl(host.concat(page0));
150
+ await html2pdf.convert(doc);
151
+ doc.save(outputPath.concat('_05.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
152
+ } catch (err) {
153
+ console.log(err);
154
+ }
155
+
156
+ console.log('Test Complete!');
157
+ }
158
+ PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error) {
159
+ console.log('Error: ' + JSON.stringify(error));
160
+ }).then(function(){ return PDFNet.shutdown(); });
161
+ };
162
+ exports.runHTML2PDFTest();
163
+ })(exports);
164
+ // eslint-disable-next-line spaced-comment
165
165
  //# sourceURL=HTML2PDFTest.js
@@ -1,97 +1,97 @@
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
- const { PDFNet } = require('@pdftron/pdfnet-node');
8
- const PDFTronLicense = require('../LicenseKey/LicenseKey');
9
-
10
- // This sample illustrates the basic text highlight capabilities of PDFNet.
11
- // It simulates a full-text search engine that finds all occurrences of the word 'Federal'.
12
- // It then highlights those words on the page.
13
- //
14
- // Note: The TextSearch class is the preferred solution for searching text within a single
15
- // PDF file. TextExtractor provides search highlighting capabilities where a large number
16
- // of documents are indexed using a 3rd party search engine.
17
- ((exports) => {
18
-
19
- exports.runHighlightsTest = async () => {
20
- const main = async () => {
21
- // eslint-disable-next-line no-unused-vars
22
- let ret = 0;
23
-
24
- // Relative path to the folder containing test files.
25
- const inputPath = '../TestFiles/';
26
- const outputPath = '../TestFiles/Output/';
27
- const inputFilename = 'paragraphs_and_tables.pdf';
28
-
29
- try {
30
- await PDFNet.startDeallocateStack();
31
- const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + inputFilename);
32
- doc.initSecurityHandler();
33
-
34
- const page = await doc.getPage(1);
35
-
36
- if (page.id === '0') {
37
- console.log('Page not found.');
38
- return 1;
39
- }
40
-
41
- const txt = await PDFNet.TextExtractor.create();
42
- txt.begin(page);
43
-
44
- // Do not dehyphenate; that would interfere with character offsets
45
- const dehyphen = false;
46
- // Retrieve the page text
47
- const pageText = await txt.getAsText(dehyphen);
48
-
49
- // Simulating a full-text search engine that finds all occurrences of the word 'Federal'.
50
- // In a real application, plug in your own search engine here.
51
- const searchText = 'Federal';
52
- let charRanges = [];
53
- let ofs = pageText.indexOf(searchText);
54
- while (ofs >= 0) {
55
- const range = { index: ofs, length: searchText.length };
56
- charRanges.push(range); // character offset + length
57
- ofs = pageText.indexOf(searchText, ofs + 1);
58
- }
59
-
60
- // Retrieve Highlights object and apply annotations to the page
61
- const hlts = await txt.getHighlights(charRanges);
62
- await hlts.begin(doc);
63
- while ((await hlts.hasNext())) {
64
- const curPage = await doc.getPage((await hlts.getCurrentPageNumber()));
65
- const quadArr = await hlts.getCurrentQuads();
66
- for (let i = 0; i < quadArr.length; ++i) {
67
- const currQuad = quadArr[i];
68
- const x1 = Math.min(Math.min(Math.min(currQuad.p1x, currQuad.p2x), currQuad.p3x), currQuad.p4x);
69
- const x2 = Math.max(Math.max(Math.max(currQuad.p1x, currQuad.p2x), currQuad.p3x), currQuad.p4x);
70
- const y1 = Math.min(Math.min(Math.min(currQuad.p1y, currQuad.p2y), currQuad.p3y), currQuad.p4y);
71
- const y2 = Math.max(Math.max(Math.max(currQuad.p1y, currQuad.p2y), currQuad.p3y), currQuad.p4y);
72
-
73
- const highlight = await PDFNet.HighlightAnnot.create(doc, (await PDFNet.Rect.init(x1, y1, x2, y2)));
74
- await highlight.refreshAppearance();
75
- await curPage.annotPushBack(highlight);
76
-
77
- console.log('[' + x1.toFixed(2) + ', ' + y1.toFixed(2) + ', ' + x2.toFixed(2) + ', ' + y2.toFixed(2) + ']');
78
- }
79
- hlts.next();
80
- }
81
-
82
- // Output highlighted PDF doc
83
- await doc.save(outputPath + 'search_highlights.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
84
-
85
- await PDFNet.endDeallocateStack();
86
- } catch (err) {
87
- console.log(err);
88
- console.log(err.stack);
89
- ret = 1;
90
- }
91
- };
92
- PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
93
- };
94
- exports.runHighlightsTest();
95
- })(exports);
96
- // eslint-disable-next-line spaced-comment
97
- //# sourceURL=HighlightsTest.js
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
+ const { PDFNet } = require('@pdftron/pdfnet-node');
8
+ const PDFTronLicense = require('../LicenseKey/LicenseKey');
9
+
10
+ // This sample illustrates the basic text highlight capabilities of PDFNet.
11
+ // It simulates a full-text search engine that finds all occurrences of the word 'Federal'.
12
+ // It then highlights those words on the page.
13
+ //
14
+ // Note: The TextSearch class is the preferred solution for searching text within a single
15
+ // PDF file. TextExtractor provides search highlighting capabilities where a large number
16
+ // of documents are indexed using a 3rd party search engine.
17
+ ((exports) => {
18
+
19
+ exports.runHighlightsTest = async () => {
20
+ const main = async () => {
21
+ // eslint-disable-next-line no-unused-vars
22
+ let ret = 0;
23
+
24
+ // Relative path to the folder containing test files.
25
+ const inputPath = '../TestFiles/';
26
+ const outputPath = '../TestFiles/Output/';
27
+ const inputFilename = 'paragraphs_and_tables.pdf';
28
+
29
+ try {
30
+ await PDFNet.startDeallocateStack();
31
+ const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + inputFilename);
32
+ doc.initSecurityHandler();
33
+
34
+ const page = await doc.getPage(1);
35
+
36
+ if (page.id === '0') {
37
+ console.log('Page not found.');
38
+ return 1;
39
+ }
40
+
41
+ const txt = await PDFNet.TextExtractor.create();
42
+ txt.begin(page);
43
+
44
+ // Do not dehyphenate; that would interfere with character offsets
45
+ const dehyphen = false;
46
+ // Retrieve the page text
47
+ const pageText = await txt.getAsText(dehyphen);
48
+
49
+ // Simulating a full-text search engine that finds all occurrences of the word 'Federal'.
50
+ // In a real application, plug in your own search engine here.
51
+ const searchText = 'Federal';
52
+ let charRanges = [];
53
+ let ofs = pageText.indexOf(searchText);
54
+ while (ofs >= 0) {
55
+ const range = { index: ofs, length: searchText.length };
56
+ charRanges.push(range); // character offset + length
57
+ ofs = pageText.indexOf(searchText, ofs + 1);
58
+ }
59
+
60
+ // Retrieve Highlights object and apply annotations to the page
61
+ const hlts = await txt.getHighlights(charRanges);
62
+ await hlts.begin(doc);
63
+ while ((await hlts.hasNext())) {
64
+ const curPage = await doc.getPage((await hlts.getCurrentPageNumber()));
65
+ const quadArr = await hlts.getCurrentQuads();
66
+ for (let i = 0; i < quadArr.length; ++i) {
67
+ const currQuad = quadArr[i];
68
+ const x1 = Math.min(Math.min(Math.min(currQuad.p1x, currQuad.p2x), currQuad.p3x), currQuad.p4x);
69
+ const x2 = Math.max(Math.max(Math.max(currQuad.p1x, currQuad.p2x), currQuad.p3x), currQuad.p4x);
70
+ const y1 = Math.min(Math.min(Math.min(currQuad.p1y, currQuad.p2y), currQuad.p3y), currQuad.p4y);
71
+ const y2 = Math.max(Math.max(Math.max(currQuad.p1y, currQuad.p2y), currQuad.p3y), currQuad.p4y);
72
+
73
+ const highlight = await PDFNet.HighlightAnnot.create(doc, (await PDFNet.Rect.init(x1, y1, x2, y2)));
74
+ await highlight.refreshAppearance();
75
+ await curPage.annotPushBack(highlight);
76
+
77
+ console.log('[' + x1.toFixed(2) + ', ' + y1.toFixed(2) + ', ' + x2.toFixed(2) + ', ' + y2.toFixed(2) + ']');
78
+ }
79
+ hlts.next();
80
+ }
81
+
82
+ // Output highlighted PDF doc
83
+ await doc.save(outputPath + 'search_highlights.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
84
+
85
+ await PDFNet.endDeallocateStack();
86
+ } catch (err) {
87
+ console.log(err);
88
+ console.log(err.stack);
89
+ ret = 1;
90
+ }
91
+ };
92
+ PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
93
+ };
94
+ exports.runHighlightsTest();
95
+ })(exports);
96
+ // eslint-disable-next-line spaced-comment
97
+ //# sourceURL=HighlightsTest.js