Skip to main content
Inspiring
December 8, 2020
Answered

Converting a rectangle annotation to a redaction

  • December 8, 2020
  • 3 replies
  • 2458 views

Hi. I have a simple javascript that will convert highlight annotations to redactions. That works well. However it would be better if I could also use the rectangle (or other shape) annotation. But i cant get the script to work. It just deletes the annotion, I am assuming because I need to somehow specify that the redaction box be the same size / location as the rectangle. Is anyone able to help?

 

Here is what i have so far:

 

var annots = this.getAnnots();
for (var i=annots.length-1; i>=0; i--) {
       if (annots[i].type == "Rectangle") {
           this.addAnnot( {
           page: annots[i].page,
           type: "Redact",
           rect: annots[i].rect,
           overlayText: "REDACTED",
           alignment: 1,
           alignment: true,
           fillColor: color.black,
           textColor: color.white,
           textSize: 0,
           });
     }
     annots[i].destroy();
}

this.applyRedactions ({
bKeepMarks: false,
bShowConfirmation: false,
})

 

 

P.S. the reason for this is that not all people in my office have Adobe Pro. This means that the solicitors with Reader can mark what they want me to redact and I can redact it faster than manually going through.

This topic has been closed for replies.
Correct answer leroy0101

Ok thanks to Bernd I have managed to get this to work. So for everyone else who might find this helpfull here is hte code

var annots = this.getAnnots();
for (var i=annots.length-1; i>=0; i--) {
    if (annots[i].type == "Square") {
    var rct = annots[i].rect;
    var left = rct[0];
    var right = rct[2];
    var top = rct[3];
    var bot = rct[1];
    var qd = [ [left, top, right, top, left, bot, right, bot] ];        
    this.addAnnot( {
        page: annots[i].page,
        type: "Redact",
        quads: qd,
        overlayText: "REDACTED", //THIS IS THE REDACTION TEXT
        alignment: 1,            // "0" = LEFT, "1" = CENTRE, "2" = RIGHT
        fillColor: color.black,  //THIS CHANGES THE FILL COLOUR
        textColor: color.white,  //THIS CHANGES THE FONT COLOUR
        textSize: 0,             //FONT SIZE, '0' will adjust the size to fit each box, otherwise you can use a specific font size
        });
    }
}

this.applyRedactions ({
bKeepMarks: false,
bShowConfirmation: false,
});

 

3 replies

leroy0101AuthorCorrect answer
Inspiring
December 13, 2020

Ok thanks to Bernd I have managed to get this to work. So for everyone else who might find this helpfull here is hte code

var annots = this.getAnnots();
for (var i=annots.length-1; i>=0; i--) {
    if (annots[i].type == "Square") {
    var rct = annots[i].rect;
    var left = rct[0];
    var right = rct[2];
    var top = rct[3];
    var bot = rct[1];
    var qd = [ [left, top, right, top, left, bot, right, bot] ];        
    this.addAnnot( {
        page: annots[i].page,
        type: "Redact",
        quads: qd,
        overlayText: "REDACTED", //THIS IS THE REDACTION TEXT
        alignment: 1,            // "0" = LEFT, "1" = CENTRE, "2" = RIGHT
        fillColor: color.black,  //THIS CHANGES THE FILL COLOUR
        textColor: color.white,  //THIS CHANGES THE FONT COLOUR
        textSize: 0,             //FONT SIZE, '0' will adjust the size to fit each box, otherwise you can use a specific font size
        });
    }
}

this.applyRedactions ({
bKeepMarks: false,
bShowConfirmation: false,
});

 

New Participant
October 23, 2023

Is there anyway to make the redaction boxes smaller?

Thom Parker
Inspiring
October 23, 2023

Yes, you can do that by changing the quads for the redaction annotation to make a smaller rectangle. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
leroy0101Author
Inspiring
December 9, 2020

UPDATE - So i have managed to get the code to kind of work, but im not sure how to to get this code to loop through every annotation box in the  PDF. 

var annots = this.getAnnots();
var rct = getAnnots(this.pageNum)[0].rect;
var left = rct[0];
var right = rct[2];
var top = rct[3];
var bot = rct[1];
var qd = [ [left, top, right, top, left, bot, right, bot] ];
for (var i=annots.length-1; i>=0; i--) {
    if (annots[i].type == "Square") {
        this.addAnnot( {
        page: annots[i].page,
        type: "Redact",
        quads: qd,
        overlayText: "REDACTED",
        alignment: true,
        fillColor: color.black,
        textColor: color.white,
        textSize: 0,
        });
    }
}

this.applyRedactions ({
bKeepMarks: false,
bShowConfirmation: false,
});

Bernd Alheit
Braniac
December 9, 2020

Put the calculation of qd in the loop.

leroy0101Author
Inspiring
December 10, 2020

Hi Bernd, can you please point in the the direction of how to do this. I dont have any coding experience, but if you could tell me a bit more specifically where it needs to go in the code, then maybe i can figure the rest out on my own. It would be a good learning experience. Thanks. 

Bernd Alheit
Braniac
December 8, 2020

Why does you destroy all annotations? A annotations type "Rectangle" doesn't exists.

leroy0101Author
Inspiring
December 8, 2020

Hi Bernd. The reason is that once it has created the redaction box i no longer need the rectangle annotation. 

Bernd Alheit
Braniac
December 8, 2020

Info: A annotations type "Rectangle" doesn't exists.