public static void Sign(IExternalSignature externalSignature, X509Certificate2 rawCertificate, string sourcePdfPath, string destinationPdfPath) { if (externalSignature == null) { throw new ArgumentNullException(nameof(externalSignature)); } if (rawCertificate == null) { throw new ArgumentNullException(nameof(rawCertificate)); } if (sourcePdfPath == null) { throw new ArgumentNullException(nameof(sourcePdfPath)); } if (destinationPdfPath == null) { throw new ArgumentNullException(nameof(destinationPdfPath)); } using PdfReader reader = new PdfReader(sourcePdfPath); Org.BouncyCastle.X509.X509Certificate bCert = Org.BouncyCastle.Security.DotNetUtilities.FromX509Certificate(rawCertificate); Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { bCert }; using FileStream stream = new FileStream(destinationPdfPath, FileMode.OpenOrCreate); PdfSigner signer = new PdfSigner(reader, stream, new StampingProperties()); signer.SetSignatureEvent(new SignatureEvent()); signer.SignDetached(externalSignature, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES); }
public void Sign(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location, String contact, String fullName) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); // Create the signature appearance PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance.SetReason(reason); appearance.SetLocation(location); appearance.SetContact(contact); signer.SetFieldName(name); // Set the signature event to allow modification of the signature dictionary. signer.SetSignatureEvent(new CustomISignatureEvent(fullName)); PrivateKeySignature pks = new PrivateKeySignature(pk, digestAlgorithm); // Sign the document using the detached mode, CMS or CAdES equivalent. signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }