public bool signCertificate(string DocumentPath, string CertificateSavePath, Stream privateKeyStream, string keyPassword, string SignatureIMGPath) { try { Pkcs12Store pk12 = new Pkcs12Store(privateKeyStream, keyPassword.ToCharArray()); privateKeyStream.Dispose(); //then Iterate throught certificate entries to find the private key entry string alias = null; foreach (string tAlias in pk12.Aliases) { if (pk12.IsKeyEntry(tAlias)) { alias = tAlias; break; } } var pk = pk12.GetKey(alias).Key; // reader and stamper PdfReader reader = new PdfReader(DocumentPath); int PageCount = reader.NumberOfPages; using (FileStream fout = new FileStream(CertificateSavePath, FileMode.Create, FileAccess.ReadWrite)) { using (PdfStamper stamper = PdfStamper.CreateSignature(reader, fout, '\0', null, true)) { // appearance PdfSignatureAppearance appearance = stamper.SignatureAppearance; //appearance.Image = new iTextSharp.text.pdf.PdfImage(); //appearance.Reason = reason; // appearance.Location = location; appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(163, 72, 297, 24), PageCount, "Icsi-Vendor");//.IsInvisible();//s iTextSharp.text.Image watermark = iTextSharp.text.Image.GetInstance(SignatureIMGPath); appearance.Image = watermark; appearance.Image.ScaleToFit(70, 70); //appearance.Image.Alignment=100; appearance.Image.SetAbsolutePosition(100, 100); appearance.GetAppearance().AddImage(watermark); //digital signature IExternalSignature es = new PrivateKeySignature(pk, "SHA-256"); MakeSignature.SignDetached(appearance, es, new Org.BouncyCastle.X509.X509Certificate[] { pk12.GetCertificate(alias).Certificate }, null, null, null, 0, CryptoStandard.CMS); stamper.Close(); } } return(true); } catch (Exception Ex) { ErrorLog.LogError(Ex); return(false); } }