Redacting PDFs programmatically on Android
Create redactions programmatically with RedactionAnnotation. Use setRects() to specify the regions to redact.
A redaction has two states. In the marked state, the redaction annotation is in the document and the content is intact. In the redacted state, the redaction has removed the content.
Change the appearance only in the marked state. Applying redactions deletes the redaction annotation, so you can’t change its appearance afterward.
These methods control the appearance of a redaction:
- Overlay text —
setOverlayText()specifies the text to draw in the redacted area. - Text repetition —
setRepeatOverlayText()repeats overlay text to fill the area. The default,false, draws the text once. This setting has no effect without overlay text. - Text color —
setColor()sets the overlay text color. It defaults toColor.RED(opens in a new tab) and has no effect without overlay text. - Fill color —
setFillColor()fills each rectangle inrects. It defaults toColor.BLACK(opens in a new tab). - Outline color —
setOutlineColor()sets the marked-state border color. It defaults toColor.RED(opens in a new tab).
The following example creates and styles a redaction annotation:
val text = "Guide"val textPosition = document.getPageText(pageIndex).indexOf(text)if (textPosition == -1) { return}val textRects = document.getPageTextRects(pageIndex, textPosition, text.length, false)
val redaction = RedactionAnnotation(pageIndex, textRects)redaction.color = Color.MAGENTAredaction.fillColor = Color.BLACKredaction.outlineColor = Color.YELLOWredaction.overlayText = "REDACTED"
requirePdfFragment().addAnnotationToPage(redaction, false)String text = "Guide";final int textPosition = document.getPageText(pageIndex).indexOf(text);if (textPosition == -1) { return;}final List<RectF> textRects = document.getPageTextRects(pageIndex, textPosition, text.length(), false);
RedactionAnnotation redaction = new RedactionAnnotation(pageIndex, textRects);redaction.setColor(Color.MAGENTA);redaction.setFillColor(Color.BLACK);redaction.setOutlineColor(Color.YELLOW);redaction.setOverlayText("REDACTED");
getPdfFragment().addAnnotationToPage(redaction, false);The code above creates the redaction annotation shown below.
| Marked State | Redacted State |
|---|---|
![]() | ![]() |
Applying redactions
Two options redact content programmatically, and they differ in what happens to the original document:
Use PdfProcessorTask to create a new document without changing the original document or its redaction annotations:
val outputFile = File(filesDir, "redacted.pdf")val processorTask = PdfProcessorTask.fromDocument(document) .applyRedactions()
PdfProcessor.processDocument(processorTask, outputFile)val redactedDocument = PdfDocumentLoader.openDocument(this, Uri.fromFile(outputFile))File outputFile = new File(getFilesDir(), "redacted.pdf");PdfProcessorTask processorTask = PdfProcessorTask.fromDocument(document) .applyRedactions();
PdfProcessor.processDocument(processorTask, outputFile);try { PdfDocument redactedDocument = PdfDocumentLoader.openDocument(this, Uri.fromFile(outputFile));} catch (IOException e) { // Handle the error.}Alternatively, enable DocumentSaveOptions#setApplyRedactions(), and save the document with saveIfModified() on PdfDocument.
This approach overwrites the existing document and permanently removes the redacted content.
The following example applies redactions while saving:
val documentSaveOptions = document.defaultDocumentSaveOptionsdocumentSaveOptions.isIncremental = falsedocumentSaveOptions.setApplyRedactions(true)document.saveIfModified(documentSaveOptions)DocumentSaveOptions documentSaveOptions = document.getDefaultDocumentSaveOptions();documentSaveOptions.setIncremental(false);documentSaveOptions.setApplyRedactions(true);try { document.saveIfModified(documentSaveOptions);} catch (IOException e) { // Handle the error.}Both options remove the redaction annotations when they redact the content.
Previewing redactions
Enable PdfFragment#isRedactionAnnotationPreviewEnabled to preview the redacted appearance without removing content:
pdfFragment.isRedactionAnnotationPreviewEnabled = truepdfFragment.setRedactionAnnotationPreviewEnabled(true);
