Redacting PDFs programmatically on Android
You can create redactions programmatically via RedactionAnnotation
. Use the setRects()
method to set the regions that should be covered by the redaction annotation.
You also have a few customization options for how a redaction should look, both while in its marked state, which is when the redaction has been created but not yet applied, and in its redacted state, which is when the redaction has been applied. It isn’t possible to change the appearance once a redaction has been applied, since the redaction annotation will be removed from the document in the process of applying the redactions. Here’s a list of available customization options for redactions:
-
setOverlayText()
can be used to set the text that should be displayed at the specified region when a redaction has been applied. -
setRepeatOverlayText()
defines whether the overlay text should be drawn only once or repeated to fill the entire redaction area. This defaults to disabled, which means the overlay text is only drawn once. It has no effect if there’s no overlay text specified. -
setColor()
can be used to change the color of the overlay text. It has no effect if there’s no overlay text specified. This defaults toColor.RED
. -
setFillColor()
specifies the background color of the redaction area after it has been applied. The color is drawn on all the specifiedrects
. This defaults toColor.BLACK
. -
setOutlineColor()
specifies the color used for the redaction’s border in its marked state. This defaults toColor.RED
.
val text = "Guide" val textPosition = document.getPageText(pageIndex).indexOf(text) if (textPosition == 0) { return } val textRects = document.getPageTextRects(pageIndex, textPosition, text.length, false) val redaction = RedactionAnnotation(0, textRects) redaction.color = Color.MAGENTA redaction.fillColor = Color.BLACK redaction.outlineColor = Color.YELLOW redaction.overlayText = "REDACTED" pdfFragment.addAnnotationToPage(redaction, false)
String text = "Guide"; final int textPosition = document.getPageText(pageIndex).indexOf(text); if (textPosition == 0) { return; } final List<RectF> textRects = document.getPageTextRects(pageIndex, textPosition, text.length(), false); RedactionAnnotation redaction = new RedactionAnnotation(0, textRects); redaction.setColor(Color.MAGENTA); redaction.setFillColor(Color.BLACK); redaction.setOutlineColor(Color.YELLOW); redaction.setOverlayText("REDACTED"); getPdfFragment().addAnnotationToPage(redaction, false);
The redaction annotation created with the above code snippet would look like what’s shown in the image below.
Applying redactions
There are two separate options for applying redactions programmatically.
-
Use
PdfProcessorTask
. This creates a new document and will leave the original document with the redaction annotations untouched:
val outputFile: File val document: PdfDocument val processorTask = PdfProcessorTask.fromDocument(document) .applyRedactions() PdfProcessor.processDocument(processorTask, outputFile) val redactedDocument = PdfDocumentLoader.openDocument(this, Uri.fromFile(outputFile))
File outputFile;
PdfDocument document;
PdfProcessorTask processorTask = PdfProcessorTask.fromDocument(document)
.applyRedactions();
PdfProcessor.processDocument(processorTask, outputFile);
PdfDocument redactedDocument = PdfDocumentLoader.openDocument(this, Uri.fromFile(outputFile));
-
Use the
DocumentSaveOptions#setApplyRedactions()
option when saving the document via any of the save methods onPdfDocument
. This will overwrite the existing document, removing content irreversibly:
val documentSaveOptions = DocumentSaveOptions(null, null, true, null); documentSaveOptions.setApplyRedactions(true) document.saveIfModified(documentSaveOptions)
DocumentSaveOptions documentSaveOptions = new DocumentSaveOptions(null, null, false, null); documentSaveOptions.setApplyRedactions(true); document.saveIfModified(documentSaveOptions);
Both options will remove the redaction annotations in the process of redacting the content.
Previewing redactions
To preview redactions and see how they’d look when applied, without removing any document content, you can use the PdfFragment#setRedactionAnnotationPreviewEnabled()
method:
pdfFragment.isRedactionAnnotationPreviewEnabled = true
pdfFragment.setRedactionAnnotationPreviewEnabled(true);