Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active February 10, 2023 20:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save conholdate-gists/cbc555b45fcfab5c422828bb5d743d37 to your computer and use it in GitHub Desktop.
Save conholdate-gists/cbc555b45fcfab5c422828bb5d743d37 to your computer and use it in GitHub Desktop.
Redact PDF Documents using C#
Redact PDF Documents using C#
1. C# API for PDF Redaction
2. Redact Text in PDF Documents using C#
3. Apply Metadata Redaction in PDF Documents using C#
4. Redact Annotation in PDF Documents using C#
// create redactor
Redactor redactor = new Redactor("C:\\Files\\sample.pdf");
// define multiple redactions
var redactionList = new Redaction[]
{
new ExactPhraseRedaction("John Doe", new ReplacementOptions("[Client]")),
new RegexRedaction("Redaction", new ReplacementOptions("[Product]")),
new RegexRedaction("\\d{2}\\s*\\d{2}[^\\d]*\\d{6}", new ReplacementOptions(System.Drawing.Color.Blue)),
new EraseMetadataRedaction(MetadataFilters.All)
};
// apply redactions
RedactorChangeLog result = redactor.Apply(redactionList);
// save if applied else show errors
if (result.Status == RedactionStatus.Applied)
{
redactor.Save();
}
else if (result.Status == RedactionStatus.Failed)
{
for (int i = 0; i < result.RedactionLog.Count; i++)
{
RedactorLogEntry logEntry = result.RedactionLog[i];
if (logEntry.Result.Status != RedactionStatus.Applied)
{
Console.WriteLine("{0} status is {1}, details: {2}",
logEntry.Redaction.GetType().Name,
logEntry.Result.Status,
logEntry.Result.ErrorMessage);
}
}
};
// create redactor
Redactor redactor = new Redactor("C:\\Files\\Redaction\\sample.pdf");
// create annotation redaction
AnnotationRedaction redaction = new AnnotationRedaction("(?im:john)", "[redacted]");
// apply redaction
redactor.Apply(redaction);
redactor.Save();
// create redactor
Redactor redactor = new Redactor("C:\\Files\\sample_with_images.pdf");
// define size and points
System.Drawing.Point samplePoint = new System.Drawing.Point(0, 0);
System.Drawing.Size sampleSize = new System.Drawing.Size(300, 240);
// define image area redaction
ImageAreaRedaction redaction = new ImageAreaRedaction(samplePoint,
new RegionReplacementOptions(System.Drawing.Color.Blue, sampleSize));
// apply redaction
RedactorChangeLog result = redactor.Apply(redaction);
if (result.Status != RedactionStatus.Failed)
{
redactor.Save();
};
// create redactor
Redactor redactor = new Redactor("C:\\Files\\sample.pdf");
// erase author, manager and company
EraseMetadataRedaction redaction = new EraseMetadataRedaction(MetadataFilters.Author | MetadataFilters.Manager | MetadataFilters.Company);
// apply redaction
redactor.Apply(redaction);
redactor.Save();
// create redactor
Redactor redactor = new Redactor("C:\\Files\\sample.pdf");
// create exact phrase redaction
ExactPhraseRedaction redaction = new ExactPhraseRedaction("John Doe", true, new ReplacementOptions("[personal]"));
// apply redaction
RedactorChangeLog result = redactor.Apply(redaction);
if (result.Status != RedactionStatus.Failed)
{
redactor.Save();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment