Docs / Guides
Redaction
SoftZenPdf.Redact removes information from PDFs for good. Find text with regular expressions or mark areas, and everything drawn there is taken out of the page itself: the text, the shapes and the image pixels. A black box is drawn on top, like a redaction bar on paper, but unlike a box drawn by hand there is nothing left beneath it to select, copy, search or extract.
Installing
dotnet add package SoftZenPdf.Redact
It brings in SoftZenPdf and needs no native libraries, so it runs wherever .NET 8 does.
Redacting a document
using SoftZenPdf;
using SoftZenPdf.Redact;
var redactor = PdfRedactor.Open("record.pdf");
redactor.RedactText(@"\d{3}-\d{2}-\d{4}"); // social security numbers
redactor.RedactText(@"[\w.+-]+@[\w-]+\.[\w.]+"); // email addresses
redactor.RedactArea(pageIndex: 0, 72, 640, 320, 700); // an area, in points
var result = redactor.Save("record-redacted.pdf");
Console.WriteLine($"{result.RemovedCharacters} characters removed, {result.RemainingMatches.Count} matches left");
RedactText and RedactArea only mark what to remove; Save does the work and writes a
new file. The original is not changed, and the same redactor can be saved more than once.
Previewing matches
Find returns the matches with their areas without marking anything, so you can review them or show them to a person first:
using SoftZenPdf;
using SoftZenPdf.Redact;
var redactor = PdfRedactor.Open("record.pdf");
foreach (var match in redactor.Find(@"\d{3}-\d{2}-\d{4}"))
{
var area = match.Areas[0];
Console.WriteLine($"page {match.PageIndex + 1}: {match.Text} at {area.X1:0},{area.Y1:0} – {area.X2:0},{area.Y2:0}");
}
Areas are in points, in the page's own coordinates (origin at the bottom-left, before any page rotation), the same as text extraction uses. A match that runs over a line break gets one area per line.
Text patterns
using SoftZenPdf;
using SoftZenPdf.Redact;
var redactor = PdfRedactor.Open("record.pdf");
redactor.RedactText("jane doe", RegexOptions.IgnoreCase); // a name, in any case
redactor.RedactText(new Regex(@"\b\d{3}-\d{2}-\d{4}\b"), margin: 1.5); // a little room around each match
Console.WriteLine(redactor.GetPageText(0)); // the text the patterns run against
redactor.Save("record-patterns.pdf");
- Patterns are .NET regular expressions, run against each page's text in reading order: lines separated by
\n, words by spaces.GetPageTextshows exactly that text. - Kerned text, embedded TrueType fonts, text in form XObjects and invisible text (such as an OCR layer) are all searched.
- In invisible OCR text, matches are widened to whole words, because OCR places letters only approximately inside each word.
- A
margingrows each match's area by that many points on every side.
What is removed
| Content | What happens |
|---|---|
| Text | Every glyph whose box touches an area is cut out of the page content. The rest of a partly covered word or line stays exactly where it was. |
| Vector graphics | Fills and strokes lose exactly the covered part of their geometry; shapes entirely inside an area are removed. Gradients are clipped around the areas. |
| Images | Every pixel touching an area is blanked in the image data, for JPEG, CCITT fax, Flate and other images, with their soft masks; the image is stored again without them. Images entirely inside an area are removed. |
| Form XObjects | A reusable drawing (such as a stamp) is copied and redacted for the place where it is covered; its other uses keep the original. |
| Annotations and form fields | Those overlapping an area are removed, with their pop-up notes, and form fields are taken out of the form. |
| Text around the page | Text matching your patterns is removed from the document metadata and XMP, bookmarks, annotation notes, form values, marked-content /ActualText and the structure tree's alternate text. |
| Hidden copies | Page thumbnails and application private data (/PieceInfo) on redacted pages are removed. |
| History | The output is a new file: earlier revisions, unused objects, object streams and encryption are not carried over. |
Options
using SoftZenPdf;
using SoftZenPdf.Redact;
var redactor = PdfRedactor.Open("record.pdf");
redactor.FillColor = PdfColor.FromHex("#1F2937"); // dark grey boxes; null draws none
redactor.ScrubDocumentText = true; // also clean metadata, bookmarks, notes and form values
redactor.RedactText("Jane Doe");
redactor.Save("record-options.pdf");
FillColor: the colour of the box drawn over each area. Black by default;nulldraws no box. Either way the content is gone.ScrubDocumentText: whether text patterns are also removed outside the pages. On by default.
Checking the result
using SoftZenPdf;
using SoftZenPdf.Redact;
RedactionResult result = redactor.Save("record-checked.pdf");
Console.WriteLine($"Pages: {result.RedactedPages}, characters: {result.RemovedCharacters}, " +
$"paths: {result.ChangedPaths}, images: {result.ChangedImages}, annotations: {result.RemovedAnnotations}");
foreach (var warning in result.Warnings) Console.WriteLine(warning);
if (result.RemainingMatches.Count > 0)
foreach (var match in result.RemainingMatches) Console.WriteLine($"Still present on page {match.PageIndex + 1}: {match.Text}");
After saving, the output is opened again and searched for every pattern. RemainingMatches should be empty; anything
listed there was still found and needs an area of its own. Warnings explains anything removed more broadly than
asked, such as an image that could not be decoded.
Scanned documents
A scan is an image, so it has no text to match until it is recognised. Add a text layer with SoftZenPdf.Ocr first; redacting the matches then removes both the recognised text and the pixels of the scan underneath:
using SoftZenPdf;
using SoftZenPdf.Ocr;
using SoftZenPdf.Redact;
using var engine = new TesseractOcrEngine(new TesseractOcrOptions { TessDataPath = "tessdata" });
new OcrPdfCreator(engine).AddTextLayer("scanned-invoice.pdf", "scanned-searchable.pdf");
var redactor = PdfRedactor.Open("scanned-searchable.pdf");
redactor.RedactText(@"\d,\d{3}\.\d{2}"); // amounts: the OCR text and the scan's pixels both go
redactor.Save("scanned-redacted.pdf");
Limits
- JBIG2 and JPEG 2000 images that are only partly covered cannot be decoded, so they are removed entirely and reported in
Warnings. - A redacted JPEG is stored losslessly, so it grows. Blanked pixels are black in grey and RGB images and white in CMYK ones; the box covers them.
- Embedded fonts are kept, so a font subset still lists the characters the document used.
- The content of tiling patterns is not changed; where a pattern is painted inside an area, the painted shape is cut.
- Text drawn as outlines or images is not matched by patterns; mark it with an area, or recognise it with OCR first.
SoftZenPdf.Redact is covered by your SoftZenPdf license, and saves with the evaluation mark until a key is set (see Licensing).
API: PdfRedactor, RedactionArea, RedactionMatch, RedactionResult.