PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2005/2008/2010/2012/2013

PDF Text Redaction Using PDFOne .NET

Removing unwanted text from a document.
By V. Subhash

In Version 4 of PDFOne .NET, we have introduced text redaction. You can redact text by specifying a region to be redacted. You can also provide a text string whose occurrences in the document can be redacted. The text string can be a literal search text or it can be a regular expression. In this article, we will see examples of both.

Simple Text Redaction

The following code snippet shows how to perform simple text redaction using literal search text.

PDFDocument PDFDocument1 = new PDFDocument("your-license-key");

// Load PDF document
PDFDocument1.Load("sample_doc1.pdf");

PDFDocument1.RedactText(
    // redact in page with number
    1,
    // redact text matching "gnostice"
    "gnostice",
    // search for literal matches
    PDFSearchMode.LITERAL,
    // match all cases and instances
    PDFSearchOptions.NONE,
    // stroke redacted regions in red color
    new Pen(Color.Red),
    // fill redacted region in lime color
    new SolidBrush(Color.Lime),
    // enable stroking of redacted regions
    true,
    // enable filling of redacted regions
    true);

PDFDocument1.Save("redacted_doc1.pdf");

// Close the document
PDFDocument1.Close();
Original Document and the Redacted Version

Advanced Text Redaction

You can also use regular expressions with the redactText method. I have modified the above code snippet so that it will redact all phone numbers, like this:

PDFDocument1.RedactText(
  // redact in page with number 2
  17,
  // redact phone numbers
  @"\(\d{3}\)\s\d{3}-\d{4}",
  // search for regular expression matches
  PDFSearchMode.REGEX,
  // match all cases and instances
  PDFSearchOptions.NONE,
  // stroke redacted regions in red color
  new Pen(Color.Red),
  // fill redacted region in lime color
  new SolidBrush(Color.Lime),
  // enable stroking of redacted regions
  true,
  // enable filling of redacted regions
  true);
Original Document and the Redacted Version

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
XtremeDocumentStudio (for Java)

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2017 Gnostice Information Technologies Private Limited. All rights reserved.