How to Redact Text and Regions

by Chaknith Bin

Redact text involves the process of permanently removing or obscuring sensitive or confidential information from a document. This is typically done by covering the text with a black box or using a tool to delete the text entirely. Redaction ensures that the information cannot be accessed or viewed, providing privacy and security for sensitive content.

Similarly, redacting a region obscures the specified areas on the document. This process requires a bit more work since the coordinates, width, and height of the region must be provided.


C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer

Check out IronPDF on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming PDF with C#.

C# NuGet Library for PDF nuget.org/packages/IronPdf/
Install-Package IronPdf

Consider installing the IronPDF DLL directly. Download and manually install it for your project or GAC form: IronPdf.zip

Manually install into your project

Download DLL

Redact Text Example

Text redaction can be easily accomplished with the help of IronPdf. Use the RedactTextOnAllPages method to remove the specified phrase from the entire document. Let's use a sample PDF.

:path=/static-assets/pdf/content-code-examples/how-to/redact-text-redact-text.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("novel.pdf");

// Redact 'Alaric' phrase from all pages
pdf.RedactTextOnAllPages("Alaric");

pdf.SaveAs("redacted.pdf");
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("novel.pdf")

' Redact 'Alaric' phrase from all pages
pdf.RedactTextOnAllPages("Alaric")

pdf.SaveAs("redacted.pdf")
VB   C#

Output PDF

Result PDF from redacting 'Alaric' phrase from all pages.

Use RedactTextOnPage and RedactTextOnPages methods to redact text from a single or multiple pages, respectively.

Here are the parameters of the redact text methods and their purposes:

  • ReplaceText: This is the text string that you want to redact.
  • CaseSensitive: A boolean value indicating whether the search should be case-sensitive. If true, it will match capital and lower-case letters exactly. The default is false.
  • OnlyMatchWholeWords: A boolean value specifying whether to match only whole words. The default is true.
  • DrawRectangles: A boolean value determining whether to draw black rectangles around the redacted areas. The default is true.
  • ReplacementText: This is the text that will be written in place of the redacted items. The default replacement text is "*".

Redact Regions Example

Redacting specific regions on the document works really well. Invoke the RedactRegionsOnAllPages method with the RectangleF object to redact region of the targeted document. Let's use the same PDF sample from the example above.

:path=/static-assets/pdf/content-code-examples/how-to/redact-text-redact-region.cs
using IronPdf;
using IronSoftware.Drawing;

PdfDocument pdf = PdfDocument.FromFile("novel.pdf");

RectangleF rectangle = new RectangleF(5, 700, 50, 50);

// Redact region on coordinates(5,700) with width and height 50 pixels
pdf.RedactRegionsOnAllPages(rectangle);

pdf.SaveAs("redactedRegion.pdf");
Imports IronPdf
Imports IronSoftware.Drawing

Private pdf As PdfDocument = PdfDocument.FromFile("novel.pdf")

Private rectangle As New RectangleF(5, 700, 50, 50)

' Redact region on coordinates(5,700) with width and height 50 pixels
pdf.RedactRegionsOnAllPages(rectangle)

pdf.SaveAs("redactedRegion.pdf")
VB   C#

Output PDF

The result PDF is from redacting region on the coordinates(5,700) with width and height of 50 pixels.

Use RedactRegionOnPage and RedactRegionOnPages methods to redact regions from a single or multiple pages, respectively.

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.