Redaction
Permanently remove text, images and annotations from PDFs by area, by text, by regular expression or by pattern with NexPDF for Node.js.
License module
Redaction needs the redaction module (Business tier and above).
Redaction in NexPDF is destructive: the text, images and annotations under the area are removed
from the file, not just covered, and an opaque box is painted over it: black, or white with
fillWhite. Save the document afterwards.
Redact an area
import { PdfDocument } from '@nexdevit/nexpdf-node';
const doc = await PdfDocument.open('statement.pdf');
doc.redact(0, [72, 600, 300, 620]); // page, rect; black box
doc.redact(1, [72, 500, 300, 520], { fillWhite: true }); // white box
await doc.save('redacted.pdf');Redact every match of a text, an expression or a pattern
doc.raw.redactMatches(query, mode, fillWhite) finds and redacts every match in the document and
returns the number of areas redacted. fillWhite is 0 or 1.
mode | query is |
|---|---|
0 | Plain text |
1 | A regular expression |
2 | A pattern id: email, phone, credit-card, iban, pt-nif, us-ssn, ipv4, url, date |
let n = doc.raw.redactMatches('CONFIDENTIAL', 0, 0);
n += doc.raw.redactMatches(String.raw`\b\d{3}-\d{2}-\d{4}\b`, 1, 0);
n += doc.raw.redactMatches('email', 2, 0);
console.log(`${n} areas redacted`);
await doc.save('redacted.pdf');Review before redacting
Use search to show the matches to a reviewer first, for example as highlight annotations, and apply the redaction once they are approved.
Redaction cannot be undone in the saved file. Keep the original if you may need it.