Skip to main content
November 18, 2013
Answered

How to create sequential number field?

  • November 18, 2013
  • 6 replies
  • 60924 views

I have created a pdf file and I'd like to have a field with sequential numbers.

For instance, if I open my pdf now and it has number 1 in the field, 'd like to have number 2 in the field the next time that I open the file, and so on...

Correct answer try67

Go to Tools - JavaScript - Document JavaScripts and create a new script with the following code (remove any default code that's inside the window when you first open it):

var f = this.getField("Serial Number");

f.value = Number(f.value)+1;

f.defaultValue = f.value;

Of course, you'll might need to adjust the field name in the first line of code.

6 replies

Participating Frequently
December 12, 2022

I have just added your java script to my form, but I would like to know if it can be modified to add a fixed numeric at the begin of the sequential number that will be created each time the form is open. For example, I would like to add 23 - to the beginning to signify the year 2023. Is there a way I can do this so that the 23 - is fixed in the code somewhere, and then I can go in and change it for 2024?

 

Thanks,

Nick

try67
Community Expert
Community Expert
December 12, 2022

Yes, like this:

 

var prefix = "23-";
if (this.documentFileName=="Original file name.pdf") {
	var num = this.getField("Text2");
	num.value = prefix  + Number(num.valueAsString.replace(prefix, ""))+1;
	num.defaultValue = num.valueAsString;
}
Participating Frequently
December 12, 2022

I copied this Java Script and changed the one I already had, and it is not changing anything. Now the number won't increase and the 23 - is not showing up.

 

Participant
October 8, 2021

I have the sequential number field using this coding: 

var num = this.getField("Text2");
num.value = Number(num.value)+1;
num.defaultValue = num.value;

 

BUT, I actually don't want the number to change after the doc is filled out. It's a work order so once the doc is opened, new number is generated, the rest of the form is completed and it's saved. I need this document to keep this same work order number idefinitely. 

 

Is it possible to have a "Master" doc that generates sequential numbers but then once it's saved as a new name, the number stays the same? So employees can open a single Master doc when they get ready to provide an estimate and each time the work order generates a new +1 number. Then, once they save and rename the document, the number sequence no longer changes. 

try67
Community Expert
Community Expert
October 8, 2021

The best way to do that is to set the "master" file (and the folder where it's located) as read-only.

If you want to make it dependent on the file name, though, that's possible too.

Adjust the code like this:

 

if (this.documentFileName=="Original file name.pdf") {
	var num = this.getField("Text2");
	num.value = Number(num.value)+1;
	num.defaultValue = num.value;
}
russb73725712
Known Participant
July 17, 2020

I can get the sequential numbering to work if I keep using the last form that was saved.

The master form does not update. 
Is there a way for the master form to update each time you open it?

I'm opening master form/adding my data/then save as with new file name/close tab/open original master form and number is the same.

Need a little help.

try67
Community Expert
Community Expert
July 17, 2020

Save it over the "master form", then, not under a new name...

russb73725712
Known Participant
July 17, 2020

I was able to get it to work that way, but that seems counter intuitive. I don't want to have to rename the file after everytime.

There's gotta be another way?

I've seen these global object scripts, that apparently works when using just your own computer, but haven't had any success, although, I'm probably doing it wrong. 

I tried to add a required one time click button to add +1 to current number, but not sure how to create a "one time" click funcion.

The only thing that I was able to make work is use the date and time to create a unique number, but then it's not really sequential. 

I'm hoping to make the sequential number work somehow. 
I'll keep trying. 

 

 

 

 

francj
Participant
August 15, 2019

Very usefull for me - thanks!

How can I modify script to do NOTHING, when field "Serial Number" is empty?

try67
Community Expert
Community Expert
August 15, 2019

Change it to this:

var f = this.getField("Serial Number");

if (f.valueAsString!="") {

    f.value = Number(f.value)+1;

    f.defaultValue = f.value;

}

francj
Participant
August 21, 2019

Thanks for Your reply, I used very similar code (from my friend):

var f = this.getField("Serial-Number");

if (f.value != "" && f.value != null) {

    f.value = Number(f.value)+1;

    f.defaultValue = f.value;

}

Works fine.

By the way, how can I tag code as “code“ :-), like You in Your last post?

Krizzzle
Participant
November 29, 2017

I am trying to do something similar, but more difficult. I need to print a 4 page PDF, it has a number field with a 3 digit number in the corner of the first page. I need the number to increase every time it prints, however, I print many copies at once, sometimes 50 to 100. Is there a way to set it to print that many copies and increase that number after each copy? Ive been trying to figure this out for about a year now, if anyone could help, please let me know.

Thom Parker
Community Expert
Community Expert
November 29, 2017

If you tell the printer to print X copies, then it handles the printing, outside of Acrobat. The only way to modify a value on the PDF is to control the printing yourself. A script would do this easily from a Folder level script, Command Script, or the Console window.

Contact me if you'd like such a script written.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Krizzzle
Participant
November 29, 2017

Well that would solve all my problems if I knew how to print outside of acrobat. Im using a Mac and Ive never printed a document outside of its program. My only other issue is being able to set print settings. Its not just a plain paper document, its an exam. Its printed on 11 x 17 cardstock then folded by the printer, so I would need to incorporate those print settings as well. Not sure if its doable, but I will keep trying to find a solution to this problem. I appreciate the response.

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
November 18, 2013

Go to Tools - JavaScript - Document JavaScripts and create a new script with the following code (remove any default code that's inside the window when you first open it):

var f = this.getField("Serial Number");

f.value = Number(f.value)+1;

f.defaultValue = f.value;

Of course, you'll might need to adjust the field name in the first line of code.

VictoriaD
Participating Frequently
April 18, 2022

Hi!  Hopefully you are still helping others with PDF forms. 

 

I am also trying to do this- sequential numbering on a PDF form when it is opened a new number is genertated based on the previous number used.  I am not a programmer, and I am not really familiar with javascript.

 

I used the script that you provided as an answer to the post here- obviously I did not do something right.

 

This is what I used-

var f=this.getField("WORK ORDER");
f.value=002038(f.value)+1;
f.defaultValue=f.value;

 

I deleted -( not sure if I was supposed to)

function SEQ NUMBERS()
{
}

 

I will also attach a copy of my form.

 

Thank you in advance for your assistance.

 

VictoriaD
Participating Frequently
April 19, 2022

You need to reset the file once first, but yes.


I'm sorry- I don't know what that means, reset the file?