Use redaction policies
Leave feedback
On this page
If you have a corporate sensitive data removal policy as a list of redaction rules, you don’t need to specify them in your code. You can specify an XML document with a list of pre-configured redactions.
Below is an example of redaction policy XML file (code properties mapping is obvious):
RedactionPolicy.xml
<?xml version="1.0" encoding="utf-8"?>
<redactionPolicy xmlns="http://www.groupdocs.com/redaction">
<regexRedaction regularExpression="(dolor)" actionType="ReplaceString" replacement="foobar" />
<exactPhraseRedaction searchPhrase="dolor" caseSensitive="true" actionType="DrawBox" color="Red" />
<cellColumnRedaction regularExpression="(foo)bar1" replacement="[red1]" columnIndex="1" worksheetIndex="2" />
<cellColumnRedaction regularExpression="(foo)bar2" replacement="[red2]" wokrsheetName="Sample" />
<eraseMetadataRedaction filter="All" />
<metadataSearchRedaction filter="Title, Author" replacement="foobar" valueExpression="(metasearch)" keyExpression="" />
<annotationRedaction regularExpression="(anno1)" replacement="foobar" />
<deleteAnnotationRedaction regularExpression="(anno2)" />
<imageAreaRedaction pointX="15" pointY="17" width="200" height="10" color="#AA50FC" />
<imageAreaRedaction pointX="110" pointY="120" width="60" height="20" color="Magenta" />
</redactionPolicy>
You can use RedactionPolicy.save() method to create XML documents of this structure, configuring redactions in runtime.
The following example demonstrates how to save a RedactionPolicy to an XML file.
RedactionPolicy policy = new RedactionPolicy(new Redaction[] {
new ExactPhraseRedaction("Redaction", new ReplacementOptions("[Product]")),
new RegexRedaction("\\d{2}\\s*\\d{2}[^\\d]*\\d{6}", new ReplacementOptions(java.awt.Color.BLUE)),
new DeleteAnnotationRedaction(),
new EraseMetadataRedaction(MetadataFilters.All)
});
policy.save("sample_policy.xml");
You can have as much policies, as you need, loading them to redact your documents.
The example below loads a redaction policy from an XML file and applies every rule it contains to a document in a single apply call:
RedactionPolicy policy = RedactionPolicy.load("redaction_policy.xml");
final Redactor redactor = new Redactor("sample.docx");
try
{
RedactorChangeLog result = redactor.apply(policy);
if (result.getStatus() != RedactionStatus.Failed)
{
SaveOptions saveOptions = new SaveOptions();
saveOptions.setAddSuffix(true);
saveOptions.setRasterizeToPDF(false);
saveOptions.setRedactedFileSuffix("redacted");
redactor.save(saveOptions);
}
}
finally { redactor.close(); }
redaction_policy.xml is the policy file used in this example. Click here to download it.
sample.docx is the document redacted in this example. Click here to download it.
Binary file (DOCX, 17 KB)
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.