Tips & Tricks

Change field shading with JavaScript when printing

November 15, 2002


What if you want to display shaded fields to assist with on screen filling out of forms, but when you print the final version, you want the fields to be unshaded. And, you want to use your own custom color to shade the fields?

Well, with a little bit of JavaScript, the problem is solved below. See this working example in PDF.

  • To write the least amount of JavaScript, create field names that are hierarchical. For example, if you have 5 fields - Name, Company, URL, Email, and Phone - if you name them as "color.Name", "color.Company", "color.URL", "color.Email", and "color.Phone" then you only need one JavaScript command to turn on/off all the fields at once.
  • Create custom RGB color at the Document Level:
color.MyColor = new Array("RGB", .75, 1, 1);

Note: Although Acrobat displays the color as 191,255,255 in the Color Palette, you will have to divide each by 255 to get the values required for the color Array.

  • Create a document level JavaScript to "turn off" the color:
function PrintNoColors()
var f=this.getField("color");
f.fillColor = color.white;
  • Create a document level JavaScript to "turn on" the color:
function ResetColor()
var f=this.getField("color");
f.fillColor = color.MyColor;
  • Add the function "PrintNoColors()" on "Doc Will Print" (Tools > JavaScripts > Document Actions)
  • Add the function "ResetColor()" on the "Doc Did Print" (Tools > JavaScripts > Document Actions)
PDF In-Depth Free Product Trials Ubiquitous PDF

Debenu Quick PDF Library

Get products to market faster with this amazing PDF developer SDK. Over 900 functions and an equally...

Download free demo

Five visions of a PDF Day

In the world of PDFs or as we like to say Planet (of) PDF, a year isn't a real PDF year without an intense few days of industry knowledge sharing.

May 15, 2018
Platinum Sponsor

Search Planet PDF
more searching options...
Planet PDF Newsletter
Most Popular Articles
Featured Product

Debenu PDF Aerialist

The ultimate plug-in for Adobe Acrobat. Advanced splitting, merging, stamping, bookmarking, and link control. Take Acrobat to the next level.


Adding a PDF Stamp Comment

OK, so you want to stamp your document. Maybe you need to give reviewers some advice about the document's status or sensitivity. This tip from author Ted Padova demonstrates how to add stamps with the Stamp Tool along with related comments.