Skip to main content
Participant
May 6, 2021
Answered

Javascript- Insert a specific image into the pdf

  • May 6, 2021
  • 3 replies
  • 10160 views

Hi,

Is there a way to use Javascript to insert an image into a PDF?

It would be something along the lines of:

Insert image from C:\Images\image.png...

and then the image would hover by the cursor to then be placed in the PDF.

I can use the image insert button, but I want to skip the step of choosing a file. 
I regularly insert the same file into PDFs and it would be quicker to go straight to that image.

 

Thanks.

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
This topic has been closed for replies.
Correct answer Yehuda5FA5
It’s usually in the same place but every now and then it needs to move to another place.

I was able to use a dynamic stamp with the following code to accomplish what I was trying to do.

The tricky part is getting the real name of the stamp. That’s on line 3 in the quotes starting with the #.

Shoutout to https://acrobatusers.com/tutorials/dynamic_stamp_secrets/ for help figuring this all out.

Check out their PDF sample.

var cAsk = "Enter the Date of the Wire" ;

var cTitle = "Wire Date ";

if(event.source.forReal && (event.source.stampName == "#ZPjduwxgTAoCc5syxiNeSA"))

{

  var cMsg = app.response(cAsk, cTitle);

  cMsg += "/2021 "

  event.value = cMsg;

  event.source.source.info.DocumentState = cMsg;

}

3 replies

ls_rbls
Community Expert
Community Expert
May 8, 2021

Just curious, why are you considering only the use of stamps in this workflow?

 

Does the users of the form require to to have the freedom to place an image in whatever part of that document, or will this image be located always in a precise location?

 

Yehuda5FA5AuthorCorrect answer
Participant
May 10, 2021
It’s usually in the same place but every now and then it needs to move to another place.

I was able to use a dynamic stamp with the following code to accomplish what I was trying to do.

The tricky part is getting the real name of the stamp. That’s on line 3 in the quotes starting with the #.

Shoutout to https://acrobatusers.com/tutorials/dynamic_stamp_secrets/ for help figuring this all out.

Check out their PDF sample.

var cAsk = "Enter the Date of the Wire" ;

var cTitle = "Wire Date ";

if(event.source.forReal && (event.source.stampName == "#ZPjduwxgTAoCc5syxiNeSA"))

{

  var cMsg = app.response(cAsk, cTitle);

  cMsg += "/2021 "

  event.value = cMsg;

  event.source.source.info.DocumentState = cMsg;

}
Thom Parker
Community Expert
Community Expert
May 17, 2021

You'll find a video here which covers everything you'll ever want to know about the stamp name and several ways to find and change it.

 

BTW: the last line of your code won't work in Reader, and will cause the script to fail because the "info" property can't be changed on that platform. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
May 6, 2021

> and then the image would hover by the cursor to then be placed in the PDF.

This part is not possible, but the rest is. You can use a stamp, as mentioned, or a button field, or even insert it as a watermark. The latter will allow you to add text on top of it, but it's the more difficult one to place.

If you add it as a stamp or a button field you can then flatten the page and add text on top.

Bernd Alheit
Community Expert
Community Expert
May 6, 2021

You can use a stamp for this.

Participant
May 6, 2021
Thanks.

I tried that before, but the problem with that is I need to type over the image I'm inserting.

You can't add text on top of a stamp. (If I'm incorrect on that, please let me know.)
Thom Parker
Community Expert
Community Expert
May 7, 2021

You can add text on top of a button image or a stamp, you just can't do it externally (unless flattened), it has to be done in that object (i.e. button or stamp). How much text do you need to place over the image?

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often