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)
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.
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?
Copy link to clipboard
Copied
Designer makes a different kind of PDF. It cannot be used with regular forms.
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.
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.
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.
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!
Copy link to clipboard
Copied
jcarlosd, did you already get the solution to the problem? My question is the same, can you help?
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"
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!
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
Copy link to clipboard
Copied
First, have you read the Acrobat documentation on using the IAC?
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()
Copy link to clipboard
Copied
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