---
title: "Redact PDFs programmatically on Android | Nutrient SDK"
canonical_url: "https://www.nutrient.io/guides/android/features/redaction/"
md_url: "https://www.nutrient.io/guides/android/features/redaction.md"
last_updated: "2026-05-25T18:42:17.659Z"
description: "Learn to programmatically redact PDFs on Android using Nutrient Android SDK. Customize redaction appearance and apply overlays for effective document protection."
---

# Redacting PDFs programmatically on Android

You can create redactions programmatically via [`RedactionAnnotation`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.annotations/-redaction-annotation/index.html). Use the [`setRects()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.annotations/-base-rects-annotation/set-rects.html) method to set the regions that should be covered by the redaction annotation.

You also have a few customization options for how a redaction should look, both while in its marked state, which is when the redaction has been created but not yet applied, and in its redacted state, which is when the redaction has been applied. It isn’t possible to change the appearance once a redaction has been applied, since the redaction annotation will be removed from the document in the process of applying the redactions. Here’s a list of available customization options for redactions:

- [`setOverlayText()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.annotations/-redaction-annotation/set-overlay-text.html) can be used to set the text that should be displayed at the specified region when a redaction has been applied.

- [`setRepeatOverlayText()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.annotations/-redaction-annotation/set-repeat-overlay-text.html) defines whether the overlay text should be drawn only once or repeated to fill the entire redaction area. This defaults to disabled, which means the overlay text is only drawn once. It has no effect if there’s no overlay text specified.

- [`setColor()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.annotations/-annotation/set-color.html) can be used to change the color of the overlay text. It has no effect if there’s no overlay text specified. This defaults to [`Color.RED`](https://developer.android.com/reference/android/graphics/Color.html#RED).

- [`setFillColor()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.annotations/-annotation/set-fill-color.html) specifies the background color of the redaction area after it has been applied. The color is drawn on all the specified [`rects`]. This defaults to [`Color.BLACK`](https://developer.android.com/reference/android/graphics/Color.html#BLACK).

- [`setOutlineColor()`] specifies the color used for the redaction’s border in its marked state. This defaults to [`Color.RED`](https://developer.android.com/reference/android/graphics/Color.html#RED).

### KOTLIN

```kotlin

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)

```

### JAVA

```java

String text = "Guide";
final int textPosition = document.getPageText(pageIndex).indexOf(text);
if (textPosition == 0) {
    return;
}
final List<RectF> textRects = document.getPageTextRects(pageIndex, textPosition, text.length(), false);

RedactionAnnotation redaction = new RedactionAnnotation(0, textRects);
redaction.setColor(Color.MAGENTA);
redaction.setFillColor(Color.BLACK);
redaction.setOutlineColor(Color.YELLOW);
redaction.setOverlayText("REDACTED");

getPdfFragment().addAnnotationToPage(redaction, false);

```

The redaction annotation created with the above code snippet would look like what’s shown in the image below.

| Marked State                                                                                               | Redacted State                                                                                                   |
| ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
|  |  |

## Applying redactions

There are two separate options for applying redactions programmatically.

- Use [`PdfProcessorTask`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.processor/-pdf-processor-task/index.html). This creates a new document and will leave the original document with the redaction annotations untouched:

### KOTLIN

```kotlin

val outputFile: File
val document: PdfDocument
val processorTask = PdfProcessorTask.fromDocument(document).applyRedactions()

PdfProcessor.processDocument(processorTask, outputFile)
val redactedDocument = PdfDocumentLoader.openDocument(this, Uri.fromFile(outputFile))

```

### JAVA

```java

File outputFile;
PdfDocument document;
PdfProcessorTask processorTask = PdfProcessorTask.fromDocument(document).applyRedactions();

PdfProcessor.processDocument(processorTask, outputFile);
PdfDocument redactedDocument = PdfDocumentLoader.openDocument(this, Uri.fromFile(outputFile));

```

- Use the [`DocumentSaveOptions#setApplyRedactions()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document/-document-save-options/set-apply-redactions.html) option when saving the document via any of the save methods on [`PdfDocument`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document/-pdf-document/index.html). This will overwrite the existing document, removing content irreversibly:

### KOTLIN

```kotlin

val documentSaveOptions = DocumentSaveOptions(null, null, true, null);
documentSaveOptions.setApplyRedactions(true)
document.saveIfModified(documentSaveOptions)

```

### JAVA

```java

DocumentSaveOptions documentSaveOptions = new DocumentSaveOptions(null, null, false, null);
documentSaveOptions.setApplyRedactions(true);
document.saveIfModified(documentSaveOptions);

```

Both options will remove the redaction annotations in the process of redacting the content.

## Previewing redactions

To preview redactions and see how they’d look when applied, without removing any document content, you can use the [`PdfFragment#setRedactionAnnotationPreviewEnabled()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui/-pdf-fragment/set-redaction-annotation-preview-enabled.html) method:

### KOTLIN

```kotlin

pdfFragment.isRedactionAnnotationPreviewEnabled = true

```

### JAVA

```java

pdfFragment.setRedactionAnnotationPreviewEnabled(true);

```
---

## Related pages

- [Redacting PDFs in Android viewer](/guides/android/redaction/built-in-ui.md)
- [Search and redact PDFs on Android](/guides/android/redaction/search-and-redact.md)
- [PDF redaction library for Android](/guides/android/redaction.md)
- [Master PDF redaction on Android devices](/guides/android/redaction/introduction-to-redaction.md)
- [Using RegEx to redact PDFs on Android](/guides/android/redaction/regex-patterns.md)

