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

//------------------------------------------------------------------------------
// PDFNet's Sanitizer is a security-focused feature that permanently removes
// hidden, sensitive, or potentially unsafe content from a PDF document.
// While redaction targets visible page content such as text or graphics,
// sanitization focuses on non-visual elements and embedded structures.
//
// PDFNet Sanitizer ensures hidden or inactive content is destroyed,
// not merely obscured or disabled. This prevents leakage of sensitive
// data such as authoring details, editing history, private identifiers,
// and residual form entries, and neutralizes scripts or attachments.
//
// Sanitization is recommended prior to external sharing with clients,
// partners, or regulatory bodies. It helps align with privacy policies
// and compliance requirements by permanently removing non-visual data.
//------------------------------------------------------------------------------

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

((exports) => {

    exports.runPDFSanitizeTest = () => {

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

            // The following example illustrates how to retrieve the existing
            // sanitizable content categories within a document.
            try {
                const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'numbered.pdf');
                if (await doc.initSecurityHandler()) {
                    const opts = await PDFNet.Sanitizer.getSanitizableContent(doc);
                    if (opts.getMetadata()) {
                        console.log('Document has metadata.');
                    }
                    if (opts.getMarkups()) {
                        console.log('Document has markups.');
                    }
                    if (opts.getHiddenLayers()) {
                        console.log('Document has hidden layers.');
                    }
                }
                console.log('Done...');
            } catch (err) {
                console.log(err.stack);
            }

            // The following example illustrates how to sanitize a document with default options,
            // which will remove all sanitizable content present within a document.
            try {
                const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'financial.pdf');
                if (await doc.initSecurityHandler()) {
                    await PDFNet.Sanitizer.sanitizeDocument(doc);
                    await doc.save(outputPath + 'financial_sanitized.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
                }
                console.log('Done...');
            } catch (err) {
                console.log(err.stack);
            }

            // The following example illustrates how to sanitize a document with custom set options,
            // which will only remove the content categories specified by the options object.
            try {
                const options = new PDFNet.Sanitizer.SanitizeOptions();
                options.setMetadata(true);
                options.setFormData(true);
                options.setBookmarks(true);

                const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'form1.pdf');
                if (await doc.initSecurityHandler()) {
                    await PDFNet.Sanitizer.sanitizeDocument(doc, options);
                    await doc.save(outputPath + 'form1_sanitized.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
                }
                console.log('Done...');
            } 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.runPDFSanitizeTest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=PDFSanitizeTest.js