I would like to create order number (following) in header, which would create automatically for each different opening the file by customer. Can I achieve this by using some functions in JS? or another? In attached screen this number should generate in each opening file
-
JavaScript has nothing to do with PDf's. Your question is not clear. – Liam May 11 '16 at 07:33
-
Are you sure? I think JS may be connect to PDF – mrkkr91 May 11 '16 at 07:57
-
No it doesn't. You can't run active code in a PDF. It is a flat document. – Liam May 11 '16 at 08:05
-
do you mean to [generate a PDF file using JavaScript?](http://stackoverflow.com/questions/742271/generating-pdf-files-with-javascript?rq=1) which is possible though (TBH) there are better ways to do it. If you mean this you need to show exactly how your generating the file, which library your using, etc. etc. – Liam May 11 '16 at 08:09
-
Buttons, fields etc in PDF forms can be made to run JavaScript in order to achieve all manner of auto complete and interactive functions. – Westside May 11 '16 at 09:40
-
@Liam: I think I have to send you a few nails from France… Language alert: "nail" in French is "clou"… 'nuff said. My credentials: I have been creating PDF applications (aka smart forms), using JavaScript since it became publicly available… and that was about 18 years ago. Anyway, mrkkr91: see answer. – Max Wyss May 11 '16 at 22:51
2 Answers
I presume that you are using Acrobat Pro to create the PDF form.
The quick and easy way to do this is to auto generate an order number based on the current date and time. Create a text field in your form (I've called mine "ordernumber"), double click it and go to the calculate tab then insert the following two lines into the custom calculation script box:
f = this.getField("ordernumber");
f.value = util.printd("yyyy/ddmm/hhmmss", new Date());
This will give you a unique order code (unless someone creates two orders in the same second!). You can change around the year (yyyy), day (dd), etc to make something that you like as a format.
If the order number needs to conform to an existing format or align with other systems then you would need to get the PDF to access an external database or something like that which would be a bit more complicated and beyond my knowledge.

- 675
- 1
- 7
- 16
It depends on whether your order number has to be unique only, or whether order numbers have to be consecutive.
In the first case, @Chris' answer pretty much gives the solution; you may be fiddling around with the base data, but that's it.
If the number has to be consecutive, there is a possibility if the use of the form can be limited to one single computer. In this case, you would create a Persistent Global Variable (which is a variable that is written back to the system, and can be reused the next time you open the document). See Acrobat JavaScript documentation for code samples. When you open the document, you read in that number, increment it and feed it into your order number field, and write it back.
If the number has to be consecutive, and the order form is used by several users, you will have to maintain the order number externally (which means, on a server). In this case, it might be even better to have a server-side order management, where the user may enter some base data, and then gets the prefilled order form made available.

- 3,549
- 2
- 20
- 26
-
-
Chris' approach does work, and is absolutely solving your problem (within its scope). – Max Wyss May 13 '16 at 07:44