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

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

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

	exports.runDocumentCreationTest = () => {

		const main = async () => {

			const outputPath = '../TestFiles/Output/';
			try {
				const para_text = 
				"Lorem ipsum dolor " +
				"sit amet, consectetur adipisicing elit, sed " +
				"do eiusmod tempor incididunt ut labore " +
				"et dolore magna aliqua. Ut enim ad " +
				"minim veniam, quis nostrud exercitation " +
				"ullamco laboris nisi ut aliquip ex ea " +
				"commodo consequat. Duis aute irure " +
				"dolor in reprehenderit in voluptate velit " +
				"esse cillum dolore eu fugiat nulla pariatur. " +
				"Excepteur sint occaecat cupidatat " +
				"non proident, sunt in culpa qui officia " +
				"deserunt mollit anim id est laborum.";

				const flowdoc = await PDFNet.FlowDocument.create();
				const para = await flowdoc.addParagraph();
		
				para.setFontSize(24);
				para.setTextColor(255, 0, 0);
				para.addText("Start Red Text\n");
				flowdoc.setDefaultMargins(0, 72.0, 144.0, 228.0);
				flowdoc.setDefaultPageSize(650, 750);
				flowdoc.addParagraphWithText(para_text);
		
				for (var i = 0; i < 50; i++) {
					const para = await flowdoc.addParagraph();
					const point_size = (17*i*i*i)%13+5;
					if (i % 2 == 0) 
					{
						para.setItalic(true);
						para.setTextColor(50, 50, 199);
						para.setSpaceBefore(20);
						para.setJustificationMode(PDFNet.ParagraphStyle.TextJustification.e_text_justify_left);
					}
					else
					{
						para.setTextColor(30, 199, 30);
						para.setSpaceBefore(50);
						para.setJustificationMode(PDFNet.ParagraphStyle.TextJustification.e_text_justify_right);
					}
		
					para.addText(para_text);
					para.setFontSize(point_size);
				}
				 
				const my_pdf = await flowdoc.paginateToPDF();
				my_pdf.save(outputPath + "created_doc.pdf", PDFNet.SDFDoc.SaveOptions.e_linearized);
				console.log('Done.');
			} catch (err) {
			  console.log(err);
			}
		};

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