DEV Community

IronSoftware
IronSoftware

Posted on

How to Redact Text in PDF with C# (Developer Guide)

Our legal team was manually redacting client names from case files before public disclosure. Hours of work with Adobe Acrobat's redaction tool, and they still missed instances. One leaked name in a 200-page filing meant re-doing everything.

I automated the entire redaction process in C#. Now we redact thousands of PDFs with zero human error. Here's how.

What Is PDF Redaction?

Redaction permanently removes content from a PDF. Not hiding it—removing it. Text is deleted from the underlying file structure and replaced with black boxes or replacement text.

using IronPdf;
// Install via NuGet: Install-Package IronPdf

var pdf = PdfDocument.FromFile("confidential.pdf");
pdf.RedactTextOnAllPages("John Doe");
pdf.SaveAs("redacted.pdf");
Enter fullscreen mode Exit fullscreen mode

Every instance of "John Doe" is now gone from the PDF. Not masked—gone. Users can't copy it, search for it, or recover it.

Why Redaction Matters for Compliance

GDPR, HIPAA, and FOIA requests require permanent removal of sensitive data. Simply covering text with a black rectangle isn't enough—the text is still in the PDF file.

I've seen PDFs where someone drew black boxes over names using annotation tools. Copy the text beneath? The name appears. Search the PDF? It finds the "redacted" text.True redaction deletes the content from the file. IronPDF does this correctly.

How Do I Redact Specific Text?

Use RedactTextOnAllPages to remove all instances across the document:

using IronPdf;

var pdf = PdfDocument.FromFile("medical-records.pdf");

// Redact patient name
pdf.RedactTextOnAllPages("Jane Smith");

// Redact SSN
pdf.RedactTextOnAllPages("123-45-6789");

pdf.SaveAs("redacted-records.pdf");
Enter fullscreen mode Exit fullscreen mode

Both "Jane Smith" and the SSN are permanently removed from every page.

Can I Redact Only on Specific Pages?

Yes. Use RedactTextOnPage for a single page or RedactTextOnPages for multiple:

using IronPdf;

var pdf = PdfDocument.FromFile("document.pdf");

// Redact only on page 1
pdf.RedactTextOnPage("Confidential", 0); // 0-indexed

// Redact on pages 2, 3, and 5
pdf.RedactTextOnPages("Internal Only", new[] { 1, 2, 4 });

pdf.SaveAs("partially-redacted.pdf");
Enter fullscreen mode Exit fullscreen mode

This is useful when sensitive data appears only in specific sections.

How Do I Control Redaction Appearance?

Configure redaction options:

using IronPdf;

var pdf = PdfDocument.FromFile("contract.pdf");

pdf.RedactTextOnAllPages(
    replaceText: "REDACTED",
    caseSensitive: false,
    onlyMatchWholeWords: true,
    drawRectangles: true,
    replacementText: "[REMOVED]"
);

pdf.SaveAs("contract-redacted.pdf");
Enter fullscreen mode Exit fullscreen mode

Parameters:

  • caseSensitive: Match exact capitalization (default: false)
  • onlyMatchWholeWords: Don't redact partial matches (default: true)
  • drawRectangles: Draw black boxes (default: true)
  • replacementText: Text to show instead of redacted content (default: "*")

Can I Redact Patterns Like SSNs or Credit Cards?

Not directly, but you can extract text, find patterns with regex, and redact matches:

using IronPdf;
using System.Text.RegularExpressions;

var pdf = PdfDocument.FromFile("financial-data.pdf");

// Extract all text
string allText = pdf.ExtractAllText();

// Find all SSNs (###-##-####)
var ssnMatches = Regex.Matches(allText, @"\d{3}-\d{2}-\d{4}");

foreach (Match match in ssnMatches)
{
    pdf.RedactTextOnAllPages(match.Value);
}

// Find all credit cards (16 digits)
var ccMatches = Regex.Matches(allText, @"\d{16}");

foreach (Match match in ccMatches)
{
    pdf.RedactTextOnAllPages(match.Value);
}

pdf.SaveAs("sanitized.pdf");
Enter fullscreen mode Exit fullscreen mode

This redacts all SSNs and credit card numbers automatically.

How Do I Redact Regions by Coordinates?

Use RedactRegionsOnAllPages to black out specific areas:

using IronPdf;
using IronSoftware.Drawing;

var pdf = PdfDocument.FromFile("document.pdf");

// Define rectangle: (X, Y, Width, Height) in points
var region = new RectangleF(50, 700, 200, 30);

pdf.RedactRegionsOnAllPages(region);
pdf.SaveAs("region-redacted.pdf");
Enter fullscreen mode Exit fullscreen mode

Coordinates start at the bottom-left corner of the page. This is useful for redacting signatures, logos, or other non-text elements that appear in consistent locations.

Can I Redact Different Regions on Different Pages?

Yes. Use RedactRegionsOnPage:

using IronPdf;
using IronSoftware.Drawing;

var pdf = PdfDocument.FromFile("report.pdf");

// Redact signature on page 1
var signatureRegion = new RectangleF(100, 50, 150, 40);
pdf.RedactRegionsOnPage(signatureRegion, 0);

// Redact chart on page 3
var chartRegion = new RectangleF(50, 300, 500, 400);
pdf.RedactRegionsOnPage(chartRegion, 2);

pdf.SaveAs("custom-redacted.pdf");
Enter fullscreen mode Exit fullscreen mode

This gives precise control over what gets redacted where.

How Do I Batch Redact Multiple PDFs?

Loop through a directory and apply redactions:

using IronPdf;
using System.IO;

var files = Directory.GetFiles(@"C:\documents", "*.pdf");

foreach (var file in files)
{
    var pdf = PdfDocument.FromFile(file);

    // Redact client name
    pdf.RedactTextOnAllPages("Acme Corporation");

    // Redact SSNs
    var text = pdf.ExtractAllText();
    var ssns = System.Text.RegularExpressions.Regex.Matches(text, @"\d{3}-\d{2}-\d{4}");

    foreach (System.Text.RegularExpressions.Match ssn in ssns)
    {
        pdf.RedactTextOnAllPages(ssn.Value);
    }

    var outputPath = Path.Combine(@"C:\redacted", Path.GetFileName(file));
    pdf.SaveAs(outputPath);
}
Enter fullscreen mode Exit fullscreen mode

We process 10,000+ legal filings this way before public release.

Can I Redact Case-Insensitively?

Set caseSensitive: false:

var pdf = PdfDocument.FromFile("transcript.pdf");

// Redacts "confidential", "Confidential", "CONFIDENTIAL", etc.
pdf.RedactTextOnAllPages("confidential", caseSensitive: false);

pdf.SaveAs("redacted-transcript.pdf");
Enter fullscreen mode Exit fullscreen mode

This catches all variations without multiple redaction calls.

What About Partial Matches?

Set onlyMatchWholeWords: false to redact substrings:

var pdf = PdfDocument.FromFile("document.pdf");

// Redacts "secret", "secrets", "secretive", etc.
pdf.RedactTextOnAllPages("secret", onlyMatchWholeWords: false);

pdf.SaveAs("redacted.pdf");
Enter fullscreen mode Exit fullscreen mode

Be careful with this—it can redact unintended text. "secretary" would also be redacted in the example above.

How Do I Verify Redactions Worked?

Search the redacted PDF for the original text:

var redacted = PdfDocument.FromFile("redacted.pdf");
string text = redacted.ExtractAllText();

if (text.Contains("John Doe"))
{
    Console.WriteLine("WARNING: Redaction failed!");
}
else
{
    Console.WriteLine("Redaction successful.");
}
Enter fullscreen mode Exit fullscreen mode

I build this check into our pipeline. If redaction fails, the PDF never leaves the system.

Can I Redact Without Black Boxes?

Set drawRectangles: false and provide replacement text:

var pdf = PdfDocument.FromFile("report.pdf");

pdf.RedactTextOnAllPages(
    "Confidential Data",
    drawRectangles: false,
    replacementText: "[REDACTED]"
);

pdf.SaveAs("text-only-redacted.pdf");
Enter fullscreen mode Exit fullscreen mode

The text is replaced with "[REDACTED]" without black boxes. Useful for maintaining readability while removing sensitive content.

How Do I Handle Multi-Line Redactions?

Redact each line separately:

var pdf = PdfDocument.FromFile("letter.pdf");

var addressLines = new[]
{
    "John Doe",
    "123 Main Street",
    "New York, NY 10001"
};

foreach (var line in addressLines)
{
    pdf.RedactTextOnAllPages(line);
}

pdf.SaveAs("address-redacted.pdf");
Enter fullscreen mode Exit fullscreen mode

IronPDF redacts each instance of each line.

What Are the Limitations?

Scanned PDFs: Redaction only works on searchable text. Scanned documents (images) require OCR first.

Complex layouts: Tables with merged cells or unusual text rendering might require region-based redaction instead of text matching.

Performance: Redacting large documents (1,000+ pages) can take time. Consider async processing or splitting into smaller batches.

How Does IronPDF Compare to Alternatives?

Adobe Acrobat: Manual redaction prone to human error. IronPDF automates it.

iTextSharp: Can redact but requires creating redaction annotations manually. IronPDF's RedactTextOnAllPages is simpler.

Syncfusion/Aspose: Similar functionality but more expensive licensing for enterprise use.

I chose IronPDF because the API is straightforward and redaction is truly permanent—I've validated output with forensic PDF tools.

Can I Undo Redactions?

No. That's the point. Once text is redacted with IronPDF, it's permanently removed from the file structure.

Always keep an unredacted original in secure storage for record-keeping.

How Do I Test Redaction?

Create test PDFs with known sensitive data:

[Test]
public void RedactsSSNCompletely()
{
    var pdf = PdfDocument.FromFile("test-doc-with-ssn.pdf");

    pdf.RedactTextOnAllPages("123-45-6789");
    pdf.SaveAs("test-redacted.pdf");

    var redacted = PdfDocument.FromFile("test-redacted.pdf");
    string text = redacted.ExtractAllText();

    Assert.IsFalse(text.Contains("123-45-6789"));
}
Enter fullscreen mode Exit fullscreen mode

Run this as part of your CI/CD pipeline to ensure redaction logic works correctly.


Written by Jacob Mellor, CTO at Iron Software. Jacob created IronPDF and leads a team of 50+ engineers building .NET document processing libraries.

Top comments (0)