-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathProgram.cs
68 lines (62 loc) · 3.57 KB
/
Program.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
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);
//Creates a digital signature.
PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], null, "Signature");
//Sets 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(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(new X509Certificate2(Path.GetFullPath(@"Data/PDF.pfx"), "password123"));
if (digitalID.PrivateKey is System.Security.Cryptography.RSACryptoServiceProvider) {
System.Security.Cryptography.RSACryptoServiceProvider rsa = (System.Security.Cryptography.RSACryptoServiceProvider)digitalID.PrivateKey;
return rsa.SignData(message, HashAlgorithm);
}
else if (digitalID.PrivateKey is RSACng) {
RSACng rsa = (RSACng)digitalID.PrivateKey;
return rsa.SignData(message, System.Security.Cryptography.HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
}
else if (digitalID.PrivateKey is System.Security.Cryptography.RSAOpenSsl)
{
System.Security.Cryptography.RSAOpenSsl rsa = (System.Security.Cryptography.RSAOpenSsl)digitalID.PrivateKey;
return rsa.SignData(message, System.Security.Cryptography.HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
}
return null;
}
}
}
}