Customizing redaction with pdfSweep

With pdfSweep it is also easy to customize your redaction process. This example shows you how to change the color of the redaction annotation to match the color of the text that was redacted, to provi

With pdfSweep it is also easy to customize your redaction process. This example shows you how to change the color of the redaction annotation to match the color of the text that was redacted, to provide a more aesthetically pleasing result.

We start by defining a class that extends CharacterRenderInfo, and keeps tracks of the color of each character.

class CCharacterRenderInfo extends CharacterRenderInfo {

    private Color strokeColor;
    private Color fillColor;

    public CCharacterRenderInfo(TextRenderInfo tri) {
        super(tri);
        this.strokeColor = tri.getStrokeColor();
        this.fillColor = tri.getFillColor();
    }

    public Color getStrokeColor() {
        return strokeColor;
    }

    public Color getFillColor() {
        return fillColor;
    }
}

This class can then be used to build a custom strategy, that knows the color of the redacted elements.

class CustomLocationExtractionStrategy extends RegexBasedLocationExtractionStrategy implements ICleanupStrategy {

    private String regex;
    private Map colorByRectangle = new HashMap();

    public CustomLocationExtractionStrategy(String regex) {
        super(regex);
        this.regex = regex;
    }

    @Override
    public List toCRI(TextRenderInfo tri) {
        List cris = new ArrayList();
        for (TextRenderInfo subTri : tri.getCharacterRenderInfos()) {
            cris.add(new CCharacterRenderInfo(subTri));
        }
        return cris;
    }

    @Override
    public List toRectangles(List cris) {
        Color col = ((CCharacterRenderInfo) cris.get(0)).getFillColor();
        List rects = new ArrayList(super.toRectangles(cris));
        for (Rectangle rect : rects) {
            colorByRectangle.put(rect, col);
        }
        return rects;
    }

    @Override
    public Color getRedactionColor(IPdfTextLocation rect) {
        return colorByRectangle.containsKey(rect.getRectangle()) ? colorByRectangle.get(rect.getRectangle()) : Color.BLACK;
    }

    public ICleanupStrategy reset() {
        return new CustomLocationExtractionStrategy(regex);
    }
}

The custom strategy can easily be called with following code:

// load license key
LicenseKey.loadLicenseFile(licenceFile);

String input = "iphone_user_guide_untagged.pdf";
String output = "redactIPhoneUserManualMatchColor.pdf";

CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
strategy.add(new CustomLocationExtractionStrategy("(iphone)|(iPhone)"));

PdfDocument pdf = new PdfDocument(new PdfReader(input), new PdfWriter(output));

// sweep
PdfAutoSweep autoSweep = new PdfAutoSweep(strategy);
autoSweep.cleanUp(pdf);
pdf.close();

This is what the example output document looks like, post redaction:

Custom redaction with pdfSweep
Custom redaction with pdfSweep

Share this article

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now