Skip to content
Redaction

Redaction

Redaction in UniPDF is done by the redactor package, and it removes content rather than hiding it. A Redactor extracts each page’s text, matches it against your regular expressions, deletes the matched characters from the page’s content stream, and draws a filled rectangle over the area they occupied. The deleted glyphs are replaced by a numeric position adjustment of the same width, so the surrounding text stays where it was and the redacted text is no longer extractable.

red := redactor.New(reader, &redactor.RedactionOptions{Terms: terms}, nil)
if err := red.Redact(); err != nil {
    return err
}

err := red.WriteToFile("output.pdf")

Three types make up the API. RedactionOptions holds the list of RedactionTerm values, each wrapping one compiled *regexp.Regexp, plus the UseStandardPatterns switch that adds the package’s built-in patterns for things like emails and card numbers. RectangleProps describes the mark: fill color, border width and fill opacity. The Redactor itself holds the reader and does the work, over every page of the document.

What redaction is not is search and replace. Both find text by regular expression and both edit the content stream, but replacement swaps one string for another and leaves the rest of the document alone, while redaction removes the text and marks the page. If you want to change wording rather than destroy information, see search and replace.

Whether the text really left the file depends on where it was drawn. Text in the page content stream is removed; text in an image, in vector outlines, or inside a form XObject or an annotation is not. Verify the output by extracting text from it before treating a document as redacted.

Where to look

GuideCovers
Text redactionMatching with regular expressions, the built-in patterns, the rectangle, and what is not removed.
Last updated on