JavaScript and VBA: The Saga Continues - A JSObject Question.
MaxCO2012 Jun 26, 2014 2:21 PMThe JavaScript code below, in concjunction with some VBA code, applies a PDF417 barcode image to the upper right hand corner of a PDF. Based on some clever suggestions by George Johnson and Gilad D I'm first adding a button field to the document, and then adding an icon to that button; the icon being a flattened PDF that contains the barcode image. The creation of the barcode image PDF is a whole other story, which I can post if anyone cares.
The JS code works well when I run it with a PDF open and I choose “Apply Barcode” from the newly created menu item. Incidentally, it also works when I run it from the console. Also, based on what I've read (thanks Karl Kremer), one can't call the "InsertPDF417Barcode" function directly from VBA, hence the creation of a new menu item and the reference to the intermediate function "myAdd417Barcode".
So, what I am working to do is to execute this script from a VBA sub, which is also listed below. But, when the “jso.myAdd417Barcode” line (in the VBA code) executes (or doesn't) I get an error – “doc is undefined”, which is clearly an Acrobat error being thrown.
Isn’t “doc” being “defined” by having “this” in my “app.addMenuItem” line? Suggestions?
Any help will be greatly appreciated.
Thanks.
My JS Code (in a folder-level script):
var InsertPDF417Barcode = app.trustedFunction( function(doc)
{
app.beginPriv();
var bcIconFileName = "/C/Temp/bcTmpImage.pdf";
var t = doc.addField("bcFormID", "button", 0, [396, 756, 576, 720]);
t.display = display.visible;
t.buttonPosition = position.iconOnly;
t.buttonScaleHow = scaleHow.proportional;
t.buttonScaleWhen = scaleWhen.always;
t.buttonFitBounds = true;
t.setButtonIcon
var x = doc.importIcon("myIcon", bcIconFileName, 0);
var f = doc.getField("bcFormID");
var i = doc.getIcon("myIcon")
var y = f.buttonSetIcon(i);
app.endPriv();
});
function myAdd417Barcode(doc)
{
InsertPDF417Barcode(doc);
}
var menuParent = (app.viewerVersion<10)? "DocumentProcessing":"Edit";
app.addSubMenu({
cName:"myTools",
cUser:"My Custom Tools",
cParent:menuParent,
nPos:((app.viewerVersion<10)? 0:7)
});
app.addMenuItem({
cName:"myPDF417Barcode",
cUser:"Apply Barcode",
cParent:"myTools",
cExec:"myAdd417Barcode(this);",
nPos:1
});
My VBA Code:
Sub BarcodeDoc_PDF417(pdfFileName As String)
Dim AVDoc As Acrobat.CAcroAVDoc
Dim PDDoc As Acrobat.CAcroPDDoc
Dim myApp As Acrobat.CAcroApp
Dim jso As Object
Dim bcTmpFile As String
bcTmpFile = "C:\Temp\bcTmpFile.pdf"
Set myApp = CreateObject("AcroExch.App")
Set AVDoc = CreateObject("AcroExch.AVDoc")
If AVDoc.Open(pdfFileName, "") Then
Set PDDoc = AVDoc.GetPDDoc
Set jso = PDDoc.GetJSObject
jso.myAdd417Barcode
PDFSave PDDoc, bcTmpFile
myApp.CloseAllDocs
myApp.Exit
Set AVDoc = Nothing
Set jso = Nothing
Set PDDoc = Nothing
Set myApp = Nothing
Else
MsgBox "Document not found:" & vbCrLf & pdfFileName
End If
End Sub




