Copy link to clipboard
Copied
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!
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?
Copy link to clipboard
Copied
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.
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
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
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Oh yes.... The button field setup was not "Icon Only".
Now it works. Thank you so so much!!!
Copy link to clipboard
Copied
I have still an other question: I use this button to display an image. On this image I do some drawings. If I save this pdf the button is displayed over my drawings and I can't see them anymore. Is it possible to change the layer of the button? I couldn't find anything.
Copy link to clipboard
Copied
You mean you use the drawing tools in acrobat to add markup annotations, such as circles, rectangles and lines?
If so, then you have a problem. Form fields are always drawn last, so form fields will always draw on top of markup annotations. The only thing you could do is flatten the button into the PDF, Page content is always drawn first.
Copy link to clipboard
Copied
Ahhh.. yes. That's perfect. I have to flatten this pdf anyway before I send it to the customer. And in the flatten pdf I can see my drawings (yes, I mean markup annotations ;)).
Thank you very much for this hint.
Copy link to clipboard
Copied
Sorry, I have an other last question:
Is it possible to flatten just the button (in my vba script)? After I imported the Icon?
I mean something like that:
objJSO.getField("image").buttonImportIcon (ImagePath)
objJSO.getField("image").flattenButton
Copy link to clipboard
Copied
Yes, the "flattenPages" function includes a parameter for controlling how non-printing annotations are handled.
Here's the reference entry.
Set the drawing markups "print" property to false, then flatten with non-printing annots ignored, then reset print to true.
Also, on a completely different issue. If you are going to use JavaScript in VBA, you should write a folder level JavaScript function which is called from the VBA. This minimizes a number of issues that can cause problems with conversions between incompatible contexts.
Copy link to clipboard
Copied
Hey there,
I am also using this workaround with the button image. And it worked well the last years... But now I got an file read error. I didn't change the code and also the images are the same. And also at the same location. I couldn't find anything on google. Maybe someoneelse has an idea?
Dim objAcroApp As Object, objAcroAVDoc As Object
Dim objAcroPDDoc As Object, objJSO As Object
Dim varKey As Variant
Dim FDate As String
Set objAcroApp = CreateObject("AcroExch.App")
Set objAcroAVDoc = CreateObject("AcroExch.AVDoc")
' open file
If objAcroAVDoc.Open(templateFile, "") = True Then
Set objAcroPDDoc = objAcroAVDoc.GetPDDoc
Set objJSO = objAcroPDDoc.GetJSObject
ClearForm = objJSO.ResetForm() 'clear the form
'add image
objJSO.getField("##Skizze##").buttonImportIcon ("/C/test.jpg")
End If
Copy link to clipboard
Copied
Can you open the jpg file in Adobe Acrobat?
Copy link to clipboard
Copied
Thank you for your reply.
Yes, I can open the image in Adobe and I can also import this image as button icon.
But not in VBA anymore. 😕
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thank you for your reply and your good explanation. I will try to fix it in that way. 🙂