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

Use TextSearch to find text for redaction in a PDF. Add a RedactionAnnotation for each search result.

Java can’t call suspend functions directly. See calling asynchronous APIs from Java for the options.

The following example marks every match for the term secret:

val wordToSearch = "secret"
val textSearch = TextSearch(document, PdfConfiguration.Builder().build())
lifecycleScope.launch {
// `performSearchAsync()` searches off the main thread and emits each result.
textSearch.performSearchAsync(wordToSearch).collect { searchResult ->
val redaction = RedactionAnnotation(searchResult.pageIndex, searchResult.textBlock.pageRects).apply {
color = Color.WHITE
fillColor = Color.BLACK
outlineColor = Color.YELLOW
overlayText = "REDACTED"
}
document.annotationProvider.addAnnotationToPage(redaction)
}
}

To apply redactions to a new PDF file, use the PdfProcessor API:

val outputFile = File(context.filesDir, "redacted.pdf")
val processorTask = PdfProcessorTask.fromDocument(document)
.applyRedactions()
PdfProcessor.processDocument(processorTask, outputFile)
val redactedDocument = PdfDocumentLoader.openDocument(context, Uri.fromFile(outputFile))

Alternatively, enable DocumentSaveOptions#setApplyRedactions() when calling PdfDocument.saveIfModified().

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)