最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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 pared 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 date/time?

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 pared 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 date/time?
Share Improve this question edited May 28, 2021 at 19:12 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 22, 2011 at 19:19 Martin ScharrerMartin Scharrer 1,4531 gold badge18 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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)

  1. 暂无评论