You can redact PDF documents programmatically without installing any external applications. As a C# developer, you can easily redact your PDF documents in your .NET applications. This article will be focusing on how to redact PDF documents using C#.
The following topics are discussed/covered in this article:
- C# API for PDF Redaction
- Redact Text in PDF using C#
- Metadata Redaction in PDF using C#
- Redact Images in PDF using C#
- Apply Multiple Redactions in PDF using C#
C# API for PDF Redaction
For redaction in PDF documents, I will be using GroupDocs.Redaction for .NET API. It allows you to redact PDF, Word, Excel, PowerPoint, and image files. It also enables you to remove the classified information from over 30 types of supported formats. You can apply various types of redaction such as text redaction, metadata redaction, annotation redaction, and tabular document redaction.
You can either download the DLL of the API or install it using the NuGet.
Install-Package GroupDocs.Redaction
Redact Text in PDF using C#
You can easily apply text redaction in PDF documents by following the simple steps mentioned below:
- Create an instance of the Redactor class with the input file path
- Create the ExactPhraseRedaction class instance with SearchPhrase and the ReplacementOptions
- Call the Redactor.Apply() method
- Get results in the RedactorChangeLog class object
- Call the Redactor.Save() method
The following code sample shows how to redact text in a PDF document using C#.
The Redactor is the main class provides various methods to perform the document redaction process. It also enables you to open, redact and save documents. The Apply() method of this class applies the defined redaction to the document. Moreover, the Save() method of this class saves the document to a file.
The ExactPhraseRedaction provides methods to perform a text redaction to replace an exact phrase in the document. It also allows to search case sensitive data by setting the IsCaseSensitive to true.
The ReplacementOptions represents options for matched text replacement.
The RedactorChangeLog class represents results for a list of redactions, passed to Apply() method of Redactor class.
Metadata Redaction in PDF using C#
You can apply metadata redaction in PDF documents by following the simple steps mentioned below:
- Create an instance of the Redactor class with the input file path
- Create the EraseMetadataRedaction class instance with MetadataFilter to include
- Call the Redactor.Apply() method
- Call the Redactor.Save() method
The following code sample shows how to redact metadata in a PDF document using C#.
The EraseMetadataRedaction class provides methods to erase all the metadata. It also enable to erase metadata matching specific MetadataFilters from the document.
The MetadataFilters is a list of the most common types of document metadata such as Author, Comments, Company.
Redact Images in PDF using C#
You can apply image redaction in PDF documents by following the simple steps mentioned below:
- Create an instance of the Redactor class with the input file path
- Define drawing points and size
- Create the ImageAreaRedaction class instance with drawing points and RegionReplacementOptions
- Call the Redactor.Apply() method
- Call the Redactor.Save() method
The following code sample shows how to redact images in a PDF document using C#.
The ImageAreaRedaction class allows to place a colored rectangle in given area of an image document.
The RegionReplacementOption class represents the color and area parameters for a region to be replaced with image.
Apply Multiple Redactions in PDF using C#
You can apply multiple redactions in PDF documents by following the simple steps mentioned below:
- Create an instance of the Redactor class with the input file path
- Create the ExactPhraseRedaction, RegexRedaction, and EraseMetadataRedaction
- Add created redactions to the redaction list
- Call the Redactor.Apply() method
- Call the Redactor.Save() method, show errors if failed
The following code sample shows how to apply multiple redactions in a PDF document using C#.
The RegexRedaction class allows to perform a text redaction. You can search and replace any text in the document by matching a text using regular expression.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to redaction PDF documents using C#. You have also learned how to redact Text, Metadata and Images in PDF documents. Moreover, you have learned how to apply multiple redactions in PDF using C#. You can learn more about GroupDocs.Redaction for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.