Skip to main content
Participant
February 11, 2020
Question

Inserting a signature and signing a PDF with VBA

  • February 11, 2020
  • 1 reply
  • 12991 views

I have been searching in different boards about this need that I have. Using code I found in Excel forums and also this forum I got the following code, that works for inserting a signature but failed on the process of signing, and that's why I wrote that in the title.

 

 

Sub SignPDF()
On Error GoTo Err_Handler
Dim pdfPDDoc As New AcroPDDoc, oJS As Object, oSign As Object, oPpklite As Object, oFields As Object
Dim strFName1 As String, strFName2 As String, strSignFName As String
Dim oSignInfo As Object, strSecInfo As String
Dim oParam As Parameter
    
   strSignFName = "C:\mySignature.pfx"
   strFName1 = "C:\myPdf.pdf"
   strFName2 = "C:\myPdf-signed.pdf"

   Set pdfPDDoc = CreateObject("AcroExch.PDDoc")
    
   If pdfPDDoc.Open(strFName1) Then
      Set oJS = pdfPDDoc.GetJSObject
      
      Set oFields = oJS.AddField("SignatureField", "signature", 0, Array(350, 800, 500, 750))
      Set oSign = oJS.GetField("SignatureField")
      
      Set oPpklite = oJS.security.getHandler("Adobe.PPKLite", True)
      oPpklite.login "{'myPasswd', '" & strSignFName & "'}"
      oSign.signatureSign oPpklite
      
      pdfPDDoc.Save 1, strFName2
      oPpklite.logout
   End If
   
Exit_Proc:
    Exit Sub
        
Err_Handler:
    MsgBox "In test" & vbCrLf & Err.Number & "--" & Err.Description
    Resume Exit_Proc
End Sub

 

The problem is in the line with "oSign.signatureSign oPpklite". If I leave as it is, it does not sign the PDF. But if try to add any option, as I read in the Acrobat JavaScript Scripting Reference, it returns an error. This is what I tried:

 

oSign.signatureSign oPpklite, "{password:'myPasswd', mdp: allowNone}"

 

It always return an error, no matter what I tried.

Anybody has experience with this task?

(I also asked three days ago in an Excel forum, without success)

 

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
February 11, 2020

Running Acrobat JavaScript from VB is problematic. So don't do it. 

Do this instead:

 

Step #1: Develop the actual JavaScript for this from the console window in Acrobat.

Step#2: Write a  folder level JavaScript function to perform this action.

Step#3: Call this function from your VB. Do not pass any objects into this function Keep It Simple. Use Strings and numbers. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
June 22, 2022

Hi Thom,

 

I have a similar problem. I would like to call a folder-level javascript function from a .vbs script. Here is the javascript code.

function deleteFirstPage() {
  this.deletePages({
    nStart: 0,
    nEnd: 0
  });
  this.saveAs({
    cPath: this.path
  });
}

How would I go about this?

 

Thanks,

Trey

Participating Frequently
June 23, 2022

First, have you read the Acrobat documentation on using the IAC?  

https://opensource.adobe.com/dc-acrobat-sdk-docs/acrobatsdk/html2015/index.html#t=Acro12_MasterBook%2FAcro_Interapp_Commun_SectionPage%2FAcro_Interapp_Commun_SectionPage.htm

 

It provide the basics you'll need to get started. Some of which is already provided (in code) at the top of this thread. 

Here is a simple script that will call the JavaScript function on the current  document

   Set pdfPDDoc = CreateObject("AcroExch.PDDoc")
   Set oJS = pdfPDDoc.GetJSObject
   oJS.deleteFirstPage()

 

 


Yes, I've read some of the IAC documentation including the part on using the GetJSObject for the PDDOC class.

 

Right now I'm saving folder-level javascript functions in my "user" folder and then using the javascript object to access those functions. It's a very roundabout way to execute the javascript without having to visually open the pdf.

 

I was just hoping there was a more elegant way to do it.

 

Thanks,

Trey