Modifying link properties using JavaScript

When creating interactive PDF documents, links are an integral tool, so what do you do if you find that your links don’t match or you need to change link styles throughout a long document? Updating each link manually is often unfeasible due to time constraints, but Acrobat JavaScript may provide the solution.

I often copy links when I create our user guides, for example, sometimes only realizing I had copied 50 ‘visible’ links instead of 50 ‘invisible’ links after the fact. Unfortunately, this meant that I ended up starting all over again because there was no feature in Acrobat capable of modifying the visibility of all links. This sample JavaScript globally sets link properties of all links in the open document — i.e. make all links visible, invisible, blue etc. I used the Link object’s borderColor and borderWidth properties, but it’s also possible to use the sample to set link highlight (highlightMode) with a little tweaking.

Note:
Although I originally wrote this to be executed via a custom tool button using ARTS PDF Aerialist, you can also execute it from Acrobat Professional’s JavaScript debugger.


/*
Title: Set Link Properties
Purpose: Change the appearance of all links in the active document.
Author: Maryanne Montes, ARTS PDF, http://www.debenu.com/arts-pdf/
Date: December 8, 2004
*/


// declare variables
var numLinks=0;
var myBorderWidth=1;
var myBorderColor=color.black;

// build a pop-up menu: link type, line thickness and color
var strResult =
app.popUpMenuEx({cName:'Link Type',oSubMenu:[
{cName:'Visible',cReturn:'type:visible'},
{cName:'Invisible', cReturn:'type:invisible'}]
},
{cName:'Line Thickness',oSubMenu:[{cName:'Thin',cReturn:'thickness:thin'},
{cName:'Medium', cReturn:'thickness:medium'},
{cName:'Thick', cReturn:'thickness:thick'}]
},
{cName:'Color',oSubMenu:[{cName:'Black',cReturn:'color:black'},
{cName:'Red',cReturn:'color:red'},
{cName:'Green', cReturn:'color:green'},
{cName:'Blue', cReturn:'color:blue'}]
});

// get the item selected from the menu
var arrayVals = strResult.split(':');

// check if any item was selected
if (strResult != null)
{
// check if Link Type or Line Thickness was selected
if (arrayVals[0] == 'type' || arrayVals[0] == 'thickness')
{

// assign the border width value to set
// if visible was selected assign border width to 1
if (arrayVals[1] == 'visible') {
myBorderWidth = 1;
}

// if invisible was selected assign border width to 0
if (arrayVals[1] == 'invisible') {
myBorderWidth = 0;
}

// if thin was selected assign border width to 1
if (arrayVals[1] == 'thin') {
myBorderWidth = 1;
}

// if medium was selected assign border width to 2
if (arrayVals[1] == 'medium') {
myBorderWidth = 2;
}

// if thick was selected assign border width to 3
if (arrayVals[1] == 'thick') {
myBorderWidth = 3;
}

// change the border width of all links in the document
for ( var p = 0; p < this.numPages; p++) // loop through each page
{
var b = this.getPageBox('Crop', p); // get the page box
var l = this.getLinks(p, b); // get the link
for (var i=0; i < l.length; i++) // loop through each link
{
l[i].borderWidth = myBorderWidth; // set the border width
}
}
}

// check if Color was selected
if (arrayVals[0] == 'color')
{

// assign the border color value to set
// if black was selected assign border color to color.black
if (arrayVals[1] == 'black') {
myBorderColor = color.black;
}

// if red was selected assign border color to color.red
if (arrayVals[1] == 'red') {
myBorderColor = color.red;
}

// if green was selected assign border color to color.green
if (arrayVals[1] == 'green') {
myBorderColor = color.green;
}

// if blue was selected assign border color to color.blue
if (arrayVals[1] == 'blue') {
myBorderColor = color.blue;
}

// change the border color of all links in the document
for ( var p = 0; p < this.numPages; p++) // loop through each page
{
var b = this.getPageBox('Crop', p); // get the page box
var l = this.getLinks(p, b); // get the links
for (var i=0; i < l.length; i++) // loop through each link
{
l[i].borderColor = myBorderColor; // set the border color
}
}
}
}

You May Also Like

About the Author: Maryanne Montes

Leave a Reply