cosign - api:调用SignatureFieldCreateSign时出错

时间:2014-09-17 09:15:46

标签: cosign-api

我想签署PDF文档,我使用的是CoSign API,代码是用vb6编写的。 当我使用SAPI.SignetureFieldCreateSign(创建签名并在文档中签名)时,如果一切正常,此方法需要返回0,但它返回-1878850896。

我的代码:

Private Sub SignPDF()

'自定义值

Dim rc As Integer
Dim SAPI As New SAPICrypt
Dim SESHandle As New SESHandle
Dim SFS As New SAPILib.SigFieldSettings
Dim TF As New SAPILib.timeFormat
Dim fileType As SAPI_ENUM_FILE_TYPE
Dim objFSO As FileSystemObject
Dim objFile As File
Dim objFolder As Folder

Dim strFolderPath As String
Dim flags As Integer
Dim filePath As String  'PDF file to sign
Dim username As String  'CoSign account username
Dim password As String  'CoSign account password
Dim domain As String  'CoSign account domain
Dim sigPageNum As Integer  'Create signature on the first page
Dim sigX As Integer  'Signature field X location
Dim sigY As Integer  'Signature field Y location
Dim sigWidth As Integer  'Signature field width
Dim sigHeight As Integer  'Signature field height
Dim timeFormat As String  'The display format of the time
Dim dateFormat As String   'The display format of the date
Dim appearanceMask As Integer  'Elements to display on the signature field


'Initialize variables
fileType = SAPI_ENUM_FILE_TYPE.SAPI_ENUM_FILE_ADOBE   'Type of the file to sign - PDF
flags = 0
strFolderPath = "C:\Users\jennya\Desktop\pdfFiles\"
username = "MyUsername"
password = "MyPassword"
domain = ""
sigPageNum = 1
sigX = 145
sigY = 125
sigWidth = 160
sigHeight = 45
timeFormat = "hh:mm:ss"
dateFormat = "dd/MM/yyyy"
appearanceMask = SAPI_ENUM_DRAWING_ELEMENT.SAPI_ENUM_DRAWING_ELEMENT_GRAPHICAL_IMAGE Or _
                 SAPI_ENUM_DRAWING_ELEMENT.SAPI_ENUM_DRAWING_ELEMENT_SIGNED_BY Or _
                 SAPI_ENUM_DRAWING_ELEMENT.SAPI_ENUM_DRAWING_ELEMENT_TIME

'实例化并初始化SAPI

'Initialize SAPI library
rc = SAPI.Init
If rc <> SAPI_OK Then
    MsgBox "error initializing SAPI", vbOKOnly, "Error"
    'Exit Sub
End If

“HandleAcquire

'Acquire SAPI session handle
rc = SAPI.HandleAcquire(SESHandle)
If rc <> SAPI_OK Then
    MsgBox "Failed in SAPIHandleAcquire"
End If

“登录

'Personalize SAPI Session
rc = SAPI.Logon(SESHandle, username, domain, password)
If rc <> SAPI_OK Then
    MsgBox "Failed to authenticate user"
End If

'定义签名字段设置

'Define signature field settings
SFS.Page = sigPageNum
SFS.x = sigX
SFS.y = sigY
SFS.Width = sigWidth
SFS.Height = sigHeight
SFS.appearanceMask = appearanceMask
SFS.SignatureType = SAPI_ENUM_SIGNATURE_TYPE.SAPI_ENUM_SIGNATURE_DIGITAL
SFS.DependencyMode = SAPI_ENUM_DEPENDENCY_MODE.SAPI_ENUM_DEPENDENCY_MODE_INDEPENDENT
TF.dateFormat = dateFormat
TF.timeFormat = timeFormat
TF.ExtTimeFormat = SAPI_ENUM_EXTENDED_TIME_FORMAT.SAPI_ENUM_EXTENDED_TIME_FORMAT_GMT  'Display GMT offset
SFS.timeFormat = TF

“签名

Set objFSO = New FileSystemObject 'creates a new File System Object reference

If objFSO.FolderExists(strFolderPath) Then 'check if Source folder exists
    Set objFolder = objFSO.GetFolder(strFolderPath) 'get Source folder
    For Each objFile In objFolder.Files 'for every file in the folder.

        filePath = objFile.Path

        'Create and sign a new signature field in the document
        rc = SAPI.SignatureFieldCreateSign(SESHandle, fileType, filePath, SFS, flags, "")
        If rc <> SAPI_OK Then
            MsgBox "Failed in SAPISignatureFieldCreateSign"
        End If
    Next
Else
    MsgBox "Folder not exists"
End If

'清理

'Release user context
rc = SAPI.Logoff(SESHandle)
If rc <> SAPI_OK Then
    MsgBox "Failed to Logoff"
End If
SAPI.Finalize

End Sub

1 个答案:

答案 0 :(得分:2)

你得到的错误&#34; -1878850896&#34;转换为十六进制时为900302B0。 如果您在SAPI参考指南中查找此错误(您可以在http://www.arx.com/api下找到在线版本 - 请参阅确切位置here),您会发现以下内容:

SAPI_ERR_TOO_MANY_CERTS_TO_SELECT_FROM 无法获得默认证书。用户有多个证书,SAPI无法确定哪个证书应该用作默认值。 0x900302b0

这表示您尝试签名的CoSign帐户包含多个签名证书,而在您的SAPI代码中,您没有指明用于签名的默认证书。

阿里