Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Created January 14, 2020 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GroupDocsGists/ac47e2a0191e8f8539932cd555edc67f to your computer and use it in GitHub Desktop.
Save GroupDocsGists/ac47e2a0191e8f8539932cd555edc67f to your computer and use it in GitHub Desktop.
// For complete examples and data files, please go to https://github.com/groupdocs-redaction/GroupDocs.Redaction-for-.NET
public class PlainTextDocument : DocumentFormatInstance, ITextualFormatInstance
{
private RedactorSettings Settings { get; set; }
private List<string> FileContent { get; set; }
public PlainTextDocument()
{
FileContent = new List<string>();
}
public override void Initialize(DocumentFormatConfiguration config, RedactorSettings settings)
{
Settings = settings;
}
public override void Load(System.IO.Stream input)
{
FileContent.Clear();
using (var reader = new StreamReader(input))
{
string line = string.Empty;
do
{
line = reader.ReadLine();
if (!string.IsNullOrEmpty(line))
{
FileContent.Add(line);
}
}
while (!string.IsNullOrEmpty(line));
reader.Close();
}
}
public override void Save(System.IO.Stream output)
{
using (var writer = new StreamWriter(output))
{
foreach (var line in FileContent)
{
writer.WriteLine(line);
}
writer.Close();
}
}
public RedactionResult ReplaceText(Regex regex, Redactions.ReplacementOptions options)
{
try
{
if (options.ActionType != Redactions.ReplacementType.ReplaceString)
{
return new RedactionResult.Failed("This format allows only ReplaceString redactions!");
}
for (int i = 0; i < FileContent.Count; i++)
{
FileContent[i] = regex.Replace(FileContent[i], options.Replacement);
}
return RedactionResult.Successful();
}
catch (Exception ex)
{
return RedactionResult.Failed(ex.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment