Redact text

<< Click to Display Table of Contents >>

Navigation:  Gnostice Document Studio .NET > Going Deeper > Document Engines > Working with PDF > Processing >

Redact text

In this section we present example code to redact text from a PDF document. Before proceeding further please make sure that the prerequisite steps as listed in the Document Engines parent topic are followed.

 

Given below is the sample code that shows how to redact literal text and, optionally, replace the region with a colored rectangle.

 

using (var pdfDoc = new PDF())
{
   pdfDoc.LoadDocument(@"statement.pdf");
   pdfDoc.RedactText(pageRange: "1-", searchString: "sensitive", searchMode: PDFSearchMode.LITERAL, searchOptions: PDFSearchOptions.NONE, 
      pen: new Pen(Color.Red), new SolidBrush(Color.Gray), isFill: true, isStroke: true);
   pdfDoc.Save("redacted.pdf");
}

 

Given below is the sample code that shows how to search for text using regular expression and then redact it.

 

using (var pdfDoc = new PDF())
{
   pdfDoc.LoadDocument(@"statement.pdf");
   // Redact all phone numbers
   pdfDoc.RedactText(pageRange: "1-", searchString: @"\(\d{3}\)\s\d{3}-\d{4}", searchMode: PDFSearchMode.REGEX, searchOptions: PDFSearchOptions.NONE);
   pdfDoc.Save("redacted.pdf");
}