Some test text!

menu

PDF redaction in JavaScript

Switch language

chevron_right
Switch language
JavaScript
JS (Node.js)

Sample JavaScript code for using PDFTron SDK to remove potentially sensitive content within PDF documents. Using 'pdftron.PDF.Redactor' makes sure that if a portion of an image, text, or vector graphics is contained in a redaction region, that portion is destroyed and is not simply hidden with clipping or image masks. Learn more about our JavaScript PDF Library.

Get StartedSamplesDownload

To run this sample, get started with a free trial of PDFTron SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------


const { PDFNet } = require('../../lib/pdfnet.js');

((exports) => {

  exports.runPDFRedactTest = () => {

    const main = async() => {
      console.log('Beginning Test');
      // Relative path to the folder containing test files.
      const inputPath = '../TestFiles/';
      // eslint-disable-next-line no-unused-vars
      let ret = 0;
      try {
        const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
        doc.initSecurityHandler();
        console.log('PDF document initialized and locked');

        const redactionArray = []; // we will contain a list of redaction objects in this array
        redactionArray.push(await PDFNet.Redactor.redactionCreate(1, (await PDFNet.Rect.init(100, 100, 550, 600)), false, 'Top Secret'));
        redactionArray.push(await PDFNet.Redactor.redactionCreate(2, (await PDFNet.Rect.init(30, 30, 450, 450)), true, 'Negative Redaction'));
        redactionArray.push(await PDFNet.Redactor.redactionCreate(2, (await PDFNet.Rect.init(0, 0, 100, 100)), false, 'Positive'));
        redactionArray.push(await PDFNet.Redactor.redactionCreate(2, (await PDFNet.Rect.init(100, 100, 200, 200)), false, 'Positive'));
        redactionArray.push(await PDFNet.Redactor.redactionCreate(2, (await PDFNet.Rect.init(300, 300, 400, 400)), false, ''));
        redactionArray.push(await PDFNet.Redactor.redactionCreate(2, (await PDFNet.Rect.init(500, 500, 600, 600)), false, ''));
        redactionArray.push(await PDFNet.Redactor.redactionCreate(3, (await PDFNet.Rect.init(0, 0, 700, 20)), false, ''));

        const appear = {};
        appear.redaction_overlay = true;
        const blue = await PDFNet.ColorPt.init(0.1, 0.2, 0.6, 0);
        appear.positive_overlay_color = blue;
        appear.border = false;
        const timesFont = await PDFNet.Font.create(doc, PDFNet.Font.StandardType1Font.e_times_roman);
        appear.font = timesFont;
        appear.show_redacted_content_regions = true;
        PDFNet.Redactor.redact(doc, redactionArray, appear, false, false);

        await doc.save('../TestFiles/Output/redacted.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
        console.log('done');
      } catch (err) {
        console.log(err.stack);
        ret = 1;
      }
    };
    // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
    PDFNet.runWithCleanup(main).then(function(){PDFNet.shutdown();});
  };
  exports.runPDFRedactTest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=PDFRedactTest.js
close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.