Skip to main content
All docs
V25.1
  • How to: Sign a PDF File with a Certificate Stored in a .PFX File

    • 3 minutes to read

    The PdfDocumentSigner class allows you to sign and save documents with signatures (PKCS#7) and certificates (X.509). For example, you can use certificates stored in a .PFX file. To apply a signature to a form field in these documents, use the PdfSignatureBuilder class.

    The following code snippet executes the following actions:

    • Creates a PKCS#7 signature with a certificate stored in a .PFX file,
    • Applies a digital signature to a new and existing form field,
    • Signs a document with these signatures.

    View Example: How to Apply Multiple Signatures

    using System;
    using DevExpress.Pdf;
    using System.Diagnostics;
    using System.IO;
    using DevExpress.Office.DigitalSignatures;
    
    // Load a document to sign:
    using (var signer = new PdfDocumentSigner("Document.pdf"))
    {
    
        // Specify the name and location of the signature field
        var signatureFieldInfo = new PdfSignatureFieldInfo(1);
        signatureFieldInfo.Name = "SignatureField";
        signatureFieldInfo.SignatureBounds = new PdfRectangle(20, 20, 150, 150);
        signatureFieldInfo.RotationAngle = PdfAcroFormFieldRotation.Rotate90;
    
        // Create a PKCS#7 signature:
        Pkcs7Signer pkcs7Signature = new Pkcs7Signer("Signing Documents/certificate.pfx", "123", 
            HashAlgorithmType.SHA256);
    
        // Apply a signature to a newly created signature field:
        var cooperSignature = new PdfSignatureBuilder(pkcs7Signature, signatureFieldInfo);
    
        // Specify an image and signer information:
        cooperSignature.SetImageData(File.ReadAllBytes("Signing Documents//JaneCooper.jpg"));
        cooperSignature.Location = "USA";
        cooperSignature.Name = "Jane Cooper";
        cooperSignature.Reason = "Acknowledgement";
    
        // Apply a signature to an existing form field:
        var santuzzaSignature = new PdfSignatureBuilder(pkcs7Signature, "Sign");
    
        // Specify an image and signer information:
        santuzzaSignature.SetImageData(File.ReadAllBytes("Signing Documents//SantuzzaValentina.jpg"));
        santuzzaSignature.Location = "Australia";
        santuzzaSignature.Name = "Santuzza Valentina";
        santuzzaSignature.Reason = "I Agree";
    
        // Add signatures to an array:
        PdfSignatureBuilder[] signatures = { cooperSignature, santuzzaSignature };
    
        // Sign and save the document:
        signer.SaveDocument("SignedDocument.pdf", signatures);
    }
    
    See Also