Is there any way to implement PDF redaction using iText? Working with the Acrobat SDK API I found that redactions also just seem to be annotations with the subtype "Redact". So I was wondering if it's possible to create those in iTextSharp as well?
With the Acrobat SDK the code would like simply like this:
AcroPDAnnot annot = page.AddNewAnnot(-1, "Redact", rect) as AcroPDAnnot;
(I haven't be able to apply them though as annot.Perform(avDoc)
does not seem to work. Ideas?)
In iTextSharp I can create simple text annotations like this
PdfAnnotation annotation = PdfAnnotation.CreateText(stamper.Writer, rect, "Title", "Content", false, null);
The only other option I found so was was to create black rectangles as explained here, but that doesn't remove the text (it can still be selected). I want to create redaction annotations and eventually apply redaction.
// Update:
As I finally got around to create a working example I wanted to share it here. It does not apply the redactions in the end but it creates valid redactions which are properly shown within Acrobat and can then be applied manually.
using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
PdfReader pdfReader = new PdfReader(stream);
// Create a stamper
using (PdfStamper stamper = new PdfStamper(pdfReader, new FileStream(newFileName, FileMode.OpenOrCreate)))
{
// Add the annotations
int page = 1;
iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(500, 50, 200, 300);
PdfAnnotation annotation = new PdfAnnotation(stamper.Writer, rect);
annotation.Put(PdfName.SUBTYPE, new PdfName("Redact"));
annotation.Title = "My Author"; // Title = author
annotation.Put(new PdfName("Subj"), new PdfName("Redact")); // Redaction "Subject". When created in Acrobat, this is always set to "Redact"
float[] fillColor = { 0, 0, 0 }; // Black
annotation.Put(new PdfName("IC"), new PdfArray(fillColor)); // Interior color
float[] fillColorRed = { 1, 0, 0 }; // Red
annotation.Put(new PdfName("OC"), new PdfArray(fillColorRed)); // Outline color
stamper.AddAnnotation(annotation, page);
}
}