Search and Redact PDF on Server/Desktop
This sample code searches a PDF document for all instances of a search pattern and redacts these instances.
const { PDFNet } = require('@pdftron/pdfnet-node');
const ApryseLicense = require('../LicenseKey/LicenseKey');
const main = async () => {
const pattern = 'features'; // expression to search for
const redactText = 'Top Secret'; // text to show in place of redacted content. Can be empty string.
const doc = await PDFNet.PDFDoc.createFromURL(filename);
const txtSearch = await PDFNet.TextSearch.create();
const mode = PDFNet.TextSearch.Mode.e_whole_word + PDFNet.TextSearch.Mode.e_highlight; // Use whole word search
txtSearch.setMode(mode);
txtSearch.setPattern(pattern);
// call Begin() method to initialize the text search.
txtSearch.begin(doc, pattern, mode);
const redactions = []; // array to hold redaction objects
let result;
// loop to find all instances of the pattern
while ((result = await txtSearch.run()).code === PDFNet.TextSearch.ResultCode.e_found) {
// add a redaction object based on the location of the found instance
const highlights = result.highlights;
await highlights.begin(doc);
while (await highlights.hasNext()) {
const pageNumber = await highlights.getCurrentPageNumber();
const quadArr = await highlights.getCurrentQuads();
for (let i = 0; i < quadArr.length; ++i) {
const currQuad = quadArr[i];
const x1 = Math.min(Math.min(Math.min(currQuad.p1x, currQuad.p2x), currQuad.p3x), currQuad.p4x);
const x2 = Math.max(Math.max(Math.max(currQuad.p1x, currQuad.p2x), currQuad.p3x), currQuad.p4x);
const y1 = Math.min(Math.min(Math.min(currQuad.p1y, currQuad.p2y), currQuad.p3y), currQuad.p4y);
const y2 = Math.max(Math.max(Math.max(currQuad.p1y, currQuad.p2y), currQuad.p3y), currQuad.p4y);
redactions.push(await PDFNet.Redactor.redactionCreate(pageNumber, (await PDFNet.Rect.init(x1, y1, x2, y2)), false, redactText));
}
highlights.next();
}
}
const appearance = {};
appearance.redaction_overlay = true;
appearance.border = false;
appearance.positive_overlay_color = await PDFNet.ColorPt.init(1, 0.2, 0.2, 0); // red
appearance.show_redacted_content_regions = true;
PDFNet.Redactor.redact(doc, redactions, appearance, false, false);
doc.save('textsearch_redacted.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
}
PDFNet.runWithCleanup(main, ApryseLicense.key) // provide your license key here
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
})
.then(function () {
PDFNet.shutdown();
});
PDF redaction Full code sample which shows how to use Apryse's PDFNet.Redactor to remove potentially sensitive content within PDF documents.
About redactor
Apryse Redactor makes sure that if a portion of an image, text, or vector graphics is contained in a redaction region, that portion of the image or path data is destroyed and is not hidden with clipping or image masks. Apryse SDK API can also be used to review and remove metadata and other content that can exist in a PDF document, including XML Forms Architecture (XFA) content and Extensible Metadata Platform (XMP) content.
The redaction process in Apryse SDK consists of two steps:
1. Content identification A user applies redact annotations that specify the pieces or regions of content that should be removed. This example uses PDFNet.TextSearch to identify the content for redaction programmatically, but it can also be identified in other ways such as using PDFNet.TextExtractor, or interactively (e.g. using WebViewer). Up until the next step is performed, the user can see, move and redefine these annotations.
2. Content removal Using PDFNet.Redactor.redact() the user instructs Apryse SDK to apply the redact regions, after which the content in the area specified by the redact annotations is removed. The redaction function includes a number of options to control the style of the redaction overlay (including color, text, font, border, transparency, etc.)
Last updated
Was this helpful?