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

//---------------------------------------------------------------------------------------
// The following sample illustrates how to extract xlf from a PDF document for translation.
// It then applies a pre-prepared translated xlf file to the PDF to produce a translated PDF. 
//---------------------------------------------------------------------------------------

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

((exports) => {

	exports.runTransPDF = () => {

		const main = async () => {
			// Relative path to the folder containing test files.
			const inputPath = '../TestFiles/';
			const outputPath = inputPath + 'Output/';

			try {
				// start stack-based deallocation with startDeallocateStack. Later on when endDeallocateStack is called,
				// all objects in memory that were initialized since the most recent startDeallocateStack call will be
				// cleaned up. Doing this makes sure that memory growth does not get too high.
				await PDFNet.startDeallocateStack();

				// Open a PDF document to translate
				const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'find-replace-test.pdf');

				const options = await PDFNet.TransPDF.createTransPDFOptions();

				// Set the source language in the options
				options.setSourceLanguage("en");

				// Set the number of pages to process in each batch
				options.setBatchSize(20);

				// Optionally, subset the pages to process
				// This PDF only has a single page, but you can specify a subset of pages like this
				// options.setPages("-2,5-6,9,11-");

				// Optionally, set the XLIFF exported version, default is 1.2
				// options.setXLIFFVersion(PDFNet.TransPDF.TransPDFOptions.XLIFFVersion.e_xliff_version_2);

				// Extract the xlf to file and field the PDF for translation
				await PDFNet.TransPDF.extractXLIFF(doc, outputPath + 'find-replace-test.xlf', options);

				// Save the fielded PDF
				await doc.save(outputPath + 'find-replace-test-fielded.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);

				// The extracted xlf can be translated in a system of your choice.
				// In this sample a pre-prepared translated file is used - find-replace-test_(en_to_fr).xlf

				// Perform the translation using the pre-prepared translated xliff
				await PDFNet.TransPDF.applyXLIFF(doc, inputPath + "find-replace-test_(en_to_fr).xlf", options);

				await doc.save(outputPath + 'find-replace-test-fr.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);

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