-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentSigner.cs
87 lines (79 loc) · 3.91 KB
/
DocumentSigner.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
using System.Security.Cryptography.X509Certificates;
using Microsoft.Extensions.Configuration;
using iTextSharp.text.pdf;
using Org.BouncyCastle.Security;
using X509Certificate = Org.BouncyCastle.X509.X509Certificate;
using iTextSharp.text.pdf.security;
namespace DesktopDocumentSigner
{
internal class DocumentSigner
{
private string location = "Location";
private MemoryStream SignDocument(byte[] file, string document_name)
{
X509Certificate2 certificate = GetCertificateFromStore();
if (certificate != null)
{
var arialBaseFont = BaseFont.CreateFont("ArialCE.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
IList<X509Certificate> chain = new List<X509Certificate>();
PdfReader inputPdf = new PdfReader(file);
using (MemoryStream signedPdf = new MemoryStream())
{
IExternalSignature externalSignature = new X509Certificate2Signature(certificate, "SHA-256");
PdfStamper pdfStamper = PdfStamper.CreateSignature(inputPdf, signedPdf, '\0');
PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
pdfStamper.AcroFields.AddSubstitutionFont(arialBaseFont);
signatureAppearance.Layer2Font = new iTextSharp.text.Font(arialBaseFont, 12f);
signatureAppearance.Location = location;
if (document_name.Contains("DRIVER_CALL"))
{
try
{
signatureAppearance.SetVisibleSignature(new iTextSharp.text.Rectangle(350, 50, 550, 200), inputPdf.NumberOfPages - 1, "Digitally signed");
}
catch (ArgumentException e)
{
return new MemoryStream(file);
}
}
else if (document_name.Contains("ORDER_WITH_EIF"))
{
try
{
signatureAppearance.SetVisibleSignature(new iTextSharp.text.Rectangle(375, 565, 575, 715), 1, "Digitally signed");
}
catch (ArgumentException e)
{
Console.WriteLine($"Dokument {document_name} již byl podepsán, vrácen beze změny ({DateTime.Now}).");
return new MemoryStream(file);
}
}
else
{
try
{
signatureAppearance.SetVisibleSignature(new iTextSharp.text.Rectangle(350, 50, 550, 200), inputPdf.NumberOfPages, "Digitally signed");
}
catch (ArgumentException e)
{
Console.WriteLine($"Dokument {document_name} již byl podepsán, vrácen beze změny ({DateTime.Now}).");
return new MemoryStream(file);
}
}
signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
chain.Add(DotNetUtilities.FromX509Certificate(certificate));
MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
inputPdf.Close();
pdfStamper.Close();
Console.WriteLine($"Dokument {document_name} byl podepsán ({DateTime.Now}).");
return signedPdf;
}
}
return new MemoryStream();
}
private X509Certificate2 GetCertificateFromStore()
{
throw new NotImplementedException();
}
}
}