PNG
JPG
BMP
TIFF
TXT
Redact TXT Formats in C#
Native and high performance TXT document sensitive redaction information using server-side .NET APIs, without the use of any software like Microsoft or Adobe PDF.
How to Redact TXT File Using C#
In order to redact TXT file, we’ll use
API which is a feature-rich, powerful and easy to use document manipulation API for C# platform. Open
package manager, search for Aspose.Words and install. You may also use the following command from the Package Manager Console.
Command
PM> Install-Package Aspose.Words
Steps to Redact TXT Files in C#
A basic document search and replace text in contents, comments or metadata using API can be done with just few lines of code. Redact sensitive information through search and replace text in contents, comments or metadata in Word documents.
- Load TXT file.
- Create FindReplaceOptions object.
- Set Search Options
- Highlight phrases using Regex.
- Use Replace method with relevant options.
- Save document.
System Requirements
Our APIs are supported on all major platforms and Operating Systems. Before executing the code below, please make sure that you have the following prerequisites on your system.
- Microsoft Windows or a compatible OS with .NET Framework, .NET Core, Windows Azure, Mono or Xamarin Platforms
- Development environment like Microsoft Visual Studio
- Aspose.Words for .NET DLL referenced in your project - Install from NuGet using the Download button above
Redact TXT Files - C#
string fileName = "TestFile.doc";
Document doc = new Document(dataDir + fileName);
FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = new ReplaceEvaluatorFindAndHighlight();
options.Direction = FindReplaceDirection.Backward;
// We want the "your document" phrase to be highlighted.
Regex regex = new Regex("your document", RegexOptions.IgnoreCase);
doc.Range.Replace(regex, "", options);
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the output document.
doc.Save(dataDir);
// For ReplaceEvaluatorFindAndHighlight class check the detail here
//https://docs.aspose.com/words/net/find-and-replace/#how-to-find-and-highlight-text
.NET Words API can be used to load, view and convert Microsoft Word and OpenDocument Formats like DOC, DOCX, ODT to PDF, XPS, HTML and various other formats. You can also create new documents from scratch and save them in the supported formats. It is a standalone API that is suitable for server side and backend systems where high performance is required. It does not depend on any software like Microsoft or OpenOffice.
Online TXT Redaction Live Demos
Other Supported Redaction Formats
Using C#, one can easily redact different formats including.
DOC (Microsoft Word Binary Format)
DOCX (Office 2007+ Words Document)
DOT (Microsoft Word Template Files)
DOTX (Microsoft Word Template File)
HTML (Hyper Text Markup Language)
MD (Markdown Language)
MHTML (Web Page Archive Format)
ODT (OpenDocument Text File Format)
OTT (OpenDocument Standard Format)
RTF (Rich Text Format)
XHTML (XML Text Based Markup)