开发者

Placing current date into a text field of a PDF form using JavaScript

开发者 https://www.devze.com 2023-02-18 06:34 出处:网络
I like to create a PDF which when printed out includes the date and time of when it was printed. Because actually changing the PDF content would require a PDF editor I thought using a PDF form (i.e. a

I like to create a PDF which when printed out includes the date and time of when it was printed. Because actually changing the PDF content would require a PDF editor I thought using a PDF form (i.e. a PDF which contains a fillable form) where one text field is automatically updated with the current date and time when the PDF is printed. The actual creation of the PDF is not target of this question (the goal is to use pdflatex which can place JavaScript into PDFs).

I know that PDFs can include JavaScript code, but there are limitation compared to websites. Is it possible, i.e. supported by the Adobe PDF JavaScript API, to have a script which:

  1. is triggered when the PDF is printed
  2. can modify a text field or something else which is printed with the normal text
  3. update this field with the current d开发者_如何学Cate/time?


Yes, it is possible. The PDF documents support a WillPrint action that is executed before the document is printed. You set this action with a javascript action that retrieves the current date and time and sets a field value with this date.

The thing is that not all PDF viewers support Acrobat javascript code so the WillPrint action might not be executed.


After seeing this post, I wrote an article for our PDF product - Creating Print-Only PDF Content - to show that this is indeed possible. What I have done is create a form field that is hidden on the screen but available when printed. I then added a JavaScript PDF action to the "before print" event of the document so that the form field value was set to the current date and time.

DISCLAIMER: I work for Gnostice.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号