Guide
How to redact PDF content in C#
Mark exact extracted-text matches or explicit page regions, remove matched text and intersecting supported painted content, and save the output as a rewritten file.
This redaction API is a document-level workflow on Pdf. It removes matched text from supported page content, redacts intersecting supported vector and XObject content, draws the visible redaction overlay, and rewrites the saved file so earlier file history is not preserved.
Mark exact text and apply redaction
This example removes one exact text match from supported page content, adds overlay text, and saves the result as a rewritten file.
using var pdf = Pdf.Load(File.OpenRead("input.pdf"));
var plan = await pdf.RedactionAsync();
await plan.MarkTextAsync("12345");
await plan.ApplyAsync(new PdfRedactionOptions
{
OverlayText = "REDACTED"
});
await pdf.SaveAsync(File.Create("redacted.pdf"));
Match text with a regular expression
Use the regex overload when the content follows a known pattern, such as identifiers or account numbers.
using System.Text.RegularExpressions;
await plan.MarkTextAsync(
new Regex(@"\b\d{3}-\d{2}-\d{4}\b"));
Mark an explicit region on a page
Use region marks when you already know the page and rectangle that should be removed.
plan.MarkRegion(
1,
Rectangle.FromCoordinates(
new Coordinate(72, 520),
new Coordinate(180, 548)));
Redaction uses rewritten-file save behavior
ApplyAsync(...) opts into RemoveHistoryAsync() before you save, so earlier incremental revisions are not preserved in the output file.
await plan.ApplyAsync(new PdfRedactionOptions
{
FillColor = RGBColour.Black,
OverlayText = "REDACTED"
});
await pdf.SaveAsync(File.Create("redacted.pdf"));
Current limits
This version removes matched text from supported page content and structurally redacts intersecting vector paths, image XObjects, and form XObjects. It still refuses inline images and shading content, and it does not yet derive OCR bounds for scanned pages.
Need the document-level API details too?
The docs page covers PdfRedactionPlan, save behavior, and the rest of the core document workflow.