These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Hiding Fields at PDF Open

Registered: Jul 26 2010
Posts: 10

I'm looking for a Javascript sample of how to show/hide fields when a document opens based on the values of a specific field (txtCost).

I currently have this script in place for validation on txtCost field, which works great when the document is edited the first time, but when it is saved and opened again, the fields are not displaying correctly.

var f = this.getField("txtVicePresidentSignature");
f.hidden = (event.value < 1001);
var f = this.getField("txtVicePresidentSignatureDate");
f.hidden = (event.value < 1001);
var f = this.getField("cmbVicePresident");
f.hidden = (event.value < 1001);
var f = this.getField("txtPresidentSignature");
f.hidden = (event.value < 5001);
var f = this.getField("txtPresidentSignatureDate");
f.hidden = (event.value < 5001);

Thanks.

David C. Ross
Webmaster
Mitchell Community College

My Product Information:
Acrobat Pro 9.3.1, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Instead of using the event object, you will need to access the field directly, like so:

var f = this.getField("txtVicePresidentSignature");
f.hidden = (this.getField("txtCost").value < 1001);etc.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
When a PDF opens, there are some events that get automatically processed by the JavaScirpt interpretor, the document level scripts and the page open action. So you could place the code with the full field reference in the Document Level scripts. You should also keep the validation script. You could even create a function for this action that could be used by both the initialization and validation actions.

[url=http://www.acrobatusers.com/tutorials/2007/07/js_document_scripts]Entering Document Scripts[/url] by Thom Parker.

George Kaiser

Registered: Jul 26 2010
Posts: 10
Thanks guys for the quick responses...

But it still doesn't seem to be working.

Here's the new script (based on Try67's response):

function Init()
{
var f = this.getField("txtVicePresidentSignature");
f.hidden = (this.getField("txtCost").value < 1001);
var f = this.getField("txtVicePresidentSignatureDate");
f.hidden = (this.getField("txtCost").value < 1001);
var f = this.getField("cmbVicePresident");
f.hidden = (this.getField("txtCost").value < 1001);
var f = this.getField("txtPresidentSignature");
f.hidden = (this.getField("txtCost").value < 5001);
var f = this.getField("txtPresidentSignatureDate");
f.hidden = (this.getField("txtCost").value < 5001);
}

And I put it in the Document Level scripts (as per Gkaseril's post).

Not sure if this matters, but the Signature and Date fields are initially hidden (under field properties), and should only be shown if the value in txtCost is > 1000 and > 5000.
But this is working on the field validation level.

David C. Ross
Webmaster
Mitchell Community College

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Either call init() or take the code out of the function, or it won't run.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Registered: Jul 26 2010
Posts: 10
So its case-sensitive then?

David C. Ross
Webmaster
Mitchell Community College

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Yes, but that was just a mistake on my part.
The point is that code inside a function won't be executed unless the function is called.
So in this case:

Init()
function Init() {
...
}

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Registered: Jul 26 2010
Posts: 10
Ahhh... gotcha. I'll try that, thanks.

David C. Ross
Webmaster
Mitchell Community College

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
I would have written the document level code as:
function FieldDisplay(nTxtCost){var f = this.getField("txtVicePresidentSignature");f.hidden = (nTxtCost < 1001);var f = this.getField("txtVicePresidentSignatureDate");f.hidden = (nTxtCost < 1001);var f = this.getField("cmbVicePresident");f.hidden = (nTxtCost < 1001);var f = this.getField("txtPresidentSignature");f.hidden = (nTxtCost < 5001);var f = this.getField("txtPresidentSignatureDate");f.hidden = (nTxtCost < 5001);return;} FieldDisplay(this.getField("txtCost").value);

The validation script for the the "txtCost"field would then be:
FieldDisplay(event.value);

George Kaiser