Skip to main content

RedactionAnnotation Class

Defines a PDF redaction annotation.

Declaration

public class RedactionAnnotation extends MarkupAnnotation

Remarks

Redaction annotations permanently remove sensitive information from a PDF document. You can redact text, images, and other page content before you distribute the document.

Redaction Annotation, DevExpress PDF Document API for Java

A typical redaction workflow includes the following steps:

  1. Create one or more redaction annotations.
  2. Review or modify annotations if necessary.
  3. Apply annotations to permanently remove the underlying content.

Important

A redaction annotation only marks content for removal. The original content remains in the document until you apply the annotation. Once applied, the content cannot be recovered.

The following code snippet searches for confidential information in a PDF document and creates redaction annotations over all matches:

package pdf;

import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;

import java.nio.channels.*;
import java.nio.file.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (FileChannel fileChannel = FileChannel.open(Path.of("Confidential.pdf"));
             PdfDocument pdfDocument = new PdfDocument(fileChannel)) {

            Page page = pdfDocument.getPages().getFirst();

            createRedactionAnnotation(pdfDocument);

            // Save the document.
            try (WritableByteChannel channel =
                         FileChannel.open(Path.of("Result.pdf"),
                                 StandardOpenOption.CREATE,
                                 StandardOpenOption.WRITE,
                                 StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(channel);
            }
        }
    }

    static void createRedactionAnnotation(PdfDocument pdfDocument) {
        // Text strings to locate and redact.
        String[] searchTerms = {
                "Alex Carter",
                "+1 (415) 555-0187",
                "alex.carter@example.com",
                "742 Evergreen",
        };

        // Search text case-insensitively and match only whole words.
        TextSearchOptions searchOptions = new TextSearchOptions(false, true);

        // Search for each text string.
        for (String searchTerm : searchTerms) {

            // Find all occurrences of the current search string.
            Iterable<TextSearchInfo> results = pdfDocument.findText(searchTerm, searchOptions);

            // Process each search result.
            for (TextSearchInfo result : results) {

                // Get the page that contains the matched text.
                Page page = pdfDocument.getPages().get(result.getPageIndex());

                // Store the bounding boxes for all matched text fragments.
                List<RectangleF> areas = new ArrayList<>();

                // Process each text match.
                for (TextMatchInfo match : result.getMatches()) {

                    // A match can consist of multiple text fragments.
                    for (TextMatchFragment fragment : match.getMatchFragments()) {

                        // Add the fragment's bounding box to the list.
                        areas.add(fragment.getRectangle().getBoundingBox());
                    }
                }

                // Create a redaction annotation for each text fragment.
                for(RectangleF rectangle : areas) {

                    RedactionAnnotation annotation = new RedactionAnnotation(rectangle);

                    // Specify the annotation border color before the redaction is applied.
                    annotation.setColor(PdfColor.getRed());

                    // Specify the fill color after the redaction is applied.
                    annotation.setFillColor(PdfColor.getBlack());

                    // Specify the overlay text appearance.
                    annotation.setTextAppearance(new TextAppearance() {{
                        setFill(new SolidFill(PdfColor.getWhite()));
                        setFontSize(5);
                    }});

                    // Specify the overlay text.
                    annotation.setOverlayText("CONFIDENTIAL");

                    // Center the overlay text horizontally.
                    annotation.setTextJustification(TextJustification.CENTERED);

                    // Display the overlay text only once.
                    annotation.setRepeatText(false);

                    // Add the annotation to the page.
                    page.getAnnotations().add(annotation);
                }
            }
        }
    }
}

Refer to the following help topic for additional information: Redaction Annotations.

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
com.devexpress.system.JavaObject.memberwiseClone()
com.devexpress.system.JavaObject.toString()
java.lang.Object.finalize()
java.lang.Object.getClass()
java.lang.Object.notify()
java.lang.Object.notifyAll()
java.lang.Object.wait()
java.lang.Object.wait(long)
java.lang.Object.wait(long,int)

Inheritance

Object
JavaObject

Constructors

RedactionAnnotation(OrientedRectangle[] rectangles) Constructor

Initializes a new instance of the RedactionAnnotation class with specified settings.

Declaration

public RedactionAnnotation(OrientedRectangle[] rectangles)

Parameters

Name Type Description
rectangles OrientedRectangle[]

The redaction annotation rectangles.

RedactionAnnotation(Quadrilateral[] quads) Constructor

Initializes a new instance of the RedactionAnnotation class with specified quadrilaterals.

Declaration

public RedactionAnnotation(Quadrilateral[] quads)

Parameters

Name Type Description
quads Quadrilateral[]

The redaction annotation quadrilaterals.

RedactionAnnotation(RectangleF bounds) Constructor

Initializes a new instance of the RedactionAnnotation class with the specified bounds.

Declaration

public RedactionAnnotation(RectangleF bounds)

Parameters

Name Type Description
bounds RectangleF

The redaction annotation bounds.

RedactionAnnotation(RedactionGeometry geometry) Constructor

Initializes a new instance of the RedactionAnnotation class with the specified geometry.

Declaration

public RedactionAnnotation(RedactionGeometry geometry)

Parameters

Name Type Description
geometry RedactionGeometry

The redaction annotation geometry.

Methods

getBounds() Method

Returns the redaction annotation bounds.

Declaration

public RectangleF getBounds()

Returns

Type Description
RectangleF

The redaction annotation bounds.

getFillColor() Method

Returns the redaction annotation fill color.

Declaration

public PdfColor getFillColor()

Returns

Type Description
PdfColor

The redaction annotation fill color.

getGeometry() Method

Returns the redaction annotation geometry.

Declaration

public RedactionGeometry getGeometry()

Returns

Type Description
RedactionGeometry

The redaction annotation geometry.

getOverlayText() Method

Returns the redaction annotation overlay text.

Declaration

public String getOverlayText()

Returns

Type Description
String

The redaction annotation overlay text.

getRepeatText() Method

Returns whether the redaction annotation repeats overlay text.

Declaration

public boolean getRepeatText()

Returns

Type Description
boolean

true if the redaction annotation repeats overlay text; otherwise, false.

getReplaceObject() Method

Returns the redaction annotation replacement object.

Declaration

public FormTemplate getReplaceObject()

Returns

Type Description
FormTemplate

The redaction annotation replacement object.

getTextAppearance() Method

Returns the redaction annotation overlay text formatting settings.

Declaration

public TextAppearance getTextAppearance()

Returns

Type Description
TextAppearance

The redaction annotation overlay text appearance.

getTextJustification() Method

Returns the redaction annotation overlay text justification.

Declaration

public TextJustification getTextJustification()

Returns

Type Description
TextJustification

The redaction annotation overlay text justification.

setBounds(RectangleF value) Method

Sets the redaction annotation bounds.

Declaration

public void setBounds(RectangleF value)

Parameters

Name Type Description
value RectangleF

The redaction annotation bounds.

setFillColor(PdfColor value) Method

Sets the redaction annotation fill color.

Declaration

public void setFillColor(PdfColor value)

Parameters

Name Type Description
value PdfColor

The redaction annotation fill color.

setGeometry(RedactionGeometry value) Method

Sets the redaction annotation geometry.

Declaration

public void setGeometry(RedactionGeometry value)

Parameters

Name Type Description
value RedactionGeometry

The redaction annotation geometry.

setOverlayText(String value) Method

Sets the redaction annotation overlay text.

Declaration

public void setOverlayText(String value)

Parameters

Name Type Description
value String

The redaction annotation overlay text.

setRepeatText(boolean value) Method

Sets whether the redaction annotation repeats overlay text.

Declaration

public void setRepeatText(boolean value)

Parameters

Name Type Description
value boolean

true to repeat overlay text; otherwise, false.

setReplaceObject(FormTemplate value) Method

Sets the redaction annotation replacement object.

Declaration

public void setReplaceObject(FormTemplate value)

Parameters

Name Type Description
value FormTemplate

The redaction annotation replacement object.

setTextAppearance(TextAppearance value) Method

Sets the redaction annotation overlay text formatting settings.

Declaration

public void setTextAppearance(TextAppearance value)

Parameters

Name Type Description
value TextAppearance

The redaction annotation overlay text appearance.

setTextJustification(TextJustification value) Method

Sets the redaction annotation overlay text justification.

Declaration

public void setTextJustification(TextJustification value)

Parameters

Name Type Description
value TextJustification

The redaction annotation overlay text justification.