GroupDocs.Redaction allows to easily redact data of sensitive or private nature from your documents. The most popular redaction case is to remove a text from a document.
With GroupDocs.Redaction API you can apply text redaction using exact phrase or regular expression for documents of different formats like PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and others. See full list at supported document formats article.
Use exact phrase redaction
In the example below, we apply textual redaction, replacing personal exact phrase “John Doe” with “[personal]” (or any exemption code):
Python
importgroupdocs.redactionasgrimportgroupdocs.redaction.optionsasgroimportgroupdocs.redaction.redactionsasgrrdefrun():# Specify the redaction optionsrepl_opt=grr.ReplacementOptions("[personal]")ex_red=grr.ExactPhraseRedaction("John Doe",repl_opt)# Load the document to be redactedwithgr.Redactor("source.pdf")asredactor:# Apply the redactionresult=redactor.apply(ex_red)# Save the redacted documentso=gro.SaveOptions()so.add_suffix=Trueso.rasterize_to_pdf=Falseresult_path=redactor.save(so)
By default, search for exact phase is case insensitive. For a case-sensitive redaction, there is a constructor parameter and corresponding public property:
Python
importgroupdocs.redactionasgrimportgroupdocs.redaction.optionsasgroimportgroupdocs.redaction.redactionsasgrrdefrun():# Specify the redaction optionscase_sensitive=Truerepl_opt=grr.ReplacementOptions("[personal]")ex_red=grr.ExactPhraseRedaction("John Doe",case_sensitive,repl_opt)# Load the document to be redactedwithgr.Redactor("source.docx")asredactor:# Apply the redactionresult=redactor.apply(ex_red)# Save the redacted documentso=gro.SaveOptions()so.add_suffix=Trueso.rasterize_to_pdf=Falseresult_path=redactor.save(so)
If you need a color box over the redacted text, you can use color instead of replacement string. The redaction will erase matched text and put a rectangle of the specified color in place of redacted text:
Python
importgroupdocs.redactionasgrimportgroupdocs.redaction.redactionsasgrrimportgroupdocs.pydrawingasgrddefrun():# Specify the redaction optionscolor=grd.Color.from_argb(255,220,20,60)repl_opt=grr.ReplacementOptions(color)ex_red=grr.ExactPhraseRedaction("John Doe",repl_opt)# Load the document to be redactedwithgr.Redactor("source.pdf")asredactor:# Apply the redactionresult=redactor.apply(ex_red)# Save the redacted documentredactor.save()
You might need to apply redaction to a right-to-left document, such as Arabic or Hebrew. The following example demonstrates how to apply ExactPhraseRedaction to an Arabic PDF document:
Python
importgroupdocs.redactionasgrimportgroupdocs.redaction.redactionsasgrrdefrun():# Specify the redaction optionsrepl_opt=grr.ReplacementOptions("[test]")ex_red=grr.ExactPhraseRedaction("أﺑﺠﺪ",repl_opt)# Load the document to be redactedwithgr.Redactor("source.pdf")asredactor:# Apply the redactionresult=redactor.apply(ex_red)# Save the redacted documentredactor.save()
Use regular expression
Behind the scenes, “exact phrase” redaction works though regular expressions, which are the baseline approach for redaction. In the example below, we redact out any text, matching “2 digits, space or nothing, 2 digits, again space and 6 digits” with a blue color box:
Python
importgroupdocs.redactionasgrimportgroupdocs.redaction.optionsasgroimportgroupdocs.redaction.redactionsasgrrimportgroupdocs.pydrawingasgrddefrun():# Specify the redaction optionscolor=grd.Color.from_argb(255,220,20,60)repl_opt=grr.ReplacementOptions(color)reg_red=grr.RegexRedaction("\\d{2}\\s*\\d{2}[^\\d]*\\d{6}",repl_opt)# Load the document to be redactedwithgr.Redactor("sample.docx")asredactor:# Apply the redactionresult=redactor.apply(reg_red)# Save the redacted documentso=gro.SaveOptions()so.add_suffix=Trueso.rasterize_to_pdf=Falseresult_path=redactor.save(so)
If you need to apply redact a whole paragraph, you might also need to use RegexRedaction. The following example demonstrates how to redact the whole paragraph in a PDF document:
Python
importgroupdocs.redactionasgrimportgroupdocs.redaction.redactionsasgrrimportgroupdocs.pydrawingasgrddefrun():# Specify the redaction optionscolor=grd.Color.from_argb(255,220,20,60)repl_opt=grr.ReplacementOptions(color)ex_red=grr.RegexRedaction("(Lorem(\n|.)+?urna)",repl_opt)# Load the document to be redactedwithgr.Redactor("sample.pdf")asredactor:# Apply the redactionresult=redactor.apply(ex_red)# Save the redacted documentredactor.save()
More resources
Advanced usage topics
To learn more about document redaction features, please refer to the advanced usage section.
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples:
Along with full featured .NET library we provide simple, but powerful free Apps.
You are welcome to perform redactions for various document formats like PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Redaction App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.