|
//Load existing PDF document
PdfLoadedDocument document = new PdfLoadedDocument("PDF_Succinctly.pdf");
//Get the existing page.
PdfLoadedPage page = document.Pages[0] as PdfLoadedPage;
//Load the certificate with exportable.
X509Certificate2 x509 = new X509Certificate2("certchain.pfx", "password@123", X509KeyStorageFlags.Exportable);
//Create PdfCertificate instance.
PdfCertificate certificate = new PdfCertificate(x509);
//Create the signature.
PdfSignature signature = new PdfSignature(document, page, certificate, "Signature1");
//Set the digest algorithm.
signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA256;
//Save the PDF document
document.Save("Output.pdf");
//Close the document.
document.Close(true);
|
|
//Load existing PDF document
PdfLoadedDocument document = new PdfLoadedDocument("PDF_Succinctly.pdf");
//Get the existing page.
PdfLoadedPage page = document.Pages[0] as PdfLoadedPage;
//Create the signature with empty certificate.
PdfSignature signature = new PdfSignature(document, page, null, "Signature1");
signature.Bounds = new RectangleF(0, 0, 100, 50);
//Initialize the ComputeHash event.
signature.ComputeHash += Signature_ComputeHash;
//Save the PDF document
document.Save("ExternalSignature.pdf");
//Close the document.
document.Close(true);
|
|
private void Signature_ComputeHash(object sender, PdfSignatureEventArgs ars)
{
//Get the document bytes.
byte[] documentBytes = ars.Data;
SignedCms signedCms = new SignedCms(new ContentInfo(documentBytes), detached: true);
//Compute the signature using the specified digital ID file and the password.
X509Certificate2 certificate = new X509Certificate2("certchain.pfx", "password@123");
var cmsSigner = new CmsSigner(certificate);
cmsSigner.IncludeOption = X509IncludeOption.WholeChain;
//Set the digest algorithm SHA256
cmsSigner.DigestAlgorithm = new Oid("2.16.840.1.101.3.4.2.1");
signedCms.ComputeSignature(cmsSigner);
//Embed the encoded digital signature to the PDF document.
ars.SignedData = signedCms.Encode();
} |