10 Replies Latest reply: Jun 10, 2012 8:36 PM by pysicsmaster RSS

    Need help with code for adding dates to form

    pysicsmaster

      Hello forum goers

       

      I'm new to making forms and figured out how to auto add the date, however I need the form to change the date for every copy made.

       

      For example today is 06/08/2012 if I print 10 copies of the form it will output 10 pages ranging from 06/08/2012 to 06/17/2012. If code exists to do this I would be very gratefull to whoever helps, I also wouldn't mind if that is not possible for manually inputing the start / end dates.

       

      Currently I print 15-30 copies of the form and hand write each of the dates but I'm just getting to busy to do that. I also cannot print one a day it must be in batches.

       

       

      Thanks in advanced.

        • 1. Re: Need help with code for adding dates to form
          Thom Parker CommunityMVP

          What you are asking for is more complex than just setting the current date. Each time the form prints it has to know that it has to change the date. I would suggest doing this.

           

          First, setup a document level script to set the date to the current date. I suspect that you have already done this?

           

          Next, Create a "DidPrint" document action to increment the date.  To do this the script will need to scan the current text value of the date, add one day to it, and then reformat it.  You'll find information on this type of scripting in these articles:

          http://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascr
          ipt

          http://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascr
          ipt-part-2

          http://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascr
          ipt-part-3

           

          Since the increment happenes in the Did Print you will need to print each copy individually. If you enter 10 copies in the print dialog it won't work. You have to print one at a time.  You can automate this activity with a console script.

           

          One of the advantages of incrementing in the DidPrint is that you can also manually enter a date and it will increment from there.

           

           

          Thom Parker
          The source for PDF Scripting Info
          pdfscripting.com

           

          The Acrobat JavaScript Reference, Use it Early and Often

           

          Then most important JavaScript Development tool in Acrobat
          The Console Window (Video tutorial)
          The Console Window(article)

           

          Having trouble, Why Doesn't my Script Work?

          • 2. Re: Need help with code for adding dates to form
            pysicsmaster Community Member

            Thanks for the info.

             

            Due to restrictions on the computer the consule script won't be possible. I think the best way to do it would be to have one page (first page) where you set the date/auto sets for today and x (15-30) additional pages where each page has a different date field and has a java script code that takes the date from the first page and adds 1 day to that and so on till the end of the document.

             

            Would this method be possible? If so do you have the code I'm not the best with javascript, and I couldn't find info on this from your links (especially making sure months don't have 32 days), unless the code needed has to do with getting the milliseconds etc.

             

            --Thomas

            • 3. Re: Need help with code for adding dates to form
              GKaiseril CommunityMVP

              You could create a non-printable button with a script that asks how many copies to print and that button uses a loop to print one page and then increment the date and print the next copy.

              • 4. Re: Need help with code for adding dates to form
                GKaiseril CommunityMVP

                There is JavaScript console within Acrobat and it is limited to the scripts within Acrobat and it is used to debug the JavaScript within  Acrobat.

                • 5. Re: Need help with code for adding dates to form
                  Thom Parker CommunityMVP

                  I think you misunderstood my comment about the Console Window. Please view the Console Window tutorial link in my previous response. The Acrobat JavaScript Console Window is internal to Acrobat, so there aren't any system restrictions that could prevent you from using it. It is a perfect place to run short automation scripts that do things like print out separate copies.

                   

                  George also points out an excellent solution, put the script in a (non-printable) button on the document itself. However, due to security restrictions this solution can be a bit awkward, but no more so than using the Console window. Each solution takes a little extra effort.

                   

                  The problem with haveing multiple pages is that you need to manually create a document with all the pages already in it.  This is certainly a doable solution, It just limits you to the number of pages you've already created manually.  At least for an AcroForm Solution. If you recreate your form as a Dynamic LiveCycle, then creating new pages on the fly is not an issue.

                   

                  Any of these solutions will work just fine, the real trick is to increment the date. 

                   

                  Here's a short script that starts with todays date and adds one day for each iteration of the loop and then prints

                   

                  // One day in milliseconds

                  var nOneDay = 24 * 60 * 60 * 1000;

                  var nToday = (new Date()).getTime();

                   

                  for(var i=0;i<15;i++)

                  {

                      oTheDate = new Date(nToday + i*nOneDay);

                      // Format Date

                      cTheDate = util.printd("mm/dd/yyyy",oTheDate);

                      this.getField("MyDate").value = cTheDate;

                      this.print()

                  }

                   

                  This same loop could be used to fill in fields on a multi- (repeated) page form.

                  Using the util.scand() function, this loop could be setup to start at any arbitrary date.

                   

                  Cheers,

                  • 6. Re: Need help with code for adding dates to form
                    pysicsmaster Community Member

                    Yes I did misunderstand your first post, based on what you said there should be no problem as soon as I get more time to mess with the file I'll get back to you (however they are working me to death so it might be a few days)

                     

                    lol thanks for the help

                     

                    also will the code take into account different months like if I do the print on 6/30, will it know when to do 7/1

                    • 7. Re: Need help with code for adding dates to form
                      Thom Parker CommunityMVP

                      The date calculation is done with milliseconds since 1970, then formatted as date text. Quanties such as day and month are not used, it is the formatting that determines the year, month, and day.

                       

                      Now if you enter something like 6/30, it is the date parser that determines how the numbers are interpreted, the "util.scand()" function. And I believe it will read bad dates like this correctly.  But you'll need to test it out.

                      • 8. Re: Need help with code for adding dates to form
                        pysicsmaster Community Member

                        Thanks for the additional info, and I had enough time to try it out.

                         

                        The code you gave works and I added a button to print instead of it instantly poping up on open. So basically I just have to set my printer settings (certain paper tray / hole punching) and spam click print as it pops up 15 times or if I change it to 30, 30 times?

                         

                        There is no code that can be within the pdf file, that will auto accept all the print promts after the first one? Spam clicking print is good enough but having code to do it itself would be much better.

                        • 9. Re: Need help with code for adding dates to form
                          Thom Parker CommunityMVP

                          The problem with automating print in a document is that it is a privileged operation. Which is why you get the popups, and why I suggested running the code from the console window. The console window is privileged. However, I believe that if you list the document as "trusted" in the Enhanced Security privileges pane in the Acrobat Preferences, that Acrobat will allow silent printing from a script in that document.

                          • 10. Re: Need help with code for adding dates to form
                            pysicsmaster Community Member

                            Hmm those options are already enabled, o well for simplicities sake the people using the form (9 times out of 10 me) will just need to hit print over and over.

                             

                            Thank you very much for solving this