//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------
// The Data Extraction suite is an optional PDFNet add-on collection that can be used to
// extract various types of data from PDF documents.
//
// The Apryse SDK Data Extraction suite can be downloaded from http://www.pdftron.com/
//---------------------------------------------------------------------------------------

const fs = require('fs');
const { PDFNet } = require('@pdftron/pdfnet-node');
const PDFTronLicense = require('../LicenseKey/LicenseKey');

((exports) => {
	'use strict';

	exports.runDataExtractionTest = () => {

		const main = async () => {

			const inputPath = '../TestFiles/';
			const outputPath = '../TestFiles/Output/';

			//////////////////////////////////////////////////////////////////////////

			await PDFNet.addResourceSearchPath('../../lib/');

			//////////////////////////////////////////////////////////////////////////
			// The following sample illustrates how to extract tables from PDF documents.
			//////////////////////////////////////////////////////////////////////////

			// Test if the add-on is installed
			if (!await PDFNet.DataExtractionModule.isModuleAvailable(PDFNet.DataExtractionModule.DataExtractionEngine.e_Tabular)) {
				console.log('\nUnable to run Data Extraction: Apryse SDK Tabular Data module not available.');
				console.log('---------------------------------------------------------------');
				console.log('The Data Extraction suite is an optional add-on, available for download');
				console.log('at https://docs.apryse.com/documentation/core/info/modules/. If you have already');
				console.log('downloaded this module, ensure that the SDK is able to find the required files');
				console.log('using the PDFNet.addResourceSearchPath() function.\n');
			}
			else
			{
				try {
					// Extract tabular data as a JSON file
					console.log('Extract tabular data as a JSON file');

					let outputFile = outputPath + 'table.json';
					await PDFNet.DataExtractionModule.extractData(inputPath + 'table.pdf', outputFile, PDFNet.DataExtractionModule.DataExtractionEngine.e_Tabular);

					console.log('Result saved in ' + outputFile);

					///////////////////////////////////////////////////////
					// Extract tabular data as a JSON string
					console.log('Extract tabular data as a JSON string');

					outputFile = outputPath + 'financial.json';
					const json = await PDFNet.DataExtractionModule.extractDataAsString(inputPath + 'financial.pdf', PDFNet.DataExtractionModule.DataExtractionEngine.e_Tabular);
					fs.writeFileSync(outputFile, json);

					console.log('Result saved in ' + outputFile);

					///////////////////////////////////////////////////////
					// Extract tabular data as an XLSX file
					console.log('Extract tabular data as an XLSX file');

					outputFile = outputPath + 'table.xlsx';
					await PDFNet.DataExtractionModule.extractToXLSX(inputPath + 'table.pdf', outputFile);

					console.log('Result saved in ' + outputFile);

					///////////////////////////////////////////////////////
					// Extract tabular data as an XLSX stream (also known as filter)
					console.log('Extract tabular data as an XLSX stream');

					outputFile = outputPath + 'financial.xlsx';
					const outputXlsxStream = await PDFNet.Filter.createMemoryFilter(0, false);
					const options = new PDFNet.DataExtractionModule.DataExtractionOptions();
					options.setPages("1"); // page 1
					await PDFNet.DataExtractionModule.extractToXLSXWithFilter(inputPath + 'financial.pdf', outputXlsxStream, options);
					outputXlsxStream.memoryFilterSetAsInputFilter();
					outputXlsxStream.writeToFile(outputFile, false);

					console.log('Result saved in ' + outputFile);
				} catch (err) {
					console.log(err);
				}
			}

			//////////////////////////////////////////////////////////////////////////
			// The following sample illustrates how to extract document structure from PDF documents.
			//////////////////////////////////////////////////////////////////////////

			// Test if the add-on is installed
			if (!await PDFNet.DataExtractionModule.isModuleAvailable(PDFNet.DataExtractionModule.DataExtractionEngine.e_DocStructure)) {
				console.log('\nUnable to run Data Extraction: Apryse SDK Structured Output module not available.');
				console.log('---------------------------------------------------------------');
				console.log('The Data Extraction suite is an optional add-on, available for download');
				console.log('at https://docs.apryse.com/documentation/core/info/modules/. If you have already');
				console.log('downloaded this module, ensure that the SDK is able to find the required files');
				console.log('using the PDFNet.addResourceSearchPath() function.\n');
			}
			else
			{
				try {
					// Extract document structure as a JSON file
					console.log('Extract document structure as a JSON file');

					let outputFile = outputPath + 'paragraphs_and_tables.json';
					await PDFNet.DataExtractionModule.extractData(inputPath + 'paragraphs_and_tables.pdf', outputFile, PDFNet.DataExtractionModule.DataExtractionEngine.e_DocStructure);

					console.log('Result saved in ' + outputFile);

					///////////////////////////////////////////////////////
					// Extract document structure as a JSON string
					console.log('Extract document structure as a JSON string');

					outputFile = outputPath + 'tagged.json';
					const json = await PDFNet.DataExtractionModule.extractDataAsString(inputPath + 'tagged.pdf', PDFNet.DataExtractionModule.DataExtractionEngine.e_DocStructure);
					fs.writeFileSync(outputFile, json);

					console.log('Result saved in ' + outputFile);
				} catch (err) {
					console.log(err);
				}
			}

			//////////////////////////////////////////////////////////////////////////
			// The following sample illustrates how to extract form fields from PDF documents.
			//////////////////////////////////////////////////////////////////////////

			// Test if the add-on is installed
			if (!await PDFNet.DataExtractionModule.isModuleAvailable(PDFNet.DataExtractionModule.DataExtractionEngine.e_Form)) {
				console.log('\nUnable to run Data Extraction: Apryse SDK AIFormFieldExtractor module not available.');
				console.log('---------------------------------------------------------------');
				console.log('The Data Extraction suite is an optional add-on, available for download');
				console.log('at https://docs.apryse.com/documentation/core/info/modules/. If you have already');
				console.log('downloaded this module, ensure that the SDK is able to find the required files');
				console.log('using the PDFNet.addResourceSearchPath() function.\n');
			}
			else
			{
				try {
					// Extract form fields as a JSON file
					console.log('Extract form fields as a JSON file');

					let outputFile = outputPath + 'formfields-scanned.json';
					await PDFNet.DataExtractionModule.extractData(inputPath + 'formfields-scanned.pdf', outputFile, PDFNet.DataExtractionModule.DataExtractionEngine.e_Form);

					console.log('Result saved in ' + outputFile);

					///////////////////////////////////////////////////////
					// Extract form fields as a JSON string
					console.log('Extract form fields as a JSON string');

					outputFile = outputPath + 'formfields.json';
					const json = await PDFNet.DataExtractionModule.extractDataAsString(inputPath + 'formfields.pdf', PDFNet.DataExtractionModule.DataExtractionEngine.e_Form);
					fs.writeFileSync(outputFile, json);

					console.log('Result saved in ' + outputFile);
				} catch (err) {
					console.log(err);
				}
			}

			//////////////////////////////////////////////////////////////////////////

			console.log('Done.');
		};

		PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) {
			console.log('Error: ' + JSON.stringify(error));
		}).then(function () { return PDFNet.shutdown(); });
	};
	exports.runDataExtractionTest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=DataExtractionTest.js
