Tips & Tricks

Modifying link properties using JavaScript

January 05, 2005

Advertisement
Advertisement
 

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.artspdf.com
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
         }
      }   
   }
}
Related Products at PDF Store

ARTS PDF Split & Merge Lite

The easiest way to split and merge PDFs! It provides a simpler method of splitting and merging your ... View full product details
Download free demo

Nitro PDF Professional

Nitro PDF Professional, your PDF creation and editing product. Priced at $99, Nitro PDF Pro is the m... View full product details
Download free demo

ARTS PDF Aerialist

Take Acrobat to the next level with advanced splitting and merging; flexible bookmark creation and m... View full product details
Download free demo

PDF In-Depth Free Product Trials Ubiquitous PDF

Nitro PDF Professional

the perfect PDF product for business and enterprise, combining an extremely competitive price with a...

Download free demo

XpdfViewer

This ActiveX control (OCX) provides a PDF file viewer component, enabling developers to add PDF viewing...

Download free demo

Ubiquitous PDF: PDF eBooks-Library

If you are looking for a good store of PDF content, you could do a lot worse than visiting eBooks-Library.com...

September 03, 2009
Search Planet PDF
more searching options...







Download PDF Creator

Download The Best of Planet PDF volume 2
Planet PDF Newsletter
Most Popluar Articles
Features

Collating PDFs using JavaScript

Despite the numerous benefits, there can be potential issues with the conversion of paper documents into electronic archives. When scanning paper pages into PDF, it's possible to end up with the odd- and even-numbered pages in separate PDF files. It can be very time-consuming to collate them manually, but there is an easier way. Sean Stewart explains.

Featured Product

BCL easyPDF SDK

BCL easyPDF SDK is a set of PDF Programming Libraries designed specifically to help Software Developers / Programmers build and deploy enterprise class PDF applications for corporate wide PDF...

Platinum Sponsor
Create & Edit PDF - Nitro PDF Software

ARTS PDF

Silver Sponsors

PDF-Tools enfocus

QuickPDF: The Unrivaled PDF Developer Toolkit