Skip to main content
Participant
July 26, 2023
Answered

"Move" button creation by JS

  • July 26, 2023
  • 1 reply
  • 365 views

Hello,

I am beginner in javascript in acrobat.

My goal is to create signature fields by javascript (executed from action wizard).

I already made all fields with no problems (one field as example)

var cm = 72 / 2.54;
var aRect = this.getPageBox( {nPage:0} );
aRect[0] += 7.5*cm;
aRect[2] = aRect[0]+7*cm;
aRect[1] -= 19.73*cm;
aRect[3] = aRect[1]-0.7*cm;
var f = this.addField("Created", "signature", 0, aRect);

Now I need to center all fields to the scanned original.

The reason is that documents to be signed are mostly scanned, so they are shifted and rotated a little bit, so fields which I add are not fits to the scanned original.

I write moving scripts for all fields together in selected direction (rotation is not necessary).

var mm = 72 / 25.4;
var s = this.getField("Created");
var aRect = s.rect
aRect [1] += 0.5
aRect [3] += 0.5
s.rect = aRect

Now I want to create buttons located on the unused region of original document for moving fields left, right, up and down.

All shall be dony using JS.

My idea is to use "setAction" for button field named "UP", which will execute moving script as provided above.

How to create in JS "UP" button which will call moving script as provided?

This topic has been closed for replies.
Correct answer try67

Place the code in a doc-level function, and then call it from the MouseUp event of the button field.

To set that action you can use the following:

 

this.getField("UP").setAction("MouseUp", "moveFieldUp();");

 

To add a doc-level function use this:

 

var moveUpCode = "\
function moveFieldUp() {\n\
var mm = 72 / 25.4;\n\
var s = this.getField(\"Created\");\n\
var aRect = s.rect;\n\
aRect [1] += 0.5;\n\
aRect [3] += 0.5;\n\
s.rect = aRect;\n\
}";
this.addScript("MoveUp", moveUpCode);

 

If you don't want it to only be able to move one field then you could pass the field name as a parameter to this function.

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
July 26, 2023

Place the code in a doc-level function, and then call it from the MouseUp event of the button field.

To set that action you can use the following:

 

this.getField("UP").setAction("MouseUp", "moveFieldUp();");

 

To add a doc-level function use this:

 

var moveUpCode = "\
function moveFieldUp() {\n\
var mm = 72 / 25.4;\n\
var s = this.getField(\"Created\");\n\
var aRect = s.rect;\n\
aRect [1] += 0.5;\n\
aRect [3] += 0.5;\n\
s.rect = aRect;\n\
}";
this.addScript("MoveUp", moveUpCode);

 

If you don't want it to only be able to move one field then you could pass the field name as a parameter to this function.