Redactor Class
Represents a main class that controls document redaction process, allowing to open, redact and save documents.
Inheritance Hierarchy
SystemObject
  GroupDocs.RedactionRedactor

Namespace: GroupDocs.Redaction
Assembly: GroupDocs.Redaction (in GroupDocs.Redaction.dll) Version: 20.7
Syntax
public sealed class Redactor : IDisposable

The Redactor type exposes the following members.

Constructors
  NameDescription
Public methodCode exampleRedactor(Stream)
Initializes a new instance of Redactor class using stream.
Public methodCode exampleRedactor(String)
Initializes a new instance of Redactor class using file path.
Public methodCode exampleRedactor(Stream, LoadOptions)
Initializes a new instance of Redactor class for a password-protected document using stream.
Public methodRedactor(String, LoadOptions)
Initializes a new instance of Redactor class for a password-protected document using its path.
Public methodCode exampleRedactor(Stream, LoadOptions, RedactorSettings)
Initializes a new instance of Redactor class for a password-protected document using stream and settings.
Public methodRedactor(String, LoadOptions, RedactorSettings)
Initializes a new instance of Redactor class for a password-protected document using its path and settings.
Methods
  NameDescription
Public methodCode exampleApply(Redaction)
Applies a redaction to the document.
Public methodCode exampleApply(Redaction)
Applies a set of redactions to the document.
Public methodCode exampleApply(RedactionPolicy)
Applies a redaction policy to the document.
Public methodDispose
Releases resources.
Public methodEquals (Inherited from Object.)
Public methodCode exampleGeneratePreview
Generates preview images of specific pages in a given image format.
Public methodCode exampleGetDocumentInfo
Gets the general information about the document - size, page count, etc.
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodSave
Saves the document to a file with the following options: AddSuffix = true, RasterizeToPDF = true.
Public methodCode exampleSave(SaveOptions)
Saves the document to a file.
Public methodCode exampleSave(Stream, RasterizationOptions)
Saves the document to a stream, including custom location.
Public methodToString (Inherited from Object.)
Remarks
Learn more
Examples

The following example demonstrates applying a single redaction to the document.

C#
using (Redactor redactor = new Redactor(@"D:\\test.docx"))
{
   RedactorChangeLog result = redactor.Apply(new RegexRedaction(LookupStrings.SSNRegexPattern, new ReplacementOptions("[ssn]")));
   if (result.Status != RedactionStatus.Failed)
   {
      redactor.Save();
   };
}

The following example demonstrates applying a list of redactions to the document.

C#
using (Redactor redactor = new Redactor(@"D:\\test.docx"))
{
   var redactionList = new Redaction[] 
   {
      new ExactPhraseRedaction(LookupStrings.ClientName, new ReplacementOptions("[client]")),
      new ExactPhraseRedaction(LookupStrings.ClientAddress, new ReplacementOptions(System.Drawing.Color.Red)),
      new RegexRedaction(LookupStrings.SSNRegexPattern, new ReplacementOptions("[ssn]")),
      new RegexRedaction(LookupStrings.BankCardRegexPattern, new ReplacementOptions(System.Drawing.Color.Blue)),
      // ... other redactions
      new DeleteAnnotationRedaction("(?im:(use|show|describe))"),
      new EraseMetadataRedaction(MetadataFilter.Author),
      new MetadataSearchRedaction(LookupStrings.CompanyName, "--company--") 
   }; 
   RedactorChangeLog result = redactor.Apply(redactionList);
   // false, if at least one redaction failed
   if (result.Status != RedactionStatus.Failed)
   {
      redactor.Save();
   };
}

The following example demonstrates how to apply a redaction policy to all files within a given inbound folder, and save to one of outbound folders - for successfully updated files and for failed ones.

C#
RedactionPolicy policy = RedactionPolicy.Load("RedactionPolicy.xml");
foreach (var fileEntry in Directory.GetFileNames("C:\\Inbound")) 
{
     using (Redactor redactor = new Redactor(Path.Combine("C:\\Inbound\\", fileEntry)))
     {
             RedactorChangeLog result = redactor.Apply(policy);
             String resultFolder = result.Status != RedactionStatus.Failed ? "C:\\Outbound\\Done\\" : "C:\\Outbound\\Failed\\";
             using (Stream fileStream = File.Open(Path.Combine(resultFolder, fileEntry), FileMode.Open, FileAccess.ReadWrite))
            {
               redactor.Save(fileStream, new RasterizationOptions() { Enabled = false });
            }        
     }
}

The following example demonstrates how to open a password-protected documents using LoadOptions.

C#
LoadOptions loadOptions = new LoadOptions("mypassword");
using (Redactor redactor = new Redactor(@"C:\sample.pdf", loadOptions))
{
    // Here we can use document instance to perform redactions
}

The following example demonstrates how to save a document using SaveOptions.

C#
using (Redactor redactor = new Redactor(@"C:\sample.pdf"))
{
   // Document redaction goes here
   // ...

   // Save the document with default options (convert pages into images, save as PDF)
   redactor.Save();

   // Save the document in original format overwriting original file
   redactor.Save(new SaveOptions() { AddSuffix = false, RasterizeToPDF = false });

   // Save the document to "*_Redacted.*" file in original format
   redactor.Save(new SaveOptions() { AddSuffix = true, RasterizeToPDF = false });

   // Save the document to "*_AnyText.*" (e.g. timestamp instead of "AnyText") in its file name without rasterization
   redactor.Save(new SaveOptions(false, "AnyText"));
}
See Also