Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void SignPDF(byte[] pdf, SignData signData) //signData: my custom class, it's about sign image,location,size
- {
- string timeStampServer = "http://xxxxxxxx";
- string timeStampUser = "xxxx";
- string timeStampPass = "xxxx";
- string cryptokiDLL = @"C:Program FilesSafeNetLunaClientcryptoki.dll";
- string tokenLabel = "xxx";
- string pin = "xxxx";
- string primaryKeyLabel = "xxxxx";
- string certsDir = @"C:CERTs";
- iTextSharp.text.Image signImg = null;
- iTextSharp.text.Rectangle signRec = null;
- byte[] signedPDF = null;
- string hash = null;
- try
- {
- HashAlgorithm hashAlgorithm = HashAlgorithm.SHA256;
- Pkcs11RsaSignature pkcs11RsaSignature = new Pkcs11RsaSignature(cryptokiDLL, null, tokenLabel, pin, primaryKeyLabel, null, hashAlgorithm);
- byte[] signingCertificate = pkcs11RsaSignature.GetSigningCertificate();//1460
- List<byte[]> otherCertificates = new List<byte[]>();
- foreach (string file in Directory.GetFiles(certsDir))
- otherCertificates.Add(File.ReadAllBytes(file));
- var chain = CertUtils.BuildCertPath(signingCertificate, otherCertificates).ToList();
- ITSAClient tsaClient = new TSAClientBouncyCastle(timeStampServer, timeStampUser, timeStampPass);
- IOcspClient ocspClient = new OcspClientBouncyCastle();
- List<ICrlClient> crlList = new List<ICrlClient>();
- crlList.Add(new CrlClientOnline(chain));
- using (PdfReader pdfReader = new PdfReader(pdf))
- {
- using (MemoryStream msPDFfinal = new MemoryStream())
- {
- using (PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, msPDFfinal, '