Skip to main content
New Participant
January 24, 2018
Answered

Insert a JPEG into an image field using Excel/VBA

  • January 24, 2018
  • 1 reply
  • 8755 views

Hi,

I have been using a modified version of the code on this website (Read And Write PDF Forms From Excel (VBA) ~ My Engineering World ) to copy text from an excel document to a PDF template and then save the result. Now I want to have the macro also add in an image to the image field in my template. I'm pretty new to VBA coding, so appologies if this is a dumb question.

Basically for my text I was just using "objAcroPDDoc.GetJSObject. GetField.[field name].Value" to highlight the relevant field in the PDF and paste in the value from my excel file. But I'm not sure how to modify it to have it add in an image from a specified file path instead.

Thanks in advance for any help!

This topic has been closed for replies.
Correct answer Thom Parker

First, is the button field setup as "Icon Only"?

Then, are you getting any results at all? Are there any errors reported in the console window?

 

Can you run the JavaScript equivalant of this code from the console window and import the image?

 

1 reply

Thom Parker
Adobe Expert
January 24, 2018

The only PDF field type that can load and display an image is a button field, and it cannot be done with the IAC directly. Instead you have to use the JSO object in the IAC to call JavaScript functions for loading the image.

Here's the JavaScript reference for the "field.importButtonIcon" function needed for loading an image. Be aware that the file path must be an Acrobat Device indepedant path.

Acrobat DC SDK Documentation

Here's an article on Paths in Acrobat JavaScript

https://acrobatusers.com/tutorials/file-paths-acrobat-javascript

And here's the reference page on the JSO

Acrobat DC SDK Documentation

Your best strategy is to develop the script for importing the image entirely in JavaScript, as a folder level function, then call this function from VB, passing in the path and button field name

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
January 15, 2020

Hello,

I have the same problem. Is there still (2 years later :)) no other way to import an image to a pdf image field instead of doing it via button icon?

I tried this solution already but it does not work for me.

This is a snippet of my code:

 

Set objAcroPDDoc = objAcroAVDoc.GetPDDoc
Set objJSO = objAcroPDDoc.GetJSObject


objJSO.getField("image").buttonImportIcon ("/C/test.pdf")

// I also tried : objJSO.getField("image").buttonImportIcon ("/C/test.jpg")

 

Is there someone who can help me with my problem?

 

Thanks already!

 

New Participant
November 25, 2021

Instead of running the JavaScript directly from the JSO.  Create a trusted folder level function for importing the image. Then call this function from the JSO. 

 

The biggest problem with mixing JavaScript and the IAC is that there are huge differences in how objects are handled. Proper object member conversion is not guarenteed, and even documented. Connected objects (such as the field object) are particularly troublesome.  So, a conversion that worked, could stop working in any update. And apparently this is now happening more frequently than it used to. The solution is avoid using any kind of JS objects in the IAC. Create JS folder level functions instead, then call these functions with basic types, numbers, strings, and arrays. This way there is never a problem with object handling. 

 

 


Thank you for your reply and your good explanation. I will try to fix it in that way. 🙂