If format is not supported, you will need to implement a handler for it by inheriting from DocumentFormatInstance class. Depending on the document’s features and required redactions, you will also need to implement one or several interfaces, allowing GroupDocs.Redaction to work with this document format.
Interface
Description
ITextualFormatInstance
Required for document text redactions to work, replaces occurrences of given regular expression with text or a color block
IMetadataAccess
Required for metadata redactions, reads metadata and changes specific metadata item
IAnnotatedDocument
Required for annotation redactions, changes or deletes annotations, matching given regular expression
IRasterizableDocument
Required to rasterize (save document as a PDF with page images)
IImageFormatInstance
Required for raster image format redactions, based on area top-left corner coordinates and area size
IPreviewable
Required to provide document general information and preview functionality
Each of these interfaces is optional, i.e. you don’t have to implement all of them, e.g. IImageFormatInstance - if you don’t need its functionality or IMetadataAccess, if your format does not support metadata.
Below, we create a DocumentFormatInstance class with custom logic for textual documents processing, supporting only textual redactions:
importcom.groupdocs.redaction.options.RedactorSettings;publicclassCustomTextualDocumentextendsDocumentFormatInstanceimplementsITextualFormatInstance{privateRedactorSettings_settings;privatefinaljava.util.List<String>_fileContent;publicCustomTextualDocument(){_fileContent=newjava.util.ArrayList<>();}@Overridepublicvoidinitialize(DocumentFormatConfigurationconfig,RedactorSettingssettings){_settings=settings;}@Overridepublicvoidload(java.io.InputStreaminput)throwsjava.lang.Exception{_fileContent.clear();java.io.BufferedReaderreader=newjava.io.BufferedReader(newjava.io.InputStreamReader(input));Stringline=reader.readLine();while(line!=null){_fileContent.add(line);line=reader.readLine();}reader.close();}@Overridepublicvoidsave(java.io.OutputStreamoutput)throwsjava.lang.Exception{java.io.BufferedWriterwriter=newjava.io.BufferedWriter(newjava.io.OutputStreamWriter(output));for(Stringline:_fileContent){writer.write(line);}writer.close();}@OverridepublicRedactionResultreplaceText(java.util.regex.Patternregex,ReplacementOptionsoptions){try{if(options.getActionType()!=ReplacementType.ReplaceString){returnRedactionResult.failed("This format allows only ReplaceString redactions!");}for(inti=0;i<_fileContent.size();i++){_fileContent.set(i,regex.matcher(_fileContent.get(i)).replaceAll(options.getReplacement()));}returnRedactionResult.successful();}catch(java.lang.Exceptionex){returnRedactionResult.failed(ex.getMessage());}}}
In order to use this class, we will need to add it to pre-configured formats, e.g. as a handler for dump files ("*.dump"):
If your format supports security options like password protection, you’ll have to pass true or false to SetAccessGranted method of DocumentFormatInstance class in your override of Load method and throw IncorrectPasswordException or PasswordRequiredException, if applicable. For instance:
@Overridepublicvoidload(java.io.InputStreaminput)throwsjava.lang.Exception{try{// check security and load document
setAccessGranted(true);}catch(SomeSecurityExceptionex){setAccessGranted(false);// Password, provided with LoadOptions
if(string.IsNullOrEmpty(Password))thrownewExceptions.PasswordRequiredException();elsethrownewExceptions.IncorrectPasswordException();}}
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.