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

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:

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.MAGENTA
redaction.fillColor = Color.BLACK
redaction.outlineColor = Color.YELLOW
redaction.overlayText = "REDACTED"
requirePdfFragment().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 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))

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.defaultDocumentSaveOptions
documentSaveOptions.isIncremental = false
documentSaveOptions.setApplyRedactions(true)
document.saveIfModified(documentSaveOptions)

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 = true