Hello !
I have implemented the pdf signature in VB.net. Unfortunately, after the process, Adobe still uses the file and I can't move or delete it.
Currently, I'm using the latest Acrobat 7.0 SDK and Acrobat Pro 7.0 Beta 5.
Here is the developped code :
--------------------------
----------
----------
-
Public Class SignPdfWithAcrobat
Const SignError = 1
'Private m_PDDoc As Acrobat.CAcroPDDoc
' Private m_jso As Object
' Private m_secHandler As Object
Private m_sigFilePath As String
Private m_sigPassword As String
' --------------------------
----------
----------
----------
----------
----------
----------
----------
----------
-------
' Constructeur
'
' m_sigFilePath : chemin du fichier pfx de signature. Le mot de passe correspond à celui de cette signature
' m_sigPassword : password de la signature
' --------------------------
----------
----------
----------
----------
----------
----------
----------
----------
-------
Public Sub New(ByVal p_sigFilePath As String, ByVal p_sigPassword As String)
' connection a acrobat
'm_PDDoc = CreateObject("AcroExch.PDD
oc")
m_sigFilePath = p_sigFilePath
m_sigPassword = p_sigPassword
End Sub
' --------------------------
----------
----------
----------
----------
----------
----------
----------
----------
-------
' Methode de signature d'un document PDF
' Créée un champ signature
' puis y appose la signature et sauvegarde le fichier signé
'
' m_inPDFFilePath : fichier à signer (chemin complet)
' --------------------------
----------
----------
----------
----------
----------
----------
----------
----------
-------
Public Sub Sign(ByVal p_inPDFFilePath As String)
Dim _PAGE_NUMBER_ = 0
Dim _LEFT_X_COORD_ = 10
Dim _BOX_LENGTH_ = 200
Dim _TOP_Y_COORD_ = 10
Dim _BOX_HEIGHT_ = 60
Dim _FIELD_NAME_ = "Signature"
Dim _FIELD_TEXT_ = "FieldText"
Sign_Full(p_inPDFFilePath,
m_sigPassword, m_sigFilePath, p_inPDFFilePath, _PAGE_NUMBER_, _LEFT_X_COORD_, _BOX_LENGTH_, _TOP_Y_COORD_, _BOX_HEIGHT_, _FIELD_NAME_)
End Sub
' --------------------------
----------
----------
----------
----------
----------
----------
----------
----------
-------
' Methode de signature d'un document PDF
' Créée un champ signature
' puis y appose la signature et sauvegarde le fichier signé. Pour rendre le champ invisible,
' utiliser Width et Length à zéro
'
' m_inPDFFilePath : fichier à signé (chemin complet)
' m_sigPassword : password de la signature
' m_sigFilePath : chemin du fichier pfx de signature. Le mot de passe correspond à celui de cette signature
' _PAGE_NUMBER_ : page du document à signer
' _LEFT_X_COORD_ : coordonnée gauche de la signaure
' _BOX_LENGTH_ : taille de la boite de signature
' _TOP_Y_COORD_ : hauteur de l'emplacement de la signature
' _BOX_HEIGHT_ : largeur de la boite de signature
' _FIELD_NAME_ : nom duc hamp de signature
' --------------------------
----------
----------
----------
----------
----------
----------
----------
----------
-------
Public Sub Sign_Full(ByVal p_inPDFFilePath As String, ByVal p_sigPassword As String, ByVal p_sigFilePath As String, _
ByVal p_outPDFFilePath As String, ByVal _PAGE_NUMBER_ As Integer, ByVal _LEFT_X_COORD_ As Integer, _
ByVal _BOX_LENGTH_ As Integer, ByVal _TOP_Y_COORD_ As Integer, ByVal _BOX_HEIGHT_ As Integer, _
ByVal _FIELD_NAME_ As String)
Dim m_PDDoc As Acrobat.CAcroPDDoc
Dim gPage As Acrobat.CAcroPDPage
Dim jso As Object
Dim pageRect As Object
Dim formFieldRect(3) As Integer
Dim formField As Object
Dim secHandler As Object
Try
m_PDDoc = CreateObject("AcroExch.PDD
oc")
' ouverture du fichier a signer
If m_PDDoc.Open(p_inPDFFilePa
th) Then
jso = m_PDDoc.GetJSObject
' *** Load security
' validate signatures when the document is opened
jso.security.validateSigna
turesOnOpe
n = True
' *** Create the field
gPage = m_PDDoc.AcquirePage(_PAGE_
NUMBER_)
pageRect = gPage.GetSize
' Taille et position du champ de signature
formFieldRect(0) = _LEFT_X_COORD_
formFieldRect(2) = formFieldRect(0) + _BOX_LENGTH_
formFieldRect(1) = pageRect.Y - _TOP_Y_COORD_ - _BOX_HEIGHT_
formFieldRect(3) = formFieldRect(1) + _BOX_HEIGHT_
' AJout du champ dans le document
formField = jso.AddField(_FIELD_NAME_,
"signature", _PAGE_NUMBER_, formFieldRect)
' *** Configure field's style
'formField.BorderStyle = jso.border.s
'formField.TextSize = 0
'formField.LineWidth = 3
'formField.TextColor = jso.Color.black
'formField.StrokeColor = jso.Color.black
'formField.FillColor = jso.Color.transparent
'formField.textFont = jso.Font.HelvB
'formField.ReadOnly = True
' FM le 31/05/2005 : on rend le champ de signature est invisible
' JP le 06/06/2005 : utiliser width et height à zéro pour obtenir le même comportement
''formField.Display = jso.Display.Visible
''formField.Display = jso.Display.noPrint ' Pour avoir une signature visible mais non imprimable
formField.Display = jso.Display.Hidden
' *** Add signature
secHandler = jso.security.getHandler("A
dobe.PPKLi
te", True)
If secHandler.login(p_sigPass
word, p_sigFilePath) Then
' Signe le champ
If Not formField.signatureSign(se
cHandler) Then
Err.Raise(vbObjectError + 512 + SignError, "SignPdfWithAcrobat.Sign_F
ull", "Cannot sign field")
End If
' Teste si la signature est valide
Dim status = formField.signatureValidat
e()
If status < 3 Then
Err.Raise(vbObjectError + 512 + SignError, "SignPdfWithAcrobat.Sign_F
ull", "Invalid signature :" & formField.signatureInfo().
statusText
())
End If
' *** Save the file
m_PDDoc.Save(&H1, p_outPDFFilePath)
secHandler.logout()
Else
Err.Raise(vbObjectError + 512 + SignError, "SignPdfWithAcrobat.Sign_F
ull", "Cannot get security handler :" & Err.Description)
End If
End If
Catch ex As Exception
Err.Raise(vbObjectError + 512 + SignError, "SignPdfWithAcrobat.Sign_F
ull", "Sign Exception raise : " & Err.Description)
Finally
'fermeture du fichier
m_PDDoc.Close()
System.Runtime.InteropServ
ices.Marsh
al.Release
ComObject(
gPage)
System.Runtime.InteropServ
ices.Marsh
al.Release
ComObject(
m_PDDoc)
m_PDDoc = Nothing
gPage = Nothing
jso = Nothing
pageRect = Nothing
formField = Nothing
secHandler = Nothing
End Try
End Sub
' --------------------------
----------
----------
----------
----------
----------
----------
----------
----------
-------
' Teste si le fichier de signature est valide (accessibilité et mot de passe du fichier)
'
' m_sigFilePath : chemin du fichier pfx de signature. Le mot de passe correspond à celui de cette signature
' m_sigPassword : password de la signature
' return : false = erreur
' --------------------------
----------
----------
----------
----------
----------
----------
----------
----------
-------
Public Function TestSignature(ByVal m_sigPassword As String, ByVal m_sigFilePath As String) As Boolean
Dim gPDDoc As Acrobat.CAcroPDDoc
Dim jso As Object
Dim secHandler As Object
gPDDoc = CreateObject("AcroExch.PDD
oc")
jso = gPDDoc.GetJSObject
secHandler = jso.security.getHandler("A
dobe.PPKLi
te", True)
Return secHandler.login(m_sigPass
word, m_sigFilePath)
End Function
End Class