This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /guides/android/features/redaction.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Redact PDFs programmatically on Android | Nutrient SDK

You can create redactions programmatically via RedactionAnnotation. Use the setRects() method to set the regions that should be covered by the redaction annotation.

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.

The marked state is the only state that accepts appearance changes. Because the redaction process also deletes the redaction annotation from the document, the appearance cannot change after that point.

These methods control the appearance of a redaction:

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)

The code above creates the redaction annotation shown below.

Marked StateRedacted State
Marked state Redacted state

Applying redactions

Two options redact content programmatically, and they differ in what happens to the original document:

  • Use PdfProcessorTask. This creates a new document and leaves the original document, including its 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))
val documentSaveOptions = DocumentSaveOptions(null, null, true, null);
documentSaveOptions.setApplyRedactions(true)
document.saveIfModified(documentSaveOptions)

Both options remove the redaction annotations when they redact the content.

Previewing redactions

The PdfFragment#setRedactionAnnotationPreviewEnabled() method shows how the redactions will look once they are applied, without removing any content from the document:

pdfFragment.isRedactionAnnotationPreviewEnabled = true