-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathInvisibleSignatureViewModel.cs
436 lines (403 loc) · 15.5 KB
/
InvisibleSignatureViewModel.cs
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
#region Copyright Syncfusion® Inc. 2001-2025.
// Copyright Syncfusion® Inc. 2001-2025. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// licensing@syncfusion.com. Any infringement will be prosecuted under
// applicable laws.
#endregion
using Syncfusion.Pdf.Graphics;
using Syncfusion.Windows.PdfViewer;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Resources;
using System.Drawing;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Security;
using Syncfusion.Pdf;
using System.Security.Cryptography.X509Certificates;
using Syncfusion.Pdf.Interactive;
using System.Threading.Tasks;
using System.Threading;
using System.ComponentModel;
using syncfusion.demoscommon.wpf;
using Syncfusion.Windows.Tools.Controls;
namespace syncfusion.pdfviewerdemos.wpf
{
public class InvisibleSignatureViewModel : INotifyPropertyChanged
{
private Stream m_documentStream;
internal InvisibleSignature invisibleSignature = null;
private bool isSignatureAdded = false;
internal int SignaturePageIndex = -1;
private ICommand documentLoadedCommand;
private ICommand loadedCommand;
private ICommand downloadCommand;
private ICommand completeSigningCommand;
ToolTip tooltip = new ToolTip();
private MemoryStream signatureStream;
private Visibility isSuccessMsgVisible = Visibility.Collapsed;
private string validationMessage = "";
private Visibility isInvalidMsgVisible= Visibility.Collapsed;
private Visibility isErrorMsgVisible = Visibility.Collapsed;
private bool isCompleteSigning;
private bool isSaved;
private Visibility isValidationMsgVisibility = Visibility.Collapsed;
internal string keyFileName = "localhost.pfx";
private PdfViewerControl pdfViewer;
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Gets or sets the path for the document.
/// </summary>
public Stream DocumentStream
{
get
{
return m_documentStream;
}
set
{
m_documentStream = value;
}
}
/// <summary>
/// Gets or sets the path for the validated document.
/// </summary>
public MemoryStream SignatureStream
{
get
{
return signatureStream;
}
set
{
signatureStream = value;
}
}
/// <summary>
/// Bool property for signature added case
/// </summary>
internal bool IsSignatureAdded
{
get
{
return isSignatureAdded;
}
set
{
isSignatureAdded = value;
}
}
/// <summary>
/// Command for document loaded
/// </summary>
public ICommand DocumentLoadedCommand
{
get
{
if (documentLoadedCommand == null)
{
documentLoadedCommand = new DelegateCommand<PdfViewerControl>(OnDocumentLoaded);
}
return documentLoadedCommand;
}
}
/// <summary>
/// Command for page loaded
/// </summary>
public ICommand LoadedCommand
{
get
{
if (loadedCommand == null)
{
loadedCommand = new DelegateCommand<PdfViewerControl>(OnLoaded);
}
return loadedCommand;
}
}
/// <summary>
/// Command for Download
/// </summary>
public ICommand DownloadCommand
{
get
{
if (downloadCommand == null)
{
downloadCommand = new DelegateCommand<ICommand>(OnDownload);
}
return downloadCommand;
}
}
/// <summary>
/// Command for CompleteSigning
/// </summary>
public ICommand CompleteSigningCommand
{
get
{
if (completeSigningCommand == null)
{
completeSigningCommand = new DelegateCommand<ICommand>(OnCompleteSigning);
}
return completeSigningCommand;
}
}
/// <summary>
/// Gets or sets the validation message
/// </summary>
public string ValidationMessage
{
get => validationMessage;
set
{
validationMessage = value;
OnPropertyChanged("ValidationMessage");
}
}
/// <summary>
/// Gets or sets the visibility for successfully signed document
/// </summary>
public Visibility IsSuccessMsgVisible
{
get => isSuccessMsgVisible;
set
{
isSuccessMsgVisible = value;
OnPropertyChanged("IsSuccessMsgVisible");
}
}
/// <summary>
/// Gets or sets the visibility for invalid document
/// </summary>
public Visibility IsInvalidMsgVisible
{
get => isInvalidMsgVisible;
set
{
isInvalidMsgVisible = value;
OnPropertyChanged("IsInvalidMsgVisible");
}
}
/// <summary>
/// Gets or sets the visibility for error document
/// </summary>
public Visibility IsErrorMsgVisible
{
get => isErrorMsgVisible;
set
{
isErrorMsgVisible = value;
OnPropertyChanged("IsErrorMsgVisible");
}
}
/// <summary>
/// Gets or sets to enable the complete signing button
/// </summary>
public bool IsCompleteSigning
{
get => isCompleteSigning;
set
{
isCompleteSigning = value;
OnPropertyChanged("IsCompleteSigning");
}
}
/// <summary>
/// Gets or Sets to enable the Save button
/// </summary>
public bool IsSaved
{
get => isSaved;
set
{
isSaved = value;
OnPropertyChanged("IsSaved");
}
}
/// <summary>
/// Gets or Sets the visibility for document status
/// </summary>
public Visibility IsValidationMsgVisibility
{
get => isValidationMsgVisibility;
set
{
isValidationMsgVisibility = value;
OnPropertyChanged("IsValidationMsgVisibility");
}
}
/// <summary>
/// Initializes a new instance of the <see cref="FormFillingViewModel"/> class.
/// </summary>
public InvisibleSignatureViewModel()
{
m_documentStream = GetFileStream("InvisibleDigitalSignature.pdf");
}
private Stream GetFileStream(string fileName)
{
Uri uriResource = new Uri("/syncfusion.pdfviewerdemos.wpf;component/Assets/" + fileName, UriKind.Relative);
StreamResourceInfo streamResourceInfo = Application.GetResourceStream(uriResource);
return streamResourceInfo.Stream;
}
private void OnCompleteSigning(ICommand command)
{
MemoryStream stream = new MemoryStream();
pdfViewer.LoadedDocument.Save(stream);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
PdfPageBase loadedPage = loadedDocument.Pages[0];
PdfLoadedForm loadedForm = loadedDocument.Form;
if (loadedForm != null)
{
for (int index = 0; index < loadedForm.Fields.Count; index++)
{
loadedForm.Fields[index].ReadOnly = true;
}
}
Stream pfxFile = GetFileStream("localhost.pfx");
X509Certificate2Collection collection = new X509Certificate2Collection();
byte[] data = new byte[pfxFile.Length];
pfxFile.Read(data, 0, data.Length);
PdfCertificate pdfCertificate = new PdfCertificate(pfxFile, "password123");
PdfSignature signature = new PdfSignature(loadedDocument, loadedPage, pdfCertificate, "Signature1");
signatureStream = new MemoryStream();
loadedDocument.Save(signatureStream);
pdfViewer.Load(signatureStream);
IsCompleteSigning = false;
IsSignatureAdded = true;
}
private void OnDownload(ICommand command)
{
Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog();
saveFileDialog.FileName = "SaveDocument.pdf";
if (saveFileDialog.ShowDialog() == true)
{
File.WriteAllBytes(saveFileDialog.FileName, signatureStream.ToArray());
IsSaved = false;
}
}
private void OnLoaded(PdfViewerControl control)
{
pdfViewer = control;
pdfViewer.Load(DocumentStream);
pdfViewer.ShowToolbar = false;
pdfViewer.IsBookmarkEnabled = false;
pdfViewer.ThumbnailSettings.IsVisible = false;
pdfViewer.EnableLayers = false;
pdfViewer.PageOrganizerSettings.IsIconVisible = false;
pdfViewer.EnableRedactionTool = false;
pdfViewer.FormSettings.IsIconVisible = false;
pdfViewer.InkAnnotationChanged += PdfViewer_InkAnnotationChanged;
}
private async void OnDocumentLoaded(PdfViewerControl pdfviewer1)
{
if (isSignatureAdded)
{
await Task.Delay(2000);
ValidateSiganture();
await Task.Delay(4000);
IsValidationMsgVisibility = Visibility.Collapsed;
IsSignatureAdded = false;
}
else
{
IsCompleteSigning = false;
IsSaved = false;
}
}
private void PdfViewer_InkAnnotationChanged(object sender, Syncfusion.Windows.PdfViewer.InkAnnotationChangedEventArgs e)
{
if (e.Action == Syncfusion.Windows.PdfViewer.AnnotationChangedAction.Add)
{
IsCompleteSigning = true;
}
}
private void ValidateSiganture()
{
//Loads a PDF document.
if (signatureStream != null)
{
PdfLoadedDocument document = new PdfLoadedDocument(signatureStream);
PdfLoadedForm form = document.Form;
if (form != null)
{
foreach (PdfLoadedField field in form.Fields)
{
if (field is PdfLoadedSignatureField)
{
//Gets the first signature field of the PDF document.
PdfLoadedSignatureField signatureField = field as PdfLoadedSignatureField;
if (signatureField.IsSigned)
{
//X509Certificate2Collection to check the signers identity using root certificates.
X509Certificate2Collection collection = new X509Certificate2Collection();
//Create new X509Certificate2 with the root certificate.
Stream pfxFile = GetFileStream("localhost.pfx");
byte[] data = new byte[pfxFile.Length];
pfxFile.Read(data, 0, data.Length);
X509Certificate2 certificate = new X509Certificate2(data, "password123");
//Add the certificate to the collection.
collection.Add(certificate);
//Validate all signatures in loaded PDF document and get the list of validation result.
PdfSignatureValidationResult result = signatureField.ValidateSignature(collection);
//Checks whether the document is modified or not.
if (result.IsDocumentModified)
{
ValidationMessage = "The document has been digitally signed, but it has been modified since it was signed and at least one signature is invalid .";
IsValidationMsgVisibility = Visibility.Visible;
IsSuccessMsgVisible = Visibility.Collapsed;
IsInvalidMsgVisible = Visibility.Collapsed;
IsErrorMsgVisible = Visibility.Visible;
IsSaved = false;
}
else
{
//Checks whether the signature is valid or not.
if (result.IsSignatureValid)
{
if (result.SignatureStatus.ToString() == "Unknown")
{
ValidationMessage = "The document has been digitally signed and at least one signature has problem";
IsValidationMsgVisibility = Visibility.Visible;
IsInvalidMsgVisible = Visibility.Visible;
IsSuccessMsgVisible = Visibility.Collapsed;
IsErrorMsgVisible = Visibility.Collapsed;
IsSaved = false;
}
else
{
ValidationMessage = "The document has been digitally signed and all the signatures are valid.";
IsValidationMsgVisibility = Visibility.Visible;
IsSuccessMsgVisible = Visibility.Visible;
IsErrorMsgVisible = Visibility.Collapsed;
IsInvalidMsgVisible = Visibility.Collapsed;
IsSaved = true;
}
}
}
}
}
}
}
}
}
protected virtual void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}