Convert annotations properties based on content

Is it possible to convert annotations properties using javascript? Here's what I'm dealing with:

I have a bunch of sticky notes in my PDF. Some of them need to be changed to blue. Currently, I go through the PDF manually, looking for the sticky notes that need to be changed to blue. Then I manually change them to blue. But is it possible to have a script that would change certain ones to blue automatically?

For example, if the content in the sticky note started with "Photo Request," I would like an automated way to change those comments to blue. Is this possible?

Many thanks,
Kelly Vaughn


Kelly Vaughn


4 Answers

Voted Best Answer

Yes.

The following script can do that:

// iterate over all annotations and process only "sticky notes"
this.syncAnnotScan();
var annots = this.getAnnots();

for (var i = 0; i < annots.length; i++) {
    if (annots[i].type == "Text") {
        // does it start with "Photo Request,"? 
        if (annots[i].contents.indexOf("Photo Request,") == 0) {
            annots[i].strokeColor = color.blue;
        }
    }
}

You can use it for example in an Action and process one or more documents in one operation.

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


By Karl Heinz Kremer   

Hi Karl,

Do you know where I might find a list of available colors to use in this script? I downloaded the Acrobat SDK, and was looking through the Javascript folder. I found a few basic colors, like Yellow, Green, and Red, but I'd like to use a wider array of colors, but I don't know how to specify those values. Can you point me in the right direction?

Many thanks,
Kelly Vaughn


Kelly Vaughn   

Type the following in the Javascript console:

color.toSource();

This will print the color data structure and with that, you have a full list of all named colors. To save you the trouble of doing this, here is the list:

({
    transparent: ["T"],
    black: ["G", 0],
    white: ["G", 1],
    dkGray: ["G", 0.25],
    gray: ["G", 0.5],
    ltGray: ["G", 0.75],
    red: ["RGB", 1, 0, 0],
    green: ["RGB", 0, 1, 0],
    blue: ["RGB", 0, 0, 1],
    cyan: ["CMYK", 1, 0, 0, 0],
    magenta: ["CMYK", 0, 1, 0, 0],
    yellow: ["CMYK", 0, 0, 1, 0],
    convert: function ColorConvert(oColor, cColorspace) {
        var oOut = oColor;
        switch (cColorspace) {
            case "G":
                if (oColor[0] == "RGB") {
                    oOut = new Array("G", 0.3 * oColor[1] + 0.59 * oColor[2] + 0.11 * oColor[3]);
                } else if (oColor[0] == "CMYK") {
                    oOut = new Array("G", 1 - Math.min(1, 0.3 * oColor[1] + 0.59 * oColor[2] + 0.11 * oColor[3] + oColor[4]));
                }
                break;
            case "RGB":
                if (oColor[0] == "G") {
                    oOut = new Array("RGB", oColor[1], oColor[1], oColor[1]);
                } else if (oColor[0] == "CMYK") {
                    oOut = new Array("RGB", 1 - Math.min(1, oColor[1] + oColor[4]), 1 - Math.min(1, oColor[2] + oColor[4]), 1 - Math.min(1, oColor[3] + oColor[4]));
                }
                break;
            case "CMYK":
                if (oColor[0] == "G") {
                    oOut = new Array("CMYK", 0, 0, 0, 1 - oColor[1]);
                } else if (oColor[0] == "RGB") {
                    oOut = new Array("CMYK", 1 - oColor[1], 1 - oColor[2], 1 - oColor[3], 0);
                }
                break;
            default:
                ;
        }
        return oOut;
    },
    equal: function ColorEqual(c1, c2) {
        if (c1[0] == "G") {
            c1 = color.convert(c1, c2[0]);
        } else {
            c2 = color.convert(c2, c1[0]);
        } if (c1[0] != c2[0]) {
            return false;
        }
        var nComponents = 0;
        switch (c1[0]) {
            case "G":
                nComponents = 1;
                break;
            case "RGB":
                nComponents = 3;
                break;
            case "CMYK":
                nComponents = 4;
                break;
            default:
                ;
        }
        for (var i = 1; i <= nComponents; i++) {
            if (c1[i] != c2[i]) {
                return false;
            }
        }
        return true;
    }
})

Keep in mind that you can create any color you want based on the RGB or CMYK values.

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


Karl Heinz Kremer   

There are two bits that are tricky to the solution:

  • A color is represented in Acrobat's JavaScript as an array with the first array element defining the color space, and the remaining elements being the different colors. For grayscale, you have one additional element, for RGB you have three and for CMYK you have four.
  • The individual color components have to be in the range of 0 to 1. RGB values are usually represented as numbers between 0 and 255, so we have to divide by 255 to bring these components into the 0..1 range.

Replace the line in which you assign the color with this line:

annots[i].strokeColor = [ "RGB", 193/255, 254/255, 193/255 ];

That should do the job.

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


Karl Heinz Kremer   


Please specify a reason: