Redaction

What Is Redaction?

Redaction is the process of removing content from a PDF page. This not only involves obscuring the content, but also removing the data in the document within the specified region.

Redaction is a two-step process.

  • First, redaction annotations have to be created in the areas that should be redacted. This step won’t remove any content from the document yet; it just marks regions for redaction.
  • Second, to actually remove the content, the redaction annotations need to be applied. In this step, the page content within the region of the redaction annotations is irreversibly removed.

For further in-depth information, check out the official PDF Redaction addendum for the PDF Reference by Adobe.

PSPDFKit 5 for Android added a redaction feature with support for redacting text. However, it doesn’t yet support redacting any arbitrary regions or images in a document. Redaction support will be expanded upon in future versions. Using redaction requires the redaction component to be enabled in your license. Otherwise, all API calls related to redaction will fail, the UI will be hidden, and no RedactionAnnotations will be shown.

Creating Redactions

Programmatically

You can create redactions programmatically via RedactionAnnotation. Use the setRects() 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 is not 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() can be used to set the text that should be displayed at the specified region when a redaction has been applied.
  • setRepeatOverlayText() 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 is no overlay text specified.
  • setColor() can be used to change the color of the overlay text. It has no effect if there is no overlay text specified. This defaults to Color.RED.
  • setFillColor() 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.
  • setOutlineColor() specifies the color used for the redaction’s border in its marked state. This defaults to Color.RED.
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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)
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 this:

Marked State
Redacted State

User Interface

In addition to being able to create redactions programmatically, you can create and customize redactions via the UI PSPDFKit provides.

Adding a redaction to a document can be done by selecting the redaction tool on the annotation toolbar. This will enable the redaction mode and allow you to select text by dragging along. A redaction annotation will be created once you lift your finger.

Another way of creating a redaction is by first selecting text and then tapping the redaction item on the menu.

Once a redaction is added to the document, its properties and appearance can be customized via the annotation inspector.

Applying Redactions

Programmatically

There are two separate options for applying redactions programmatically.

  • Use PdfProcessorTask. This creates a new document and will leave the original document with the redaction annotations untouched:
Copy
1
2
3
4
5
6
7
val outputFile: File
val document: PdfDocument
val processorTask = PdfProcessorTask.fromDocument(document)
    .applyRedactions()

PdfProcessor.processDocument(processorTask, outputFile)
val redactedDocument = PdfDocumentLoader.openDocument(this, Uri.fromFile(outputFile))
Copy
1
2
3
4
5
6
7
File outputFile;
PdfDocument document;
PdfProcessorTask processorTask = PdfProcessorTask.fromDocument(document)
    .applyRedactions();

PdfProcessor.processDocument(processorTask, outputFile);
PdfDocument redactedDocument = PdfDocumentLoader.openDocument(this, Uri.fromFile(outputFile));
Copy
1
2
3
val documentSaveOptions = DocumentSaveOptions(null, null, true, null);
documentSaveOptions.setApplyRedactions(true)
document.saveIfModified(documentSaveOptions)
Copy
1
2
3
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.

User Interface

Once there are redactions in the document, a floating redaction info button is shown in the user interface view. Tapping this button reveals a UI to apply redactions in the document. Tapping the Apply Redactions button will allow you to overwrite the current document or save the redacted version to a new location and remove the content in the area of the redaction annotations irreversibly. Tapping the Clear Redactions button will remove all redaction annotations. This can be undone using the regular undo button. In the process of redacting the content, all redaction annotations are removed from the document.

Note that when opening a document with existing redactions, there may be a delay before showing the button, since we need to check the document for redactions.

Previewing Redactions

Programmatically

To preview redactions and see how they would look when applied, without removing any document content, you can use the PdfFragment#setRedactionAnnotationPreviewEnabled() method:

1
pdfFragment.isRedactionAnnotationPreviewEnabled = true
1
pdfFragment.setRedactionAnnotationPreviewEnabled(true);

User Interface

Redactions can be previewed by tapping on the floating redaction info button. When switching the preview mode on, all redaction annotations in a document will be rendered in their redacted state appearance (as opposed to the marked state that is otherwise always shown by default). This can be changed without having any content removed from the document. It is just for previewing purposes, and the actual removal of the document content will only happen when tapping the Apply Redactions button.

Custom Redaction UI

If you want to customize the UI to preview and apply redactions, you can disable the floating redaction info button in the user interface view via PdfActivityConfiguration.Builder#setRedactionUiEnabled(). Then you can roll your own UI to indicate uncommitted redactions and handle previewing and applying redactions in your own way instead.

Licensing

Redaction is a feature that has to be licensed. If this feature is not licensed, some model-level APIs and the UI will change to not function the way they are described in this article. The following list describes the expected behavior if Redaction is not part of your license: