1

我想签署 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

'处理获取

'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

结束子

4

1 回答 1

2

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

SAPI_ERR_TOO_MANY_CERTS_TO_SELECT_FROM 获取默认证书失败。用户拥有多个证书,SAPI 无法确定应使用哪一个作为默认证书。0x900302b0

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

阿里

于 2014-09-18T08:13:49.590 回答