Skip navigation
pysicsmaster
Currently Being Moderated

Need help with code for adding dates to form

Jun 7, 2012 10:53 PM

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.

 
Replies
  • Thom Parker
    216 posts
    Jul 16, 2003
    Currently Being Moderated
    Jun 8, 2012 11:23 AM   in reply to pysicsmaster

    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-acroba t-javascr
    ipt

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

    http://acrobatusers.com/tutorials/working-with-date-and-time-in-acroba t-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?

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 9, 2012 9:50 AM   in reply to pysicsmaster

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 9, 2012 4:54 PM   in reply to pysicsmaster

    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.

     
    |
    Mark as:
  • Thom Parker
    216 posts
    Jul 16, 2003
    Currently Being Moderated
    Jun 10, 2012 11:25 AM   in reply to pysicsmaster

    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,

     
    |
    Mark as:
  • Thom Parker
    216 posts
    Jul 16, 2003
    Currently Being Moderated
    Jun 10, 2012 4:10 PM   in reply to pysicsmaster

    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.

     
    |
    Mark as:
  • Thom Parker
    216 posts
    Jul 16, 2003
    Currently Being Moderated
    Jun 10, 2012 7:47 PM   in reply to pysicsmaster

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points