Text redactions

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Redaction MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-redaction -- dnx GroupDocs.Redaction.Mcp --yes
Folders, licensing and more →

You can redact text with ExactPhraseRedaction or RegexRedaction, using a replacement string or a colored box via ReplacementOptions. To limit matches to a page range or area, set filters on ReplacementOptions (see Using redaction filters).

Use exact phrase redaction

In the example below, we apply textual redaction, replacing personal exact phrase “John Doe” with “[personal]” (or any exemption code):

const redaction = require('@groupdocs/groupdocs.redaction');

const redactor = new redaction.Redactor('sample.docx');
try 
{
    redactor.apply(new redaction.ExactPhraseRedaction('John Doe', new redaction.ReplacementOptions('[personal]')));
    redactor.save();
}
finally {
  redactor.close();
}

sample.docx is the sample file used in this example. Click here to download it.

Binary file (PDF, 1012 KB)

Download full output

By default, search for exact phase is case insensitive.For a case-sensitive redaction, there is a constructor parameter and corresponding public property:

const redaction = require('@groupdocs/groupdocs.redaction');

const redactor = new redaction.Redactor('sample.docx');
try
{
    redactor.apply(new redaction.ExactPhraseRedaction('John Doe', true /*isCaseSensitive*/, new redaction.ReplacementOptions('[personal]')));
    redactor.save();
}
finally {
  redactor.close();
}

sample.docx is the sample file used in this example. Click here to download it.

Binary file (PDF, 1012 KB)

Download full output

If you need a color box over the redacted text, you can use color instead of replacement string. The redaction will erase matched text and put a rectangle of the specified color in place of redacted text:

const redaction = require('@groupdocs/groupdocs.redaction');
const java = require('java');
const Color = java.import('java.awt.Color');

const redactor = new redaction.Redactor('sample.docx');
try
{
    redactor.apply(new redaction.ExactPhraseRedaction('John Doe', new redaction.ReplacementOptions(Color.RED)));
    redactor.save();
}
finally {
  redactor.close();
}

sample.docx is the sample file used in this example. Click here to download it.

Binary file (PDF, 1003 KB)

Download full output

You might need to apply redaction to a right-to-left document, such as Arabic or Hebrew. The following example demonstrates how to apply ExactPhraseRedaction to an Arabic PDF document:

const redaction = require('@groupdocs/groupdocs.redaction');

const redactor = new redaction.Redactor('Arabic.pdf');
try
{
const red = new redaction.ExactPhraseRedaction('أﺑﺠﺪ', new redaction.ReplacementOptions('[test]'));
    red.setRightToLeft(true);
    redactor.apply(red);
    redactor.save();
}
finally {
  redactor.close();
}

arabic.pdf is the sample file used in this example. Click here to download it.

Binary file (PDF, 318 KB)

Download full output

Use regular expression

Behind the scenes, “exact phrase” redaction works though regular expressions, which are the baseline approach for redaction. In the example below, we redact out any text, matching “2 digits, space or nothing, 2 digits, again space and 6 digits” with a blue color box:

const redaction = require('@groupdocs/groupdocs.redaction');
const java = require('java');
const Color = java.import('java.awt.Color');

const redactor = new redaction.Redactor('sample.docx');
try
{
    redactor.apply(new redaction.RegexRedaction('\\d{2}\\s*\\d{2}[^\\d]*\\d{6}', new redaction.ReplacementOptions(Color.BLUE)));
const saveOptions = new redaction.SaveOptions();
    saveOptions.setAddSuffix(true);
    saveOptions.setRasterizeToPDF(false);
    redactor.save(saveOptions);
}
finally {
  redactor.close();
}

sample.docx is the sample file used in this example. Click here to download it.

Binary file (DOCX, 17 KB)

Download full output

If you need to apply redact a whole paragraph, you might also need to use RegexRedaction. The following example demonstrates how to redact the whole paragraph in a PDF document:

const redaction = require('@groupdocs/groupdocs.redaction');

const redactor = new redaction.Redactor('LoremIpsum.pdf');
try
{
    redactor.apply(new redaction.RegexRedaction('(Lorem(\n|.)+?urna)', new redaction.ReplacementOptions('[test]')));
const saveOptions = new redaction.SaveOptions();
    saveOptions.setAddSuffix(true);
    saveOptions.setRasterizeToPDF(false);
    redactor.save(saveOptions);
}
finally {
  redactor.close();
}

sample.pdf is the sample file used in this example. Click here to download it.

Binary file (PDF, 2.9 MB)

Download full output