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

How to create sequential number field?

Guest
Nov 17, 2013 Nov 17, 2013

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...

55.4K
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
1 ACCEPTED SOLUTION
Community Expert ,
Nov 18, 2013 Nov 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.

View solution in original post

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 ,
May 09, 2022 May 09, 2022

Check the JS Console (Ctrl+J) for error messages after you open the file.

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
Guest
May 09, 2022 May 09, 2022

@try67  Dont know why but it is working now.  Nothing changed on it but it is working.  I appreciate you helping all of us!

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
New Here ,
Jul 12, 2022 Jul 12, 2022

I utilized the same script and it worked great to create the sequential number; however, the number changes in the Receipt every time I opened the saved completed form. How can I correct this?

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 ,
Jul 12, 2022 Jul 12, 2022

Yes, the code is set to execute each time the file is opened. If you don't want that to happen you need to define some kind of condition to stop it from doing so, and then add that to the code.

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
New Here ,
Jul 12, 2022 Jul 12, 2022

Thank you for your expedient response. Whew - ceate a code. I'll see hat I can do! certainly not my expertise. 

Ms. V

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 Beginner ,
Nov 29, 2017 Nov 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.

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 ,
Nov 29, 2017 Nov 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 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 Beginner ,
Nov 29, 2017 Nov 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.

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 ,
Nov 29, 2017 Nov 29, 2017

I think you misunderstood me.  After you press the print button, Acrobat sends the print job off to the printer. This is what I meant by outside Acrobat.  You always print outside Acrobat, because Acrobat is not a print driver.

Now what you want to do is write an Automation script in Acrobat that prints one copy at a time, changing the increment number each time.

Contact me if you want consulting/development on this issue, or post this question to a new thread if you want more free help.

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
New Here ,
Aug 15, 2019 Aug 15, 2019

Very usefull for me - thanks!

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

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 ,
Aug 15, 2019 Aug 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;

}

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
New Here ,
Aug 21, 2019 Aug 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?

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 ,
Aug 21, 2019 Aug 21, 2019

I do it via the "Advanced Editor", but it's possible you won't have access to it...

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
Explorer ,
Jul 16, 2020 Jul 16, 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.

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 ,
Jul 17, 2020 Jul 17, 2020

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

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
Explorer ,
Jul 17, 2020 Jul 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. 

 

 

 

 

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
Explorer ,
Jul 17, 2020 Jul 17, 2020

Forgot to mention that I'm also trying to find a way to save the file as per one of the fields I'm using. Want it to save as per the customer name. 

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
New Here ,
Aug 18, 2021 Aug 18, 2021

I have a similar question. I can get the sequential numbering to work.  However, if I keep using the last form that was saved the pdf form updates each time it is opened and closed. Is there a way for the master form to update only when the document is opened? The master form is saved on a shared drive. In theory, I have designed a master form for all staff to utilize, type into, print, save as, and close.  I would like a sequential number generated each time it is opened. Any help would be appreciated.  Thank you.

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 ,
Aug 18, 2021 Aug 18, 2021

A document level script runs everytime the document is opened.  So put your increment code in a document level script and it will run everytime the document is opened. 

 

 

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
New Here ,
Aug 20, 2021 Aug 20, 2021

Thanks Thom!

Below is my current document level script.  Again, the problem that I am running into is that the PO advances each time the document is opened, printed and saved.  I only want the PO number to advance when the document is opened.  Also, the mater document is saved on a shared drive.  Thank you again for your help!

 

var f = this.getField("PO");
f.value = Number(f.value)+1;
f.defaultValue = f.value;
elsef.value = util.printf("%06d", event.value) ;

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 ,
Aug 20, 2021 Aug 20, 2021

that last line is an error, delete it. 

 

This document level code is only run when the document is opened. Therefore you must have scripts in the Document Save and Print actions that are also setting the value.  Or perhaps you have a calculation script somewhere that is setting the field value, but I can guarantee you that the document script is only run when the document is opened. 

 

 

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
New Here ,
Aug 23, 2021 Aug 23, 2021

Hi Thom.

I deleted the last line. I do not have any other scripts in the document. I have snipped and attached the all JavaScripts editor box for your review.  Thank you for all your help!

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
New Here ,
Oct 08, 2021 Oct 08, 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. 

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 ,
Oct 08, 2021 Oct 08, 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;
}
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
New Here ,
Dec 11, 2022 Dec 11, 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

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