Insert a JPEG into an image field using Excel/VBA

New Here ,
Jan 24, 2018

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!

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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?

 

TOPICS
Acrobat SDK and JavaScript

Views

2.0K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Insert a JPEG into an image field using Excel/VBA

New Here ,
Jan 24, 2018

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!

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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?

 

TOPICS
Acrobat SDK and JavaScript

Views

2.0K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 24, 2018 1
Adobe Community Professional ,
Jan 24, 2018

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.

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 24, 2018 0
Community Beginner ,
Jan 15, 2020

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!

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 15, 2020 0
Adobe Community Professional ,
Jan 15, 2020

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?

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 15, 2020 1
Community Beginner ,
Jan 16, 2020

Copy link to clipboard

Copied

Oh yes.... The button field setup was not "Icon Only".

Now it works. Thank you so so much!!!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 16, 2020 1
Community Beginner ,
Jan 20, 2020

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 20, 2020 0
Adobe Community Professional ,
Jan 20, 2020

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. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 20, 2020 1
Community Beginner ,
Jan 21, 2020

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 21, 2020 0
Community Beginner ,
Jan 21, 2020

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 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 21, 2020 0
Adobe Community Professional ,
Jan 21, 2020

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.

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_Acro...

 

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. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 21, 2020 0