Search and redact PDFs on Android
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) }}import kotlin.coroutines.EmptyCoroutineContext;import kotlinx.coroutines.BuildersKt;
// Searching and adding annotations both block, so run this on a worker thread.final String wordToSearch = "secret";final TextSearch textSearch = new TextSearch(document, new PdfConfiguration.Builder().build());final List<SearchResult> searchResults = textSearch.performSearch(wordToSearch);
try { for (SearchResult searchResult : searchResults) { final RedactionAnnotation redaction = new RedactionAnnotation(searchResult.pageIndex, searchResult.textBlock.pageRects); redaction.setColor(Color.WHITE); redaction.setFillColor(Color.BLACK); redaction.setOutlineColor(Color.YELLOW); redaction.setOverlayText("REDACTED");
// `addAnnotationToPage()` is a suspend function. `runBlocking()` waits for it to finish. BuildersKt.runBlocking( EmptyCoroutineContext.INSTANCE, (scope, continuation) -> document.getAnnotationProvider().addAnnotationToPage(redaction, continuation)); }} catch (InterruptedException e) { Thread.currentThread().interrupt();}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))final File outputFile = new File(context.getFilesDir(), "redacted.pdf");final PdfProcessorTask processorTask = PdfProcessorTask.fromDocument(document) .applyRedactions();
PdfProcessor.processDocument(processorTask, outputFile);try { final PdfDocument redactedDocument = PdfDocumentLoader.openDocument(context, Uri.fromFile(outputFile));} catch (IOException e) { // Handle the error.}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.defaultDocumentSaveOptionsdocumentSaveOptions.isIncremental = falsedocumentSaveOptions.setApplyRedactions(true)document.saveIfModified(documentSaveOptions)final DocumentSaveOptions documentSaveOptions = document.getDefaultDocumentSaveOptions();documentSaveOptions.setIncremental(false);documentSaveOptions.setApplyRedactions(true);try { document.saveIfModified(documentSaveOptions);} catch (IOException e) { // Handle the error.}