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
jcarlosdAuthor
Participant
February 13, 2020

Thanks for your answer, Thom.

It seems too complicated for me. I was just looking for an integrated solution, with all coding in the same place. That's the reason I tried the above code that it is not working just at the signing process.

My only experience with Adobe+Javascript is deeloping with LiveCycle: I never developed JS code directly from Acrobat. Is it posible to do that JS from LiveCycle Designer? How can I call the JS code from VBA? Do I need VBScript?

Thom Parker
Community Expert
Community Expert
February 13, 2020

What you are doing is already complicated and you are already calling JS code from VBA. I've actually give you a simpler more direct solutions.

This code:

 

     Set oJS = pdfPDDoc.GetJSObject
      
      Set oFields = oJS.AddField("SignatureField", "signature", 0, Array(350, 800, 500, 750))

 

This is JavaScript. Please watch this tutorial on the Acrobat Console window. 

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

 

You'll learn how to test this code in it's native environment. 

 

 

 

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