Redaction Basics

Redaction types

GroupDocs.Redaction comes with the following redaction types:

TypeDescriptionClasses
TextReplaces or hides with color block a portion of text within document bodyExactPhraseRedaction, RegexRedaction
MetadataReplace metadata values with empty ones or redacts metadata textsEraseMetadataRedaction, MetadataSearchRedaction
AnnotationsDeletes annotations from document or redacts its textsDeleteAnnotationRedaction, AnnotationRedaction
ImagesReplaces specific area of an image with a colored boxImageAreaRedaction
PagesRemoves specific range of pages (slides, worksheets, etc.)RemovePageRedaction

Apply redaction

Applying redaction to a document is done through Redactor.apply method. As a result, you receive RedactorChangeLog instance, containing a log entry for each redaction applied. The entry contains reference to Redaction instance including its options, status of the operation (see below) and textual descriptions when applicable. If at least one redaction failed, you will see Status == RedactionStatus.Failed:

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();
}

All possible statuses are listed in this table:

StatusDescriptionPossible reasons
AppliedRedaction was fully and successfully appliedAll operations within redaction process were successfully applied
PartiallyAppliedRedaction was applied only to a part of its matches1) Trial limitations for replacements were exceeded2) At least one change was rejected by user
SkippedRedaction was skipped (not applied)1) Trial limitations for redactions were exceeded2) Redaction cannot be applied to this type of document3) All replacements were rejected by user and no changes were made
FailedRedaction failed with exceptionAn exception occurred in process of redaction

For detailed information you have to iterate through redaction log entries in RedactorChangeLog.RedactionLog and check for ErrorMessage property of any items with status other than Applied:

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

const result = redactor.apply(/* ... */);
if (result.getStatus() !== redaction.RedactionStatus.Failed) {
  const log = result.getRedactionLog();
  for (let i = 0; i < log.size(); i++) {
    const logEntry = log.get(i);
    if (logEntry.getResult().getStatus() !== redaction.RedactionStatus.Applied) {
      console.log(logEntry.getRedaction().getClass().getName() + ' status is ' +
        logEntry.getResult().getStatus() + ', details: ' + logEntry.getResult().getErrorMessage());
    }
  }
}

Apply multiple redactions

You can apply as much redactions as you need in a single call to Redactor.apply() method, since its overload accepts a Java array of redactions and redaction policy. Build that array with java.newArray('com.groupdocs.redaction.Redaction', [...]) (base class is com.groupdocs.redaction.Redaction). In this case, redactions will be applied in the same order as they appear in the array. As an alternative to specifying redaction sets in your code, you can create an XML file with redaction policy, as described here.

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 {
  const redactionList = java.newArray('com.groupdocs.redaction.Redaction', [
    new redaction.ExactPhraseRedaction('John Doe', new redaction.ReplacementOptions('[Client]')),
    new redaction.RegexRedaction('Redaction', new redaction.ReplacementOptions('[Product]')),
    new redaction.RegexRedaction('\\d{2}\\s*\\d{2}[^\\d]*\\d{6}', new redaction.ReplacementOptions(Color.BLUE)),
    new redaction.DeleteAnnotationRedaction(),
    new redaction.EraseMetadataRedaction(redaction.MetadataFilters.All)
  ]);
  const result = redactor.apply(redactionList);
  if (result.getStatus() !== redaction.RedactionStatus.Failed) {
    redactor.save();
  }
} finally {
  redactor.close();
}