Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

JavaScript to Turn Off Form Field Borders when Document is Printed

New Here ,
Feb 05, 2025 Feb 05, 2025

I am doing a massive overhaul of our forms for my job, and I have written several scripts to lock all fields when a button is pressed or to automatically insert today's date in a field so the user cannot backdate the form, but I am wondering if I could write a script that turns the borders of my form fields off when the document is printed. I need to keep the borders so that users who open the forms in Adobe Reader can see the fields, but I don't love the look of that when the document is printed. I would also love for the borders to turn back on after printing.

 

I only have basic Java/JavaScript knowledge, and most of the scripts I've "written" have been adapted from various responses I've seen posted here. I'd love some feedback on this particular issue!

TOPICS
Create PDFs , General troubleshooting , How to , JavaScript , PDF , PDF forms
380
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 05, 2025 Feb 05, 2025

Use the "Will Print" and "Did Print" scripts under "Document Actions".  You can find this tool by searching the tools for "Document Actions"

 

If you want to turn off borders for all fields. Use this code.

 

// Will Print Script

var oFld;

for(var i=0;i<this.numFields;i++){

    oFld = this.getField(this.getNthFieldName(i));

    oFld.tmp = oFld.strokeColor;

    oFld.strokeColor = ["T"];

}

 

 

// Did Print Script, restores borders.  

for(var i=0;i<this.numFields;i++){

    oFld = this.getField(this.getNthFieldName(i));

    oFld.strokeColor = oFld.tmp;

}

 

Note that the border style will also need to be remove and restored.  

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 05, 2025 Feb 05, 2025

Enter the following script as a WillPrint script:

 

for(var i = 0; i < this.numFields; i++)
{
var fieldName = this.getNthFieldName(i);
var oFld=this.getField(fieldName);
if(oFld==null){continue}
oFld.strokeColor=color.transparent;
}

 

Enter the following as a DidPrint script (assuming the borders were black to start with):

 

for(var i = 0; i < this.numFields; i++)
{
var fieldName = this.getNthFieldName(i);
var oFld=this.getField(fieldName);
if(oFld==null){continue}
oFld.strokeColor=color.black;
}

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 05, 2025 Feb 05, 2025
LATEST

You want to change border on all fields?

You can do it using document actions,'Document Will Print' and 'Document Did Print':
Will print:

for (var i=0; i<this.numFields; i++) {
 var fieldName = this.getNthFieldName(i);
 var field = this.getField(fieldName);
 field.strokeColor = color.transparent;}

For 'Did print' change color to whichever you wish.
If you have radio buttons you may want to also change borderStyle.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines