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

Getting page number

nroof
Registered: Apr 29 2008
Posts: 4

Hi all,
I am new to JavaScript in Acrobat and I have what I believe to be a simple question.

I have a multipage document that I need to place certain information on each page. On page 1 I have some form fields that the user can put some information in and I have 1 output filed that concatenates these fields and places it on all subsequent pages. As part of the output field I need to include a page number. This is the only part I have not been able to figure out.

Better Break down:
Page 1.
InputText1:
InputText2:

All pages include:
OutputText1 = InputText1 + InputText2 + page number.

Here is an example of the code I have in "Output1"

event.value = this.getField("VersionCode").value + this.getField("ProductCode").value

I have tried using "this.pageNum" with no success.
Because I am copying the "OutputText1" text field to all pages they are all linked to the one on page 1 which gives me an output of 0 for the page number.

The reason I am doing this is to create a static set of images to be printed under multiple conditions. I want the prints automatically labeled using operator input on page 1 of the pdf. Part of what I am outputting is a barcode and it is important for me to know the page number.

I do have Live Cycle Designer but I can't use it for this application.

Any help would be greatly appreciated.
Thanks

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Each "OutputText" field on each page will need a unique name as text fields in AcoroForms (Acrobat forms) with the same name are the same field and any change to one is a change to all.

What are your input values and what value are you getting for an output value when those input values are used?

For this type of computation I prefer to use the "fillin" function provided in the PDF sample forms as it forces all input values to a string and thus concatenates and does not add numeric fields. One also may have to adjust for the zero based page numbering used within JavaScript.

If I use "12" for the version code and "34" for the product code, the result for your calculation is "46". Even if I include the page number

Document level function:


 // Concatenate 3 strings with separators where neededfunction fillin(s1, s2, s3, sep) { /*Purpose: concatenate up to 3 strings with an optional separator

 inputs:

s1: required input string text or empty string

s2: required input string text or empty string

s3: required input string text or empty string

sep: optional separator sting

 returns:

sResult concatenated string

*/
 // variable to determine how to concatenate the stringsvar test = 0; // all strings nullvar sResult; // re slut string to return // force any number string to a character string for input variabless1 = s1.toString();s2 = s2.toString();s3 = s3.toString(); /*assign a binary value for each string present

so the computed value of the strings will indicate which strings are present

when converted to a binary value

*/
if (s1 != "") test += 1; // string 1 present add binary value: 001if (s2 != "") test += 2; // string 2 present add binary value: 010if (s3 != "") test += 4; // string 3 present add binary value: 100 /* return appropriate string combination based oncalculated test value as a binary value

*/
switch (test.toString(2)) { case "0": // no non-empty strings passed - binary 0sResult = "";break; case "1": // only string 1 present - binary 1sResult = s1;break; case "10": // only string 2 present - binary 10sResult = s2;break; case "11": // string 1 and 2 present - binary 10 + 1sResult = s1 + sep + s2;break; case "100": // only string 3 present - binary 100sResult = s3;break; case "101": // string 1 and 3 - binary 100 + 001sResult = s1 + sep + s3;break; case "110": // string 2 and 3 - binary 100 + 010sResult = s2 + sep + s3;break; case "111": // all 3 strings - binary 100 + 010 + 001sResult = s1 + sep + s2 + sep + s3;break; default: // any missed combinationssResult = "";break;} return sResult;}

Field custom calculation script:

 event.value = fillin(this.getField("VersionCode").value, this.getField("ProductCode").value, (this.pageNum + 1), "");

George Kaiser

nroof
Registered: Apr 29 2008
Posts: 4
gkaiseril,
Thanks for the reply.
Sorry for the ambiguity of the data types.

"Each "OutputText" field on each page will need a unique name as text fields in AcoroForms (Acrobat forms) with the same name are the same field and any change to one is a change to all."

I want all of the outputs to be the same except the page number. I may have need in the future to make changes and I don't want to have to make the changes in multiple locations. This would be time consuming and prone to errors.

"What are your input values and what value are you getting for an output value when those input values are used?"

These values will be alpha numeric although I can make them all strings.

So if we use the following:

Input1 = "D"
Input2 = 1234

The output I need on all pages is:

Page 1 - D1234 0001
Page 2 - D1234 0002

I presently have pdf's upwards of 150 pages and having unique output names could be troublesome.



Thanks,
Norm
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
The code for each page's output text field can be:

 if (this.getField("VersionCode").value != "" & this.getField("ProductCode").value != 0) {var InputText1 = this.getField("ProductCode").value;var InputText2 = this.getField("VersionCode").value;var PageString =  1 + this.pageNum; // page number adjusted for zero based page numberingPageString = util.printf("%,304d", PageString); // format to leading zeros and a length of 4 charactersPageString = fillin(" ", PageString, "", ""); // add leading spaceevent.value = fillin(InputText1, InputText2, PageString, "");} else {event.value = ""; // clear field value}

George Kaiser