Signing a pdf document

2019-07-09 01:22发布

问题:

Currently I am upgrading my old application Which is done using itextsharp 5.0.0 to 5.4.5(latest)... But I have a problem in getting the equivalent code for

PdfSignatureAppearance.SetCrypto(this.myCert.Akp, this.myCert.Chain, null, PdfSignatureAppearance.SELF_SIGNED);

Can anyone help me regarding this?

Thanks in advance...

回答1:

I sign PDF documents using 5.5 version of iTextSharp. Below is the sample code.

        // Set the reader (PdfReader) and output (Stream) first
        PdfStamper stamper = PdfStamper.CreateSignature(reader, output, '\0');

        PdfSignatureAppearance signatureAppearance = stamper.SignatureAppearance;
        signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;
        signatureAppearance.Reason = "I love signing";
        signatureAppearance.LocationCaption = "";
        signatureAppearance.SignatureGraphic = Image.GetInstance(this.imageFolderPath + "sign.png");

        signatureAppearance.SetVisibleSignature(
            new Rectangle(100, 100, 300, 200), 
            reader.NumberOfPages, 
            "Signature");

        // Get certificate from store, here I am reading file
        X509Certificate2 cert = new X509Certificate2(certFile, certPassword);
        var keyPair = DotNetUtils.GetKeyPair(cert.PrivateKey).Private;
        BcX509.X509Certificate bcCert = DotNetUtils.FromX509Certificate(cert);
        var chain = new List<BcX509.X509Certificate> { bcCert };
        IExternalSignature signature = new PrivateKeySignature(keyPair, "SHA-256");

        MakeSignature.SignDetached(signatureAppearance, signature, chain, null, null, null, 0, CryptoStandard.CMS);

        stamper.Close();