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

Inserting a signature and signing a PDF with VBA

Community Beginner ,
Feb 11, 2020 Feb 11, 2020

Copy link to clipboard

Copied

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)

 

TOPICS
Create PDFs , How to

Views

11.3K

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 ,
Feb 11, 2020 Feb 11, 2020

Copy link to clipboard

Copied

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 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 ,
Feb 12, 2020 Feb 12, 2020

Copy link to clipboard

Copied

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?

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
LEGEND ,
Feb 13, 2020 Feb 13, 2020

Copy link to clipboard

Copied

Designer makes a different kind of PDF. It cannot be used with regular forms. 

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 ,
Feb 13, 2020 Feb 13, 2020

Copy link to clipboard

Copied

Though the resulting PDF are differents, is the same JS code in both forms? Sorry I have no experience of inserting JS in Acrobat, so perhaps my question makes no sense.

But my point was that with the VB code I posted I was very close to get the solution, since I can add the signature area. My only problem was to sign it.

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
LEGEND ,
Feb 13, 2020 Feb 13, 2020

Copy link to clipboard

Copied

No, it is an entirely different JavaScript code in LiveCycle Designer forms as well (with a few things the same).

 

Your code as written is seriously wrong. You write

      oPpklite.login "{'myPasswd', '" & strSignFName & "'}"

but what is this supposed to mean? ppkLite.login does not expect a single string argument. It expects at least two arguments (password and diPath) and up to four. The syntax you have used { ... } is what is often used in JavaScript, but just because you can't code that in VB doesn't mean you can wrap it in a string and hope JavaScript will know what you mean... this is not easy, which is why the advice you had earlier was excellent, if unwelcome.

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 ,
Feb 13, 2020 Feb 13, 2020

Copy link to clipboard

Copied

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 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 ,
Apr 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

Good morning Good Night! This code is very good, but I need a code like this that works with Access-Vba. Because Access Vba is different from Excell. Can anyone help!

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 ,
Apr 13, 2022 Apr 13, 2022

Copy link to clipboard

Copied

jcarlosd, did you already get the solution to the problem? My question is the same, can you help?

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 ,
Apr 14, 2022 Apr 14, 2022

Copy link to clipboard

Copied

I solved this issue using an external tool outside from Adobe. It is called PDFtk at https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/.

It allows me to create the PDF with a access-password and a modification-password or even other options such as Allow Printing CopyContents.

The code is pretty simple and probably will have in MS Access too. It requires to run an external command such as:

"pdftk.exe file1.pdf Output file2.pdf User_pw mypass1 Owner_pw mypass2"

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 ,
Apr 14, 2022 Apr 14, 2022

Copy link to clipboard

Copied

Good night Good Morning. It was very incomplete, about PDFtk. Can you post your code to help us? With this PDFtk can you digitally sign with the digital certificate? How to do it, help us!

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 ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

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

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 ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

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

 

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()

 

 

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 ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

LATEST

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

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