-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignDoc.vb
More file actions
575 lines (458 loc) · 22.2 KB
/
SignDoc.vb
File metadata and controls
575 lines (458 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
' ***********************************************************************
' Assembly : AutoStoreLibrary
' Author : John Campbell-Higgens
' Created : 16-Sep-2020
'
' Last Modified By : John Campbell-Higgens
' Last Modified On : 06-Jan-2021
' ***********************************************************************
' <copyright file="SignDoc.vb" company="John Campbell-Higgens (Kofax UK Ltd)">
' Copyright © 2020 John Campbell-Higgens (Kofax UK Ltd)
' </copyright>
' <summary></summary>
' ***********************************************************************
Imports System.IO
Imports System.Net
Imports Newtonsoft.Json
''' <summary>
''' Function Library of useful SignDoc functions. Designed to work with the SignDoc v7 Rest-API
''' </summary>
Public Class SignDoc
''' <summary>
''' Retrieve the version of the AutoStoreLibrary for SignDoc
''' </summary>
''' <returns>Verison string</returns>
Public Shared Function GetVersion() As String
Return "Version 1.2.0.4"
End Function
''' <summary>
''' Returns the Authentication Token for the user and account at the SignDoc Server. This function should be used before all other functions
''' </summary>
''' <param name="serverAddress">URL address of the SignDoc Server</param>
''' <param name="userName">SignDoc Username</param>
''' <param name="userPassword">SignDoc Users password</param>
''' <param name="accountID">SignDoc Account ID</param>
''' <returns>Authentiction Token</returns>
Public Shared Function GetAuthenticationToken(ByVal serverAddress, ByVal userName, ByVal userPassword, ByVal accountID) As String
Dim AuthenticationToken As String = ""
Dim headers As WebHeaderCollection
Dim request As HttpWebRequest
Dim response As HttpWebResponse
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = (SecurityProtocolType.Tls12)
userName = userName.Replace("@", "%40")
Dim serverURL As String = serverAddress + "/rest/v7/users/authentication?credentials=" + userName + "&accountid=" + accountID + "&password=" & userPassword & "&usedefaultaccount=false"
Try
request = WebRequest.Create(serverURL)
request.ContentType = "application/json"
request.Method = "POST"
response = request.GetResponse
If response.StatusCode = System.Net.HttpStatusCode.OK Then
headers = response.Headers
For header As Integer = 0 To headers.Count - 1
If headers.Keys(header) = "X-AUTH-TOKEN" Then
AuthenticationToken = headers(header)
End If
Next
Else
AuthenticationToken = response.StatusCode & " - " & response.StatusDescription
End If
Catch ex As Exception
AuthenticationToken = ex.Message & " Call: " & serverURL
End Try
Return AuthenticationToken
End Function
''' <summary>
''' Function to create a SignDoc package. This creates the initial package that documents and signature can then be added to.
''' </summary>
''' <param name="packageName">The name of the SignDoc Package</param>
''' <param name="packageType">the SignDoc package type - PACKAGE is currently the only supported option</param>
''' <param name="AuthenticationToken">Authentication token from the GetAuthenticationToken function</param>
''' <param name="serverAddress">URL address of the SignDoc Sever</param>
''' <returns>PackageID of the created SignDoc Package</returns>
Public Shared Function CreatePackage(ByVal packageName, ByVal packageType, ByVal AuthenticationToken, ByVal serverAddress) As String
Dim request As HttpWebRequest
Dim response As HttpWebResponse
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = (SecurityProtocolType.Tls12)
Dim packageID As String = ""
Dim packageURL As String
Dim stream As Stream
Dim streamRead As StreamReader
Dim jsonReturn As String
Dim NewPackage As New SignDocPackage
NewPackage.name = packageName
NewPackage.type = packageType
Dim jsonData As String = JsonConvert.SerializeObject(NewPackage, Formatting.Indented)
Dim serverURL As String = serverAddress + "/rest/v7/package?schedule=false&clean_fields=false&delete_existing=false&autoprepare=false&resolution=72"
Try
request = WebRequest.Create(serverURL)
request.ContentType = "application/json"
request.Accept = "application/json"
request.Method = "POST"
request.Headers.Add("X-AUTH-TOKEN", AuthenticationToken)
Using streamWriter As New StreamWriter(request.GetRequestStream())
streamWriter.Write(jsonData)
End Using
response = request.GetResponse
If response.StatusCode = 201 Then
stream = response.GetResponseStream
streamRead = New StreamReader(stream)
jsonReturn = streamRead.ReadToEnd
streamRead.Close()
stream.Close()
Dim PackageDetails As SignDocPackageDetails = JsonConvert.DeserializeObject(Of SignDocPackageDetails)(jsonReturn)
packageID = PackageDetails.id
packageURL = PackageDetails.url
End If
Catch ex As Exception
packageID = ex.Message
End Try
packageID = packageID
Return packageID
End Function
''' <summary>
''' Adds a Signer to the SignDoc Package created by the CreatePackage Function.
''' </summary>
''' <param name="serverAddress">URL address of the SignDoc Sever</param>
''' <param name="authToken">Authentication token from the GetAuthenticationToken function</param>
''' <param name="PackageID">Package ID from the CreatePackage function, used to identify the package to add signers to.</param>
''' <param name="SignerName">Name of the Signer</param>
''' <param name="SignerEmail">Email Address of the Signer</param>
''' <param name="SignerType">Type of Signer (Option SIGNER or REVIEWER)</param>
''' <returns>Returns the SignerID of the Signer Created</returns>
Public Shared Function AddSigner(ByVal serverAddress, ByVal authToken, ByVal PackageID, ByVal SignerName, ByVal SignerEmail, ByVal SignerType) As String
Dim SignerID As String = ""
Dim request As HttpWebRequest
Dim response As HttpWebResponse
Dim stream As Stream
Dim streamRead As StreamReader
Dim jsonReturn As String
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = (SecurityProtocolType.Tls12)
Dim NewSigner As New SignerDetails
NewSigner.name = SignerName
NewSigner.email = SignerEmail
NewSigner.role = SignerType
Dim jsonData As String = JsonConvert.SerializeObject(NewSigner, Formatting.Indented)
Dim serverURL As String = serverAddress + "/rest/v7/packages/" & PackageID & "/signer"
Try
request = WebRequest.Create(serverURL)
request.ContentType = "application/json"
request.Accept = "application/json"
request.Method = "POST"
request.Headers.Add("X-AUTH-TOKEN", authToken)
Using streamWriter As New StreamWriter(request.GetRequestStream())
streamWriter.Write(jsonData)
End Using
response = request.GetResponse
If response.StatusCode = 201 Then
stream = response.GetResponseStream
streamRead = New StreamReader(stream)
jsonReturn = streamRead.ReadToEnd
streamRead.Close()
stream.Close()
Dim SignerDetails As SignDocSignerDetails = JsonConvert.DeserializeObject(Of SignDocSignerDetails)(jsonReturn)
SignerID = SignerDetails.id
End If
Catch ex As Exception
SignerID = ex.Message
End Try
Return SignerID
End Function
''' <summary>
''' Adds a document to the SignDoc signing Package.
''' </summary>
''' <param name="serverAddress">URL address of the SignDoc Sever</param>
''' <param name="authToken">Authentication token from the GetAuthenticationToken function</param>
''' <param name="PackageID">Package ID from the CreatePackage function, used to identify the package to add signers to.</param>
''' <param name="DocumentFilePath">File Path to the Document to be added to the Package</param>
''' <param name="DocumentFileName">File Name of the Document to be added to the Package</param>
''' <param name="DocumentFormat">Format of the Document (PDF, TIF)</param>
''' <param name="DocumentName">Name of the Document</param>
''' <param name="DocumentDescription">Description of the Document</param>
''' <param name="DocumentMessage">Message to attach to the document</param>
''' <returns>Returns the DocumentID of the added Document</returns>
Public Shared Function AddDocument(ByVal serverAddress, ByVal authToken, ByVal PackageID, ByVal DocumentFilePath, ByVal DocumentFileName, ByVal DocumentFormat, ByVal DocumentName, ByVal DocumentDescription, ByVal DocumentMessage) As String
Dim request As HttpWebRequest
Dim response As HttpWebResponse
Dim stream As Stream
Dim streamRead As StreamReader
Dim jsonReturn As String
Dim DocumentID As String = ""
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = (SecurityProtocolType.Tls12)
Dim NewDocument As New DocumentEntry
NewDocument.content = Convert.ToBase64String(System.IO.File.ReadAllBytes(DocumentFilePath))
NewDocument.name = DocumentName
NewDocument.description = DocumentDescription
NewDocument.documentMessage = DocumentMessage
NewDocument.fileName = DocumentFileName
NewDocument.format = DocumentFormat
Dim jsonData As String = JsonConvert.SerializeObject(NewDocument, Formatting.Indented)
Dim serverURL As String = serverAddress + "/rest/v7/packages/" & PackageID & "/document?resolution=72&autoprep=false"
Try
request = WebRequest.Create(serverURL)
request.ContentType = "application/json"
request.Accept = "application/json"
request.Method = "POST"
request.Headers.Add("X-AUTH-TOKEN", authToken)
Using streamWriter As New StreamWriter(request.GetRequestStream())
streamWriter.Write(jsonData)
End Using
response = request.GetResponse
If response.StatusCode = 201 Then
stream = response.GetResponseStream
streamRead = New StreamReader(stream)
jsonReturn = streamRead.ReadToEnd
streamRead.Close()
stream.Close()
Dim DocumentDetails As DocumentReturn = JsonConvert.DeserializeObject(Of DocumentReturn)(jsonReturn)
DocumentID = DocumentDetails.id
End If
Catch ex As Exception
DocumentID = ex.Message
End Try
Return DocumentID
End Function
''' <summary>
''' Adds a Signature Field to a Document for a specified Signer
''' </summary>
''' <param name="serverAddress">URL address of the SignDoc Sever</param>
''' <param name="authToken">Authentication token from the GetAuthenticationToken function</param>
''' <param name="PackageID">Package ID from the CreatePackage function</param>
''' <param name="DocumentID">Document ID from the AddDocument function</param>
''' <param name="fieldName">Name of the Field to Add</param>
''' <param name="alternativeName">Alternative Name of the Field to Add</param>
''' <param name="SignerID">SignerID of the Signer associated with the field</param>
''' <param name="RequiredField">Required Field - True / False</param>
''' <param name="ReadOnlyField">ReadOnly Field - True / False</param>
''' <param name="SignatureDescription">Description of Field</param>
''' <param name="WidgetIndex">Index Number of Field (Integer)</param>
''' <param name="WidgetPageNumber">Page Number on the Document for Field (Integer)</param>
''' <param name="WidgetTop">Location of the Top of the Field - from Bottom Left of page (Integer)</param>
''' <param name="WidgetBottom">Location of the Bottom of the Field - from Bottom Left of page (Integer)</param>
''' <param name="WidgetLeft">Location of the Left of the Field - from Bottom Left of Page (Integer)</param>
''' <param name="WidgetRight">Location of the Right of the Field - from Bottom Left of Page (Integer)</param>
''' <returns>Returns Field ID</returns>
Public Shared Function AddSignatureField(ByVal serverAddress, ByVal authToken, ByVal PackageID, ByVal DocumentID, ByVal fieldName, ByVal alternativeName, ByVal SignerID, ByVal RequiredField, ByVal ReadOnlyField, ByVal SignatureDescription, ByVal WidgetIndex, ByVal WidgetPageNumber, ByVal WidgetTop, ByVal WidgetBottom, ByVal WidgetLeft, ByVal WidgetRight) As String
Dim request As HttpWebRequest
Dim response As HttpWebResponse
Dim stream As Stream
Dim streamRead As StreamReader
Dim jsonReturn As String
Dim fieldID As String = ""
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = (SecurityProtocolType.Tls12)
Dim jsonData As String
jsonData = "{" & ControlChars.CrLf
jsonData = jsonData & " " & ControlChars.Quote & "name" & ControlChars.Quote & ": " & ControlChars.Quote & fieldName & ControlChars.Quote & "," & ControlChars.CrLf
jsonData = jsonData & " " & ControlChars.Quote & "description" & ControlChars.Quote & ": " & ControlChars.Quote & SignatureDescription & ControlChars.Quote & "," & ControlChars.CrLf
jsonData = jsonData & " " & ControlChars.Quote & "signerId" & ControlChars.Quote & ": " & ControlChars.Quote & SignerID & ControlChars.Quote & "," & ControlChars.CrLf
jsonData = jsonData & " " & ControlChars.Quote & "alternateName" & ControlChars.Quote & ": " & ControlChars.Quote & alternativeName & ControlChars.Quote & "," & ControlChars.CrLf
jsonData = jsonData & " " & ControlChars.Quote & "required" & ControlChars.Quote & ": " & LCase(RequiredField) & "," & ControlChars.CrLf
jsonData = jsonData & " " & ControlChars.Quote & "readOnly" & ControlChars.Quote & ": " & LCase(ReadOnlyField) & "," & ControlChars.CrLf
jsonData = jsonData & " " & ControlChars.Quote & "widgets" & ControlChars.Quote & ": [" & ControlChars.CrLf
jsonData = jsonData & " " & "{" & ControlChars.CrLf
jsonData = jsonData & " " & ControlChars.Quote & "index" & ControlChars.Quote & ": " & WidgetIndex & "," & ControlChars.CrLf
jsonData = jsonData & " " & ControlChars.Quote & "pageNumber" & ControlChars.Quote & ": " & WidgetPageNumber & "," & ControlChars.CrLf
jsonData = jsonData & " " & ControlChars.Quote & "top" & ControlChars.Quote & ": " & WidgetTop & "," & ControlChars.CrLf
jsonData = jsonData & " " & ControlChars.Quote & "left" & ControlChars.Quote & ": " & WidgetLeft & "," & ControlChars.CrLf
jsonData = jsonData & " " & ControlChars.Quote & "right" & ControlChars.Quote & ": " & WidgetRight & "," & ControlChars.CrLf
jsonData = jsonData & " " & ControlChars.Quote & "bottom" & ControlChars.Quote & ": " & WidgetBottom & "," & ControlChars.CrLf
jsonData = jsonData & " " & ControlChars.Quote & "tabIndex" & ControlChars.Quote & ": " & "0" & ControlChars.CrLf
jsonData = jsonData & " " & "}" & ControlChars.CrLf
jsonData = jsonData & " " & "]" & ControlChars.CrLf & "}"
Dim serverURL As String = serverAddress + "/rest/v7/packages/" & PackageID & "/documents/" & DocumentID & "/signaturefield?resolution=72&natural_order=true"
Try
request = WebRequest.Create(serverURL)
request.ContentType = "application/json"
request.Accept = "application/json"
request.Method = "POST"
request.Headers.Add("X-AUTH-TOKEN", authToken)
Using streamWriter As New StreamWriter(request.GetRequestStream())
streamWriter.Write(jsonData)
End Using
response = request.GetResponse
If response.StatusCode = 201 Then
stream = response.GetResponseStream
streamRead = New StreamReader(stream)
jsonReturn = streamRead.ReadToEnd
streamRead.Close()
stream.Close()
Dim FieldDetails As SignDocSignerDetails = JsonConvert.DeserializeObject(Of SignDocSignerDetails)(jsonReturn)
fieldID = FieldDetails.id
End If
Catch ex As Exception
fieldID = ex.Message
End Try
Return fieldID
End Function
''' <summary>
''' Schedule the SignDoc package for Sending, effectively sends the SignDoc Package identified by PackageID to the Signers
''' </summary>
''' <param name="serverAddress">URL address of the SignDoc Sever</param>
''' <param name="authToken">Authentication token from the GetAuthenticationToken function</param>
''' <param name="PackageID">Package ID from the CreatePackage function</param>
''' <returns>System.String.</returns>
Public Shared Function SchedulePackage(ByVal serverAddress, ByVal authToken, ByVal PackageID) As String
Dim request As HttpWebRequest
Dim response As HttpWebResponse
Dim callReturn As String = ""
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = (SecurityProtocolType.Tls12)
Dim serverURL As String = serverAddress + "/rest/v7/packages/" & PackageID & "/scheduler"
Try
request = WebRequest.Create(serverURL)
request.ContentType = "application/json"
request.Method = "POST"
request.Headers.Add("X-AUTH-TOKEN", authToken)
response = request.GetResponse
If response.StatusCode = System.Net.HttpStatusCode.OK Then
callReturn = response.StatusCode
End If
Catch ex As Exception
callReturn = ex.Message & " Call: " & serverURL
End Try
Return callReturn
End Function
End Class
''' <exclude />
Public Class SignDocPackage
''' <summary>
''' Gets or sets the name.
''' </summary>
''' <value>The name.</value>
Public Property name As String
''' <summary>
''' Gets or sets the type.
''' </summary>
''' <value>The type.</value>
Public Property type As String
End Class
''' <exclude />
Public Class SignDocPackageDetails
''' <summary>
''' Gets or sets the identifier.
''' </summary>
''' <value>The identifier.</value>
Public Property id As String
''' <summary>
''' Gets or sets the URL.
''' </summary>
''' <value>The URL.</value>
Public Property url As String
End Class
''' <exclude />
Public Class SignerDetails
''' <summary>
''' Gets or sets the email.
''' </summary>
''' <value>The email.</value>
Public Property email As String
''' <summary>
''' Gets or sets the name.
''' </summary>
''' <value>The name.</value>
Public Property name As String
''' <summary>
''' Gets or sets the role.
''' </summary>
''' <value>The role.</value>
Public Property role As String
End Class
''' <exclude />
Public Class SignDocSignerDetails
''' <summary>
''' Gets or sets the identifier.
''' </summary>
''' <value>The identifier.</value>
Public Property id As String
''' <summary>
''' Gets or sets the messages.
''' </summary>
''' <value>The messages.</value>
Public Property messages As String
''' <summary>
''' Gets or sets the URL.
''' </summary>
''' <value>The URL.</value>
Public Property url As String
End Class
''' <exclude />
Public Class DocumentEntry
' Document Content as BASE64
''' <summary>
''' Gets or sets the content.
''' </summary>
''' <value>The content.</value>
Public Property content As String
''' <summary>
''' Gets or sets the description.
''' </summary>
''' <value>The description.</value>
Public Property description As String
''' <summary>
''' Gets or sets the document message.
''' </summary>
''' <value>The document message.</value>
Public Property documentMessage As String
''' <summary>
''' Gets or sets the name of the file.
''' </summary>
''' <value>The name of the file.</value>
Public Property fileName As String
''' <summary>
''' Gets or sets the format.
''' </summary>
''' <value>The format.</value>
Public Property format As String
''' <summary>
''' Gets or sets the name.
''' </summary>
''' <value>The name.</value>
Public Property name As String
End Class
''' <exclude />
Public Class DocumentReturn
''' <summary>
''' Gets or sets the description.
''' </summary>
''' <value>The description.</value>
Public Property description As String
''' <summary>
''' Gets or sets the document message.
''' </summary>
''' <value>The document message.</value>
Public Property documentMessage As String
''' <summary>
''' Gets or sets the name of the file.
''' </summary>
''' <value>The name of the file.</value>
Public Property fileName As String
''' <summary>
''' Gets or sets the identifier.
''' </summary>
''' <value>The identifier.</value>
Public Property id As String
''' <summary>
''' Gets or sets the name.
''' </summary>
''' <value>The name.</value>
Public Property name As String
''' <summary>
''' Gets or sets the order.
''' </summary>
''' <value>The order.</value>
Public Property order As String
''' <summary>
''' Gets or sets the thumbnail.
''' </summary>
''' <value>The thumbnail.</value>
Public Property thumbnail As String
''' <summary>
''' Gets or sets the URL.
''' </summary>
''' <value>The URL.</value>
Public Property url As String
End Class