-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathProgram.cs
More file actions
83 lines (69 loc) · 3.28 KB
/
Program.cs
File metadata and controls
83 lines (69 loc) · 3.28 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
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Security;
using Syncfusion.Drawing;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
namespace Externally_sign_the_PDF_document
{
internal class Program
{
static void Main(string[] args)
{
// Get the stream from the document
FileStream documentStream = new FileStream(Path.GetFullPath(@"../../../Data/Barcode.pdf"), FileMode.Open, FileAccess.Read);
// Load the existing PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream);
// Create a digital signature
PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], null, "Signature");
// Set the signature information
signature.Bounds = new RectangleF(new PointF(0, 0), new SizeF(100, 30));
signature.Settings.CryptographicStandard = CryptographicStandard.CADES;
signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA1;
// Create an external signer
IPdfExternalSigner externalSignature = new ExternalSigner("SHA1");
// Add public certificates
List<X509Certificate2> certificates = new List<X509Certificate2>();
certificates.Add(new X509Certificate2(Path.GetFullPath(@"../../../Data/PDF.pfx"), "password123"));
signature.AddExternalSigner(externalSignature, certificates, null);
// Create file stream
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
// Save the PDF document to file stream
loadedDocument.Save(outputFileStream);
}
// Close the document
loadedDocument.Close(true);
}
// Create the external signer class and sign the document hash
class ExternalSigner : IPdfExternalSigner
{
private string _hashAlgorithm;
public string HashAlgorithm
{
get { return _hashAlgorithm; }
}
public ExternalSigner(string hashAlgorithm)
{
_hashAlgorithm = hashAlgorithm;
}
public byte[] Sign(byte[] message, out byte[] timeStampResponse)
{
timeStampResponse = null;
X509Certificate2 digitalID = new X509Certificate2(Path.GetFullPath(@"../../../Data/PDF.pfx"), "password123");
if (digitalID.PrivateKey is RSACryptoServiceProvider rsaProvider)
{
return rsaProvider.SignData(message, HashAlgorithm);
}
else if (digitalID.PrivateKey is RSACng rsaCng)
{
return rsaCng.SignData(message, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
}
else if (digitalID.PrivateKey is RSAOpenSsl rsaOpenSsl)
{
return rsaOpenSsl.SignData(message, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
}
return null;
}
}
}
}