org.faceless.pdf2
Class Redactor

java.lang.Object
  extended by org.faceless.pdf2.Redactor

public class Redactor
extends Object

The Redactor can be used to redact (completely remove) text and images from a PDF. This is quite simple to do - simply add a list of Area objects on each PDFPage, then call redact(). Here's an example showing how to remove any copies of the word "secret" from a PDF.

 PDF pdf = new PDF(new PDFReader(new File("private.pdf")));
 PDFParser parser = new PDFParser(pdf);
 Redactor redactor = new Redactor();
 for (int i=0;i<pdf.getNumberOfPages();i++) {
   PageExtractor extractor = parser.getPageExtractor(i);
   Collection all = extractor.getMatchingText("secret");
   for (Iterator j = all.iterator();j.hasNext();) {
     PageExtractor.Text text = (PageExtractor.Text)j.next();
     float[] corners = text.getCorners();
     GeneralPath path = new GeneralPath();
     path.moveTo(corners[0], corners[1]);
     path.lineTo(corners[2], corners[3]);
     path.lineTo(corners[4], corners[5]);
     path.lineTo(corners[6], corners[7]);
     Area area = new Area(path);
     redactor.addArea(text.getPage(), area);
   }
 }
 redactor.redact();
 pdf.render(new FileOutputStream("public.pdf"));
 
Redaction potentially has to rebuild the PDF structure, so will lock on the PDF object itself before running and on each page before processing it.

Since:
2.11.8

Constructor Summary
Redactor()
          Creates a new Redactor
 
Method Summary
 void addArea(PDFPage page, Area area)
          Adds an area to redact out of the document.
 void redact()
          Performs the redaction.
 void setRedactionColor(Paint color)
          Sets the Paint to use to fill in any redacted areas.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Redactor

public Redactor()
Creates a new Redactor

Method Detail

addArea

public void addArea(PDFPage page,
                    Area area)
Adds an area to redact out of the document. This only redacts the area out of the specified page if a page is given. If a null page is specified, the area will be redacted out of every page in the PDF.

Parameters:
page - the page to redact the area from
area - the area to redact (in PDF page coordinates)

setRedactionColor

public void setRedactionColor(Paint color)
Sets the Paint to use to fill in any redacted areas. The paint can be a solid color, it can be new Color(0, true) (a transparent color) to erase the content so the background can be seen, or it could be a PDFPattern to redact with a "stamp".

Parameters:
color - the color to redact with

redact

public void redact()
            throws IOException
Performs the redaction. Page content streams are normalised as part of this operation.

Throws:
IOException


Copyright © 2001-2012 Big Faceless Organization