Redact documents with GroupDocs.Redaction
Leave feedback
On this page
GroupDocs.Redaction removes sensitive information from a document rather than merely hiding it — the redacted text is gone from the file, not covered by a black box you can drag away. It redacts text, metadata, annotations and image regions, and can rasterise the result to PDF so the original content cannot be recovered.
Every example below opens contract.docx and is ready to copy into a project once you have installed the package.
Redact an exact phrase
ExactPhraseRedaction replaces every occurrence of a phrase, and ReplacementOptions decides what takes its place. The contract names its engagement Project Northlight in the body — that codename is what a redacted copy has to lose.
importjava.io.FileOutputStream;importcom.groupdocs.redaction.RedactionStatus;importcom.groupdocs.redaction.Redactor;importcom.groupdocs.redaction.RedactorChangeLog;importcom.groupdocs.redaction.options.RasterizationOptions;importcom.groupdocs.redaction.redactions.ExactPhraseRedaction;importcom.groupdocs.redaction.redactions.ReplacementOptions;try(Redactorredactor=newRedactor("contract.docx")){RedactorChangeLogresult=redactor.apply(newExactPhraseRedaction("Project Northlight",newReplacementOptions("[REDACTED]")));System.out.println("Status: "+result.getStatus());if(result.getStatus()!=RedactionStatus.Failed){// save() with no path overwrites the source, so write to a new file via a stream.
// Rasterization stays off to keep the result an editable DOCX.
RasterizationOptionsrasterizationOptions=newRasterizationOptions();rasterizationOptions.setEnabled(false);try(FileOutputStreamoutput=newFileOutputStream("redaction-exact-phrase.docx")){redactor.save(output,rasterizationOptions);}}}
contract.docx is the sample file used in this example. Click here to download it.
A regex redaction catches patterns rather than literals — account numbers, amounts, email addresses — which is what most compliance rules are actually written against. The contract’s fee, 120,000 USD, is exactly the kind of figure to strip.
importjava.io.FileOutputStream;importcom.groupdocs.redaction.RedactionStatus;importcom.groupdocs.redaction.Redactor;importcom.groupdocs.redaction.RedactorChangeLog;importcom.groupdocs.redaction.options.RasterizationOptions;importcom.groupdocs.redaction.redactions.RegexRedaction;importcom.groupdocs.redaction.redactions.ReplacementOptions;try(Redactorredactor=newRedactor("contract.docx")){// Any currency amount such as "120,000 USD"
RedactorChangeLogresult=redactor.apply(newRegexRedaction("\\d{1,3}(,\\d{3})*\\s?USD",newReplacementOptions("[AMOUNT]")));System.out.println("Status: "+result.getStatus());if(result.getStatus()!=RedactionStatus.Failed){RasterizationOptionsrasterizationOptions=newRasterizationOptions();rasterizationOptions.setEnabled(false);try(FileOutputStreamoutput=newFileOutputStream("redaction-regex.docx")){redactor.save(output,rasterizationOptions);}}}
contract.docx is the sample file used in this example. Click here to download it.
Redacting the visible text is only half the job — the author, the revision history and the original filename usually live in the metadata. EraseMetadataRedaction clears them.
Turning rasterization on with rasterizationOptions.setEnabled(true) converts each page to an image inside a PDF. It is the strongest option — no text layer survives for anyone to recover — at the cost of a result that is no longer searchable or editable.
Learn more
GroupDocs.Redaction also redacts image areas, deletes or rewrites annotations, removes whole pages, applies several redactions in one pass, and supports custom redaction policies loaded from configuration.