Use Acrobat Javascript to add button field with action goto a page view

Copy link to clipboard
Copied
I am trying to use Javascript to automatically insert buttons in a document that link to other pages within the document. In order to place the button I use this.addfield, however cant find the syntax for the setAction statement.
So I then tried to set it to run a javascript making use of cscript with to set this.pageNum = x, however when ever I check the properties of the button the pageNum has been changed to the page the button has been placed on.
What syntax to I need to place a button field onto a page with the selected action going to a page view. Or how do I write the javascript to use this.pageNum to another page in the pdf.
Copy link to clipboard
Copied
A script can only apply JavaScript-based actions, but the method you described should have worked just fine... Can you maybe share the code you used to create the buttons? It sounds like you have some issues there.
Copy link to clipboard
Copied
Use something like this:
f.setAction("MouseUp", "this.pageNum = x;");

Copy link to clipboard
Copied
Hi
In the document I have, the sections have page numbering depending on the chapter. So the page number mayl be shown as "N-1" and be "N1" within acrobat. So to generate the page link in the script the letter is put in varWord and the number in linkpage. qQuads comes from another script to give the position for the button.
When tested the correct info is shown from
app.alert(pageRef);
eg. "pageNum = N101"
however when I check the button properties the following script is present:
this.pageNum = N2;
I have tried:
f.setAction("MouseUp", "this.pageNum = " +varWord + linkPage + ";");
which also resulted in a javascript beint added to the button.
function addbutton(varWord, thisWord, thisPage, qQuads, NormalPage)
{
// view page to have button added
this.pageNum = thisPage;
// work Quads of reference
var sQuads = qQuads.toString();
var aQuads = sQuads.split(",");
// get page rect dimensions
var pgRect=this.getPageBox("Crop", thisPage);
var fldRect=[];
// apply rect to make button dimension to cover width of page and apply offset fix
pgRect[0] = 0;
pgRect[1] = +aQuads[5]-234;
pgRect[2] = +423;
pgRect[3] = +aQuads[1]-234;
// set up and get page number to link to
var linkPageVal=thisWord+1;
var linkPage = this.getPageNthWord(thisPage, linkPageVal);
// confirm the page number to which the button should link
var pageRef = "pageNum = " + varWord + linkPage;
app.alert(pageRef);
// generate cscript for the button
var cscript = "this.pageNum = " +varWord + linkPage + ";";
// place button field and set parameters
var f = this.addField("actionField", "button", thisPage, pgRect);
f.setAction("MouseUp", cscript);
f.delay = false;
f.fillColor = color.transparent;
f.buttonSetCaption("");
f.borderStyle = border.s;
f.display = display.visible;
f.lineWidth = 1;
f.strokeColor = color.black;
f.highlight = highlight.o;
f.delay = false;
}
Copy link to clipboard
Copied
You can't use the page labels for this. You must only use the physical page number. The first page is 0, the second page is 1, etc.

Copy link to clipboard
Copied
I have adjusted the script to point to the physical page, which I confirm with the app.alert(cscript). However when I check the button properties the pageNum has changed and points to the page with the button on it.

Copy link to clipboard
Copied
If I run the process for one button then the script is correct. So assume, as all buttons have the same name that the javascript is modified to the last one declared. I will try giving each button an individual name.

Copy link to clipboard
Copied
I renumbered each button and converted the page label to the absolute page number for it to work.
Copy link to clipboard
Copied
Post your new code, including how you're calling it.

