• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Insert a JPEG into an image field using Excel/VBA

New Here ,
Jan 24, 2018 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!

TOPICS
Acrobat SDK and JavaScript

Views

7.0K

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
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 15, 2020 Jan 15, 2020

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?

 

Votes

Translate

Translate
Community Expert ,
Jan 24, 2018 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

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

Votes

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
community guidelines
Community Beginner ,
Jan 15, 2020 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!

 

Votes

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
community guidelines
Community Expert ,
Jan 15, 2020 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?

 

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

Votes

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
community guidelines
Community Beginner ,
Jan 16, 2020 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!!!

Votes

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
community guidelines
Community Beginner ,
Jan 20, 2020 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.

Votes

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
community guidelines
Community Expert ,
Jan 20, 2020 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. 

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

Votes

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
community guidelines
Community Beginner ,
Jan 21, 2020 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.

Votes

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
community guidelines
Community Beginner ,
Jan 21, 2020 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 

Votes

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
community guidelines
Community Expert ,
Jan 21, 2020 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. 

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

Votes

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
community guidelines
New Here ,
Nov 23, 2021 Nov 23, 2021

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

Screenshot (29).png

Votes

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
community guidelines
Community Expert ,
Nov 23, 2021 Nov 23, 2021

Copy link to clipboard

Copied

Can you open the jpg file in Adobe Acrobat?

Votes

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
community guidelines
New Here ,
Nov 23, 2021 Nov 23, 2021

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. 😕 

Votes

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
community guidelines
Community Expert ,
Nov 23, 2021 Nov 23, 2021

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. 

 

 

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

Votes

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
community guidelines
New Here ,
Nov 25, 2021 Nov 25, 2021

Copy link to clipboard

Copied

LATEST

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

Votes

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
community guidelines