1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6(exports => {
7 exports.runElementReaderTest = () => {
8 const PDFNet = exports.Core.PDFNet;
9
10 const ProcessElements = async reader => {
11 // Read page contents
12 for (let element = await reader.next(); element !== null; element = await reader.next()) {
13 const temp = await element.getType();
14 switch (temp) {
15 case PDFNet.Element.Type.e_path: // Process path data...
16 {
17 const data = await element.getPathData();
18 /* eslint-disable @typescript-eslint/no-unused-vars */
19 const operators = data.operators;
20 const points = data.points;
21 /* eslint-enable @typescript-eslint/no-unused-vars */
22 }
23 break;
24 case PDFNet.Element.Type.e_text: // Process text strings...
25 {
26 const data = await element.getTextString();
27 console.log(data);
28 }
29 break;
30 case PDFNet.Element.Type.e_form: // Process form XObjects
31 reader.formBegin();
32 await ProcessElements(reader);
33 reader.end();
34 break;
35 default:
36 }
37 }
38 };
39
40 const main = async () => {
41 console.log('-------------------------------------------------');
42 console.log('Sample 1 - Extract text data from all pages in the document.');
43 console.log('Opening the input pdf...');
44 const ret = 0;
45
46 // Relative path to the folder containing test files.
47 const inputUrl = '../TestFiles/';
48
49 const doc = await PDFNet.PDFDoc.createFromURL(inputUrl + 'newsletter.pdf'); // await if there is ret that we care about.
50 doc.initSecurityHandler();
51 doc.lock();
52
53 // eslint-disable-next-line @typescript-eslint/no-unused-vars
54 const pgnum = await doc.getPageCount();
55 const pageReader = await PDFNet.ElementReader.create();
56 const itr = await doc.getPageIterator(1);
57
58 // Read every page
59 for (itr; await itr.hasNext(); itr.next()) {
60 const curritr = await itr.current();
61 pageReader.beginOnPage(curritr);
62 await ProcessElements(pageReader);
63 pageReader.end();
64 }
65
66 console.log('Done.');
67 return ret;
68 };
69
70 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
71 PDFNet.runWithCleanup(main);
72 };
73})(window);
74// eslint-disable-next-line spaced-comment
75//# sourceURL=ElementReaderTest.js