Copy link to clipboard
Copied
I want to import a PNG image or, ideally, a single-page PDF file to an Image/Button Field with a script. The Acrobat I installed on my PC is the Pro version, not Acrobat Reader.
My original idea was to run a VBA code in Excel with Acrobat Library. The code shows below:
Sub InsertPDF()
Dim acroApp As Object
Dim arcoDoc As Object
Dim jso As Object
Set acroApp = CreateObject("AcroExch.App")
Set arcoDoc = CreateObject("AcroExch.PDDoc")
Dim insertThisPDFPath As String
Dim targetPDFPath As String
insertThisPDFPath = "C:\Temp\Insert_this.pdf"
targetPDFPath = "C:\Temp\Target.pdf"
arcoDoc.Open (targetPDFPath)
Set jso = arcoDoc.GetJSObject
Dim imageField As Object
Set imageField = jso.getField("Image1_af_image")
imageField.strokeColor = jso.Color.transparent
imageField.fillColor = jso.Color.transparent
imageField.buttonImportIcon insertThisPDFPath
arcoDoc.Save PDSaveFull, "Result.pdf"
arcoDoc.Close
acroApp.Exit
Set arcoDoc = Nothing
Set acroApp = Nothing
End Sub
However, when I ran the code, the PDF file could not be inserted into the Image Field. The colour of the field has been successfully changed to transparent, which means the field setting is correct. Also, the PDF was saved to the correct path. Therefore, I think it must be the issue with "imageField.buttonImportIcon insertThisPDFPath".
After this, I tried to use JavaScript to control the PDF directly. I edited the Action of the field, with ran the following code:
var f = this.getField("Image1_af_image");
f.fillColor = color.transparent;
f.borderColor = color.transparent;
f.buttonImportIcon("/C/Temp/Insert_This.pdf");
Then an Error shown over the Debugger said:
NotAllowedError: Security settings prevent access to this property or method.
I tried to reset every security optional under Preferences, including deselecting the global object security policy and also adding the path to the whitelist. Unfortunately, it still does not work. I guess the original VBA code might also meet this problem so that it cannot work either.
What should I do next? I just want to down-scale and insert a pdf page overlay on another pdf page. Are there any methods with VBA that can achieve this function?
Copy link to clipboard
Copied
The buttonImportIcon method can only be used from a privileged context (if you specify the file path).
I'm guessing a VBA script is not considered to be one... This will apply to all other methods of inserting a page into a PDF file, so you need to find a different solution, such as using a folder-level script.
Copy link to clipboard
Copied
As Try67 suggests, you need to use a folder level trusted function to run the buttonImportIcon function. But I would take this even further. VBA and JavaScript data types are not compatible. Adobe tries does some tricky hand waving to make the JSO work, but it has its limits, and in general it's a clunck interface. I never access JS data or functions directly from a VBA. Instead, I create a folder level script with functins to handle everything, and then call the defined functions from VBA. This also creates a clean framework for developing and testing the Acrobat JS side.
Here's an article all about trusted functions.
https://www.pdfscripting.com/public/Using-Trusted-Functions.cfm
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
I solved this problem by adding these codes in VBA.
Add
acroApp.Show
acroApp.TrustFilePath insertThisPDFPath
Before
imageField.buttonImportIcon insertThisPDFPath​
Copy link to clipboard
Copied
The buttonImportIcon method can only be used from a privileged context (if you specify the file path).
I'm guessing a VBA script is not considered to be one... This will apply to all other methods of inserting a page into a PDF file, so you need to find a different solution, such as using a folder-level script.
Copy link to clipboard
Copied
As Try67 suggests, you need to use a folder level trusted function to run the buttonImportIcon function. But I would take this even further. VBA and JavaScript data types are not compatible. Adobe tries does some tricky hand waving to make the JSO work, but it has its limits, and in general it's a clunck interface. I never access JS data or functions directly from a VBA. Instead, I create a folder level script with functins to handle everything, and then call the defined functions from VBA. This also creates a clean framework for developing and testing the Acrobat JS side.
Here's an article all about trusted functions.
https://www.pdfscripting.com/public/Using-Trusted-Functions.cfm
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
I solved this problem by adding these codes in VBA.
Add
acroApp.Show
acroApp.TrustFilePath insertThisPDFPath
Before
imageField.buttonImportIcon insertThisPDFPath​
Copy link to clipboard
Copied
Excellent fix!!
However, I stand by my solution. As you use more JS in VBA you'll run into two types of roadblocks, object and property incompatibility, and debug complexity. It's so much better to do JavaScript in the JavaScript context.
Use the Acrobat JavaScript Reference early and often

